[VC(Visual C++) ]- 53_抓取硬體資訊
[VC(Visual C++) ]- 53_抓取硬體資訊
本篇分享53_抓取硬體資訊範例,有興趣的(C/P)同好,歡迎來http://yfdisk.com/file/jashliao/fdb36af8/索取,因為我不會上傳檔案分享哈哈 ^ ^。
程式碼 |
001 #include <stdio.h> 002 #include <windows.h> 003 #include <setupapi.h> 004 #include <devguid.h> 005 #include <regstr.h> 006
007 int main( int argc, char *argv[ ], char *envp[ ] ) 008 {
009 HDEVINFO hDevInfo;
010 SP_DEVINFO_DATA DeviceInfoData;
011 DWORD i;
012
013 // Create a HDEVINFO with all present devices. 014 hDevInfo = SetupDiGetClassDevs(NULL,
015 0, // Enumerator 016 0,
017 DIGCF_PRESENT | DIGCF_ALLCLASSES );
018
019 if (hDevInfo == INVALID_HANDLE_VALUE) 020 {
021 // Insert error handling here. 022 return 1; 023 }
024
025 // Enumerate through all devices in Set. 026
027 DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); 028 for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i, 029 &DeviceInfoData);i++)
030 {
031 DWORD DataT;
032 LPTSTR buffer = NULL;
033 DWORD buffersize = 0;
034
035 // 036 // Call function with null to begin with, 037 // then use the returned buffer size (doubled) 038 // to Alloc the buffer. Keep calling until 039 // success or an unknown failure. 040 // 041 // Double the returned buffersize to correct 042 // for underlying legacy CM functions that 043 // return an incorrect buffersize value on 044 // DBCS/MBCS systems. 045 // 046 while (!SetupDiGetDeviceRegistryProperty( 047 hDevInfo,
048 &DeviceInfoData,
049 SPDRP_DEVICEDESC,
050 &DataT,
051 (PBYTE)buffer,
052 buffersize,
053 &buffersize))
054 {
055 if (GetLastError() == 056 ERROR_INSUFFICIENT_BUFFER)
057 {
058 // Change the buffer size. 059 if (buffer) LocalFree(buffer); 060 // Double the size to avoid problems on 061 // W2k MBCS systems per KB 888609. 062 buffer =(char *)LocalAlloc(LPTR,buffersize * 2); 063 }
064 else 065 {
066 // Insert error handling here. 067 break; 068 }
069 }
070
071 printf("Result:[%s]\n",buffer);
072
073 if (buffer) LocalFree(buffer); 074 }
075
076
077 if ( GetLastError()!=NO_ERROR && 078 GetLastError()!=ERROR_NO_MORE_ITEMS )
079 {
080 // Insert error handling here. 081 return 1; 082 }
083
084 // Cleanup 085 SetupDiDestroyDeviceInfoList(hDevInfo);
086
087 return 0; 088 }
|