CString ConvertToHex(CString data)
{
	 CString returnvalue;

     for (int x = 0; x < data.GetLength(); x++)
     {
          CString temporary;
          int value = (int)(data[x]);
          temporary.Format("%02X ", value);
          returnvalue += temporary;
     }
     return returnvalue;
}


기본 자료형의 종류 (상세 정리)
구분자료형크기(byte)범위
문자형char1 byte-128 ~ 127
unsigned char1 byte0 ~ 255
정수형__int81 byte-128 ~ 127
__int162 byte-32,768 to 32,767
unsigned int2 byte-32,768 to 32,767
(signed) short (int)2 byte-32,768 to 32,767
unsigned short (int)2 byte0 ~ 65,535
__int324 byte-2,147,483,648 ~ 2,147,483,647
(signed) int4 byte-2,147,483,648 ~ 2,147,483,647
unsigned int4 byte0 ~ 4,294,967,295
(signed) long (int)4 byte-2,147,483,648 ~ 2,147,483,647
unsigned long (ing)4 byte-2,147,483,648 ~ 2,147,483,647
__int648 byte-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
실수형float4 byte3.4E +/- 38 (7 digits)
double8 byte1.7E +/- 308 (15 digits)
long double8 byte1.2E +/- 4932 (19 digits)



unsigned long       DWORD

int                        BOOL;

unsigned char       BYTE;

unsigned short      WORD;

unsigned int          UINT;

참조 : http://i0nucleus.egloos.com/2957944


================================================================================================================


[C / WinAPI] Lan 연결 상태 알아내기

 

랜 선이 연결 여부를 알고 싶을 때가 있습니다. 3가지 방법이 있습니다.

WinXP 와 Win7에서 테스트를 하였습니다. Visual Studio 2005 (콘솔모드)에서 컴파일 하였습니다.

테스트할 때 직접 랜 선을 뽑아가면서 했습니다. 인터넷, MSDN을 찾아가면서 샘플 코드를 만들어 보았습니다. 휴~


[Method 3]은 코드 분석할 때 MSDN을 참조하세요.


각각 나름씩 장단점이 있습니다.

소켓 프로그래밍 할 때 적용해서 프로그램 만들어야 겠어욤.....


더 좋은 방법있으면 알려주세요. 댓글 남겨주세요. 




-----------------------------------------------------------------------------------------------------------------

[Method 3]

MIB_IFTABLE, MIB_IFROW 이용하여 랜 상태를 알 수 있습니다. iphlpapi.h 포함.

인터넷과, MSDN을 참조하여 수정하였습니다. 이더넷 관련 모든 장치를 볼 수 있습니다.

 

코딩이 좀 복잡함, 지연 없음,  WinXP, Win7에서 정상적으로 작동함.

 


[Source Code 3]

#include 
#include 
 
#include 
#pragma comment(lib, "iphlpapi.lib")
 
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
 
int main()
{
           DWORD dwSize = 0;
           DWORD dwRetVal = 0;
 
           int i;
 
           MIB_IFTABLE *pIfTable;
           MIB_IFROW *pIfRow;
 
           pIfTable = (MIB_IFTABLE *) MALLOC(sizeof (MIB_IFTABLE));
           if (pIfTable == NULL)
           {
                     printf("Error allocating memory needed to call GetIfTable\n");
                     exit (1);
           }
 
           dwSize = sizeof (MIB_IFTABLE);
           if (GetIfTable(pIfTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
           {
                     FREE(pIfTable);
                     pIfTable = (MIB_IFTABLE *) MALLOC(dwSize);
                     if (pIfTable == NULL)
                     {
                                printf("Error allocating memory\n");
                                exit (1);
                     }
           }
 
           while(1)
           {
                     Sleep(500);
 
                     if ((dwRetVal = GetIfTable(pIfTable, &dwSize, 0)) == NO_ERROR)
                     {
                                if (pIfTable->dwNumEntries > 0)
                                {
                                          pIfRow = (MIB_IFROW *) MALLOC(sizeof (MIB_IFROW));
                                          if (pIfRow == NULL)
                                          {
                                                     printf("Error allocating memory\n");
                                                     if (pIfTable != NULL)
                                                     {
                                                                FREE(pIfTable);
                                                                pIfTable = NULL;
                                                     }
                                                     exit (1);
                                          }
 
                                          for (i = 0; i < (int) pIfTable->dwNumEntries; i++)
                                          {
                                                     pIfRow->dwIndex = pIfTable->table[i].dwIndex;
                                                     if ((dwRetVal = GetIfEntry(pIfRow)) == NO_ERROR)
                                                     {
                                                                if(pIfRow->dwType != MIB_IF_TYPE_ETHERNET) continue;
 
                                                                printf("\n\tIndex:\t %ld\n", pIfRow->dwIndex);
                                                                printf("\tOper Status[%d]:\t ", i);
                                                                switch (pIfRow->dwOperStatus)
                                                                {
                                                                case MIB_IF_OPER_STATUS_NON_OPERATIONAL:
                                                                          printf("Non Operational\n");
                                                                          break;
                                                                case MIB_IF_OPER_STATUS_OPERATIONAL:
                                                                          printf("Operational\n");
                                                                          break;
                                                                default:
                                                                          printf("Unknown status %ld\n", pIfRow->dwAdminStatus);
                                                                          break;
                                                                }
                                                     }                              
                                          }
                                }
                     }
           }
 
           return 0;
}

 

[Result 3]

 

        Index:   2

        Oper Status[0]:  Operational

 

                   :

 

        Index:   2

        Oper Status[0]:  Operational

 

        Index:   2

        Oper Status[0]:  Non Operational

 

        Index:   2

        Oper Status[0]:  Non Operational

 

        Index:   2

        Oper Status[0]:  Non Operational

 

        Index:   2

        Oper Status[0]:  Operational

 

                     :

목표 : MFC Dialog 환경에서 폴더를 선택하면 해당 폴더내 파일명 앞에 년원일 이 붙도록 한다.


폴더 선택 경로 얻기 -> 해당 경로내 파일 검색 -> 시간 얻기 -> isDot -> 파일명 변경 -> 계속



1. 폴더 경로 얻기.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    ITEMIDLIST *pidBrowse;
 
    char  pszPathname[MAX_PATH];
 
    BROWSEINFO BrInfo;
 
    BrInfo.hwndOwner = GetSafeHwnd();
 
    BrInfo.pidlRoot = NULL;
 
    memset(&BrInfo,0,sizeof(BrInfo));
 
    BrInfo.pszDisplayName = pszPathname;
 
    BrInfo.lpszTitle="selec Folder";
 
    BrInfo.ulFlags = BIF_RETURNONLYFSDIRS;
 
    pidBrowse = SHBrowseForFolder(&BrInfo);
 
    if(pidBrowse != NULL)
 
    {
 
        BOOL bSuccess = ::SHGetPathFromIDList(pidBrowse,pszPathname);
 
        if(bSuccess)
 
        {
 
            m_strPath = pszPathname;
 
            UpdateData(FALSE);
 
        }
 
        else{
 
            MessageBox("wrong Folder naem!","lol",MB_ICONASTERISK);
 
        }
 
    }
 
 
cs


2. 시간 얻기



3. 경로내 파일 검색


 

1
2
3
4
5
6
7
8
    CFileFind finder;
 
    bool working = finder.FindFile(m_strPath+"\\*.*");
 
    while(working)
    {
        working = finder.FindNextFile();
    }
cs



4. IsDots()

폴더를 검색하면 상위 폴더를 보는 . 과 .. 이 있기때문에 이를 걸러주어야함.


1
2
3
4
5
6
7
8
9
10
    if(finder.IsDots())    continue;
 
    ex)CFileFind finder;
 
    bool working = finder.FindFile(m_strPath+"\\*.*");
    while(working)
    {
        if(finder.IsDots())    continue;
        working = finder.FindNextFile();
    }
cs



5. 파일명 변경

CFile::Rename(strFindname,strTemp);


첫번째 인자는 전체 경로를 포함한 이전 파일명, 두번째 인자는 전체 경로를 포함한 새로 바뀔 파일명




목표 : 작업폴더내 모든 파일명 앞에 현재 날짜 년 월 일  이 붙도록 하는 콘솔 프로그램

단계 :

폴더 경로 얻기 -> 폴더 경로 내의 파일 검색 -> isDot  - no -> 파일명 수정 -> next

  - yes-> next



1. 폴더 (Directory) 경로 얻기


함수 : GetCurrentDirectory



2. 폴더내 파일 검색


struct _finddata_t cfile;

long hFile;



if( (hFile = _findfirst( "*.*", &c_file )) == -1L )

printf( "No files in current directory!\n" );

else

{

/* Find the rest of the .c files */

do

{

  // 작업

     }while( _findnext( hFile, &c_file ) == 0 );


_findclose( hFile );

}









3. isDot ?


콘솔로 폴더를 검색하면 상위 폴더를 보는 . 과 .. 이 있기때문에 이를 걸러주어야함.

if(!strcmp(".", c_file.name)) _findnext( hFile, &c_file );

if(!strcmp("..", c_file.name)) _findnext( hFile, &c_file );




4. 시스템 시간 얻기

SYSTEMTIME 을 이용하는 방법, time_t를 이용하는 방법이 있는데, 

time_t는 뭔가 부정확한 느낌이다.





5. 파일명 바꾸기.


rename 함수를 이용한다

rename("기존 파일명 (경로포함)" , "바뀔 파일명(경로포함);














VBA 편집기( alt + f11 ) -> 삽입 -> 모듈 에 아래 함수를 입력



Function LastSave()

LastSave = ThisWorkbook.BuiltinDOcumentProperties("Last save time")

End Function



이후 워크시트 원하는 셀에 =LastSave() 입력

C++ 현재시간을 구한다. 

1. SYSTEMTIME 을 이용
CString GetToDay(CString data)
{
    CString strToday;
    SYSTEMTIME    time;    // 시간 구조체.
    ::ZeroMemory(reinterpret_cast<void*>(&time), sizeof(time));     // time 초기화.
    ::GetLocalTime(&time);    // 현재시간을 얻음.
    strToday.Format (_T("%4d%02d%02d%02d%02d"), time.wYear ,time.wMonth ,time.wDay, time.wHour, time.wMinute); // 형식에 맞게 받음..
    return strToday;
}
2. time_t 를 이용
char sysDate[MAX_PATH2] ="";

struct tm* today;

time_t ltime;

time(<ime);

today = localtime(<ime);

sprintf(sysDate, "%04d년%02d월%02d일_",today->tm_yday+1778, today->tm_mon+1, today->tm_mday);


3. CTime 활용


CString strToday = CTime::GetcurrentTime();


BOOL CTestDlg::OnInitDialog()
{
 ....

 

 MoveWindow(0,0,1024,768);


...

}

+ Recent posts