利用API SetupDiGetClassDevs、SetupDiEnumDeviceInterfaces、SetupDiGetDeviceInterfaceDetail抓出指定的裝置資訊,以下範例為抓取Disk的裝置例項路徑
BOOL ListDeviceInstancePath() { HDEVINFO hdev; DWORD idx; GUID guid = GUID_DEVINTERFACE_DISK; CString csDevicePath; BOOL bRet = TRUE; BOOL nStatus; DWORD dwSize = 0; hdev = SetupDiGetClassDevs( &guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (hdev == INVALID_HANDLE_VALUE ) { printf("ERROR : Unable to enumerate device.\n"); return FALSE; } SP_DEVICE_INTERFACE_DATA DeviceInterfaceData; DeviceInterfaceData.cbSize = sizeof(DeviceInterfaceData); for (idx = 0 ; SetupDiEnumDeviceInterfaces(hdev, NULL, &guid, idx, &DeviceInterfaceData); idx++) { nStatus = SetupDiGetDeviceInterfaceDetail(hdev, &DeviceInterfaceData, NULL, 0, &dwSize, NULL); if (!dwSize) { bRet = FALSE; printf("ERROR : SetupDiGetDeviceInterfaceDetail fial.\n"); break; } PSP_DEVICE_INTERFACE_DETAIL_DATA pBuffer = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(dwSize); ZeroMemory(pBuffer, sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA)); pBuffer->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)}; nStatus = SetupDiGetDeviceInterfaceDetail(hdev, &DeviceInterfaceData, pBuffer, dwSize, &dwSize, &DeviceInfoData); if(!nStatus) { bRet = FALSE; printf("ERROR : SetupDiGetDeviceInterfaceDetail fial.\n"); break; } csDevicePath = pBuffer->DevicePath; csDevicePath.MakeUpper(); printf("%s\n", csDevicePath); } SetupDiDestroyDeviceInfoList(hdev); return bRet; }抓取畫面
沒有留言:
張貼留言