출처 : http://linkc.tistory.com/entry/System-Information-%EC%9D%84-%EA%B0%80%EC%A0%B8%EC%98%A4%EB%8A%94-API
1. Computer Name 가져오기
– GetComputerName
- CString SerialPort::Get_Sys_Info()
- {
- TCHAR szComName[100];
- DWORD len=100;
- if ( GetComputerName(szComName,&len) )
- {
- return szComName;
- }
- else
- {
- return “Fail to Load the Computer Name “;
- }
- }
2. CPU Information 가져오기
– GetSystemInfo
다만 이 함수는 구체적인 정보를 얻을 수 없더군요
그래서 다른 방법을 찾아봤고 Registry를 이용하는 방법을 찾아냈습니다
– RegOpenKeyEx -> RegQueryValueEX
- CString Get_Cpu_Info()
- {
- TCHAR Cpu_info[100];
- HKEY hKey;
- HKEY hk_1;
- int i = 0;
- long result=0;
- DWORD c_size = sizeof(Cpu_info);
- RegOpenKeyEx(HKEY_LOCAL_MACHINE,“Hardware\Description\System\CentralProcessor\0”,0,KEY_QUERY_VALUE,&hKey);
- RegQueryValueEx (hKey, “ProcessorNameString”, NULL, NULL, (LPBYTE) Cpu_info , &c_size);
- RegCloseKey (hKey);
- return Cpu_info;
- }
3. OS Information
– GetVersionEx
OS 같은 경우도 좀 까다로운데 ..
각 버전마다 일치하는 OS를 지정해줘야합니다
일단 MS 에서 예제로 제공한 코드입니다
상당히 자세한 정보를 얻을 수 있지만
하위 Windows 에서는 안돌아가는 걸로 확인했습니다
제가 7을 쓰고 있는데 7은 정상작동합니다 😀
- #include <windows.h>
- #include <tchar.h>
- #include <stdio.h>
- #include <strsafe.h>
- #pragma comment(lib, “User32.lib”)
- #define BUFSIZE 256
- typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
- typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
- BOOL GetOSDisplayString( LPTSTR pszOS)
- {
- OSVERSIONINFOEX osvi;
- SYSTEM_INFO si;
- PGNSI pGNSI;
- PGPI pGPI;
- BOOL bOsVersionInfoEx;
- DWORD dwType;
- ZeroMemory(&si, sizeof(SYSTEM_INFO));
- ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
- osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
- if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
- return 1;
- // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
- pGNSI = (PGNSI) GetProcAddress(
- GetModuleHandle(TEXT(“kernel32.dll”)),
- “GetNativeSystemInfo”);
- if(NULL != pGNSI)
- pGNSI(&si);
- else GetSystemInfo(&si);
- if ( VER_PLATFORM_WIN32_NT==osvi.dwPlatformId &&
- osvi.dwMajorVersion > 4 )
- {
- StringCchCopy(pszOS, BUFSIZE, TEXT(“Microsoft “));
- // Test for the specific product.
- if ( osvi.dwMajorVersion == 6 )
- {
- if( osvi.dwMinorVersion == 0 )
- {
- if( osvi.wProductType == VER_NT_WORKSTATION )
- StringCchCat(pszOS, BUFSIZE, TEXT(“Windows Vista “));
- else StringCchCat(pszOS, BUFSIZE, TEXT(“Windows Server 2008 “ ));
- }
- if ( osvi.dwMinorVersion == 1 )
- {
- if( osvi.wProductType == VER_NT_WORKSTATION )
- StringCchCat(pszOS, BUFSIZE, TEXT(“Windows 7 “));
- else StringCchCat(pszOS, BUFSIZE, TEXT(“Windows Server 2008 R2 “ ));
- }
- pGPI = (PGPI) GetProcAddress(
- GetModuleHandle(TEXT(“kernel32.dll”)),
- “GetProductInfo”);
- pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
- switch( dwType )
- {
- case PRODUCT_ULTIMATE:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Ultimate Edition” ));
- break;
- case PRODUCT_PROFESSIONAL:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Professional” ));
- break;
- case PRODUCT_HOME_PREMIUM:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Home Premium Edition” ));
- break;
- case PRODUCT_HOME_BASIC:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Home Basic Edition” ));
- break;
- case PRODUCT_ENTERPRISE:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Enterprise Edition” ));
- break;
- case PRODUCT_BUSINESS:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Business Edition” ));
- break;
- case PRODUCT_STARTER:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Starter Edition” ));
- break;
- case PRODUCT_CLUSTER_SERVER:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Cluster Server Edition” ));
- break;
- case PRODUCT_DATACENTER_SERVER:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Datacenter Edition” ));
- break;
- case PRODUCT_DATACENTER_SERVER_CORE:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Datacenter Edition (core installation)” ));
- break;
- case PRODUCT_ENTERPRISE_SERVER:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Enterprise Edition” ));
- break;
- case PRODUCT_ENTERPRISE_SERVER_CORE:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Enterprise Edition (core installation)” ));
- break;
- case PRODUCT_ENTERPRISE_SERVER_IA64:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Enterprise Edition for Itanium-based Systems” ));
- break;
- case PRODUCT_SMALLBUSINESS_SERVER:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Small Business Server” ));
- break;
- case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Small Business Server Premium Edition” ));
- break;
- case PRODUCT_STANDARD_SERVER:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Standard Edition” ));
- break;
- case PRODUCT_STANDARD_SERVER_CORE:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Standard Edition (core installation)” ));
- break;
- case PRODUCT_WEB_SERVER:
- StringCchCat(pszOS, BUFSIZE, TEXT(“Web Server Edition” ));
- break;
- }
- }
- if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
- {
- if( GetSystemMetrics(SM_SERVERR2) )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Windows Server 2003 R2, “));
- else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Windows Storage Server 2003”));
- else if ( osvi.wSuiteMask & VER_SUITE_WH_SERVER )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Windows Home Server”));
- else if( osvi.wProductType == VER_NT_WORKSTATION &&
- si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
- {
- StringCchCat(pszOS, BUFSIZE, TEXT( “Windows XP Professional x64 Edition”));
- }
- else StringCchCat(pszOS, BUFSIZE, TEXT(“Windows Server 2003, “));
- // Test for the server type.
- if ( osvi.wProductType != VER_NT_WORKSTATION )
- {
- if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 )
- {
- if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Datacenter Edition for Itanium-based Systems” ));
- else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Enterprise Edition for Itanium-based Systems” ));
- }
- else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
- {
- if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Datacenter x64 Edition” ));
- else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Enterprise x64 Edition” ));
- else StringCchCat(pszOS, BUFSIZE, TEXT( “Standard x64 Edition” ));
- }
- else
- {
- if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Compute Cluster Edition” ));
- else if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Datacenter Edition” ));
- else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Enterprise Edition” ));
- else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Web Edition” ));
- else StringCchCat(pszOS, BUFSIZE, TEXT( “Standard Edition” ));
- }
- }
- }
- if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
- {
- StringCchCat(pszOS, BUFSIZE, TEXT(“Windows XP “));
- if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Home Edition” ));
- else StringCchCat(pszOS, BUFSIZE, TEXT( “Professional” ));
- }
- if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
- {
- StringCchCat(pszOS, BUFSIZE, TEXT(“Windows 2000 “));
- if ( osvi.wProductType == VER_NT_WORKSTATION )
- {
- StringCchCat(pszOS, BUFSIZE, TEXT( “Professional” ));
- }
- else
- {
- if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Datacenter Server” ));
- else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
- StringCchCat(pszOS, BUFSIZE, TEXT( “Advanced Server” ));
- else StringCchCat(pszOS, BUFSIZE, TEXT( “Server” ));
- }
- }
- // Include service pack (if any) and build number.
- if( _tcslen(osvi.szCSDVersion) > 0 )
- {
- StringCchCat(pszOS, BUFSIZE, TEXT(” “) );
- StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion);
- }
- TCHAR buf[80];
- StringCchPrintf( buf, 80, TEXT(” (build %d)”), osvi.dwBuildNumber);
- StringCchCat(pszOS, BUFSIZE, buf);
- if ( osvi.dwMajorVersion >= 6 )
- {
- if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
- StringCchCat(pszOS, BUFSIZE, TEXT( “, 64-bit” ));
- else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL )
- StringCchCat(pszOS, BUFSIZE, TEXT(“, 32-bit”));
- }
- return TRUE;
- }
- else
- {
- printf( “This sample does not support this version of Windows.n”);
- return FALSE;
- }
- }
- int __cdecl _tmain()
- {
- TCHAR szOS[BUFSIZE];
- if( GetOSDisplayString( szOS ) )
- _tprintf( TEXT(“n%sn”), szOS );
- }
좀더 간략한 정보를 얻는 것이 목적이라면 다음과 같이 수정할 수 있습니다[ 2000 이하의 OS는 생각하지 않았습니다 ]
- CString Get_Os_Info()
- {
- CString Os_info;
- DWORD dwType;
- char version = -1;
- OSVERSIONINFOEX osvi;
- SYSTEM_INFO sys;
- GetSystemInfo(&sys);
- BOOL version_ex_flag = 0;
- ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
- osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
- if(!(version_ex_flag = GetVersionEx((OSVERSIONINFO *)&osvi))){
- osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
- if(!GetVersionEx((OSVERSIONINFO *)&osvi)) return -1;
- }
- switch(osvi.dwMajorVersion){
- case 5:
- if (osvi.dwMinorVersion == 0)
- Os_info = “Windows 2000”;
- else if ( osvi.dwMinorVersion == 1)
- Os_info = “Windows XP”;
- else
- {
- if(osvi.wSuiteMask & VER_SUITE_WH_SERVER)
- Os_info =” Windows Home Server”;
- else if ((osvi.wProductType == VER_NT_WORKSTATION)
- && (sys.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64))
- Os_info =“Windows XP Professional x64 Edition”;
- if(GetSystemMetrics(SM_SERVERR2) == 0 )
- Os_info = “Windows Server 2003 “;
- else
- Os_info = “Windows Server 2003 R2 “;
- }
- break;
- case 6:
- if (osvi.dwMinorVersion == 0)
- {
- if(osvi.wProductType == VER_NT_WORKSTATION)
- Os_info = “Windows Vista”;
- else
- Os_info = “Windows Server 2008”;
- }
- else if ( osvi.dwMinorVersion == 1)
- {
- if(osvi.wProductType == VER_NT_WORKSTATION)
- Os_info = “Windows 7”;
- else
- Os_info = “Windows Server 2008 R2 “;
- }
- break;
- default:
- return “Unknown OS”;
- }
- return Os_info;
- }
다른 방법으로 레지스트리에서 가져오는 방법도 있습니다 이 방법이 가장 간단하군요
- CString Get_Os_Info()
- {
- TCHAR ProductName[100];
- TCHAR CSDVersion[100];
- CString Os_info;
- HKEY hKey;
- int i = 0;
- DWORD c_size = 100;
- if( RegOpenKeyEx(HKEY_LOCAL_MACHINE,“SOFTWARE\Microsoft\Windows NT\CurrentVersion\”,0,KEY_QUERY_VALUE,&hKey) != ERROR_SUCCESS )
- return “Fail to Open Os_info”;
- if(RegQueryValueEx (hKey, “ProductName”, NULL, NULL, (LPBYTE) ProductName, &c_size) != ERROR_SUCCESS)
- return “Fail to Load the ProductName”;
- if(RegQueryValueEx (hKey, “CSDVersion”, NULL, NULL, (LPBYTE) CSDVersion , &c_size) != ERROR_SUCCESS)
- {
- RegCloseKey (hKey);
- return ProductName;
- }
- Os_info = ProductName;
- Os_info += ” “;
- Os_info += CSDVersion;
- RegCloseKey (hKey);
- return Os_info;
- }
4. Hard Disk Information 가져오기
– GetDiskFreeSpaceEX
하드의 사용용량/ 전체용량을 구합니다
- CString Get_PhyDriver_Info()
- {
- CString Use, Total;
- ULARGE_INTEGER nFreeBytes;
- ULARGE_INTEGER nTotalBytes;
- ULARGE_INTEGER nTotalOfFree;
- if ( GetDiskFreeSpaceEx( _T(“C:”), &nFreeBytes, &nTotalBytes, &nTotalOfFree ) == NULL)
- return “Fail to load Driver Info”;
- // Total – Free = Use
- Use.Format(“%d”,nTotalBytes.QuadPart/1024/1024 – nFreeBytes.QuadPart/1024/1024);
- Use += “MB / “;
- // Free
- Total.Format(“%d”,nTotalBytes.QuadPart/1024/1024);
- Total += “MB”;
- return Use+Total;
- }
5. IP Information 가져오기
– gethostname -> gethostbyname
IP를 한번에 가져오는 API 없고 다음과 같이 2개의 API를 이용하는 방법이 있습니다
- CString Get_Ip()
- {
- WSADATA wsadata;
- if( !WSAStartup( DESIRED_WINSOCK_VERSION, &wsadata ) )
- {
- if( wsadata.wVersion >= MINIMUM_WINSOCK_VERSION )
- {
- CString str; // 이 변수에 IP주소가 저장된다.
- HOSTENT *p_host_info;
- IN_ADDR in;
- char host_name[128]={0, };
- gethostname(host_name, 128);
- p_host_info = gethostbyname( host_name );
- if( p_host_info != NULL )
- {
- for( int i = 0; p_host_info->h_addr_list[i]; i++ )
- {
- memcpy( &in, p_host_info->h_addr_list[i], 4 );
- str = inet_ntoa( in );
- }
- }
- return str;
- }
- WSACleanup();
- }
- return “Fail”;
- }
6. Mac Address 가져오기
GetAdaptersInfo
UuidCreate
NetWkstaTransportEnum – NETBIOS 이용
Mac Address를 가져오는 방법은 좀 다양한데 대표적인 3가지 방법이 위 3 가지 입니다
그중에서 저는 GetAdaptersInfo를 이용해서 구해보도록 하죠
- CString Get_Mac()
- {
- CString strMac;
- DWORD size = sizeof(PIP_ADAPTER_INFO);
- PIP_ADAPTER_INFO Info;
- ZeroMemory( &Info, size );
- int result = GetAdaptersInfo( Info, &size ); // 첫번째 랜카드 MAC address 가져오기
- if (result == ERROR_BUFFER_OVERFLOW) // GetAdaptersInfo가 메모리가 부족하면 재 할당하고 재호출
- {
- Info = (PIP_ADAPTER_INFO)malloc(size);
- GetAdaptersInfo( Info, &size );
- }
- if(!Info)
- return strMac;
- strMac.Format(“%0.2X-%0.2X-%0.2X-%0.2X-%0.2X-%0.2X”, Info->Address[0], Info->Address[1], Info->Address[2], Info->Address[3], Info->Address[4], Info->Address[5] );
- return strMac;
- }