可直接编译(设置成:使用多字节字符集)
转来的,代码:
/* http://www.experts-exchange.com/Programming/Editors_IDEs/Q_24506125.html */ #include <stdio.h> #include <windows.h> #include <setupapi.h> #include <devguid.h> #include <winioctl.h> #include <regstr.h> #include <tchar.h> #include <string> using namespace std; #pragma comment(lib,"setupapi.lib") /* 获取设备数 路径: 如G: 设备数 */ BOOL GetDeviceNumber(LPCTSTR pszDevPath, DWORD& dwDevNum) { BOOL bRC = FALSE; HANDLE hDrive = CreateFile( pszDevPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL ); if (INVALID_HANDLE_VALUE != hDrive) { STORAGE_DEVICE_NUMBER sdn; DWORD dwBytesReturned = 0; bRC = DeviceIoControl( hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwBytesReturned, NULL ); if (bRC) dwDevNum = sdn.DeviceNumber; CloseHandle(hDrive); }else printf("Error:[%d] (%s) ",GetLastError(),pszDevPath); return bRC; } int GetDeviceDescription(LPCTSTR pszDrive, TCHAR* pszDesc, const size_t szDescSize) { TCHAR acDevName[MAX_PATH]; // QueryDosDevice(pszDrive,acDevName,MAX_PATH); // printf("Test: %s ",acDevName); _stprintf(acDevName,"\\.\%s", pszDrive); DWORD dwNumOfInterest = -1;; GetDeviceNumber(acDevName,dwNumOfInterest); //printf("DeviceNumber:[%d] ",dwNumOfInterest); GUID* guid = (GUID*)&GUID_DEVINTERFACE_DISK; // Get device interface info set handle // for all devices attached to system HDEVINFO hDevInfo = SetupDiGetClassDevs(guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (hDevInfo == INVALID_HANDLE_VALUE) { return 0; } // Retrieve a context structure for a device interface // of a device information set. DWORD dwIndex = 0; BOOL bRet = FALSE; BYTE Buf[1024]; PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)Buf; SP_DEVICE_INTERFACE_DATA spdid; SP_DEVINFO_DATA spdd; DWORD dwSize; spdid.cbSize = sizeof(spdid); while ( true ) { bRet = SetupDiEnumDeviceInterfaces(hDevInfo, NULL, guid, dwIndex, &spdid); if (!bRet) { break; } dwSize = 0; SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, NULL, 0, &dwSize, NULL); if ( dwSize!=0 && dwSize<=sizeof(Buf) ) { pspdidd->cbSize = sizeof(*pspdidd); // 5 Bytes! ZeroMemory((PVOID)&spdd, sizeof(spdd)); spdd.cbSize = sizeof(spdd); long res = SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, pspdidd, dwSize, &dwSize, &spdd); printf("Result:[%s] ",pspdidd->DevicePath); // <--------- here! DWORD dwType; LPTSTR buffer = NULL; DWORD buffersize = 0; while (!SetupDiGetDeviceRegistryProperty( hDevInfo, &spdd, SPDRP_FRIENDLYNAME, &dwType, (PBYTE)buffer, buffersize, &buffersize)) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Change the buffer size. if (buffer) LocalFree(buffer); // Double the size to avoid problems on // W2k MBCS systems per KB 888609. buffer =(LPTSTR) LocalAlloc(LPTR,buffersize * 2); } else { // Insert error handling here. break; } } DWORD dwNum = 0;; GetDeviceNumber(pspdidd->DevicePath,dwNum); //printf("DeviceNumber:[%d] ",dwNum); //if (dwNumOfInterest == dwNum) _stprintf(pszDesc,_T("%s"),buffer); if (dwNumOfInterest == dwNum) _stprintf(pszDesc,_T("%s"),pspdidd->DevicePath); //printf("Result:[%s] ",buffer); if (buffer) LocalFree(buffer); } dwIndex++; } SetupDiDestroyDeviceInfoList(hDevInfo); return 0; } void main (int argc, char** argv) { TCHAR acDesc[1024] = "<unknown>"; TCHAR* pszDrive; if (2 != argc) { printf("Usage: usbdev.exe <drive letter> (ex.: 'usbdev f:') "); } pszDrive = *(argv + 1); GetDeviceDescription(pszDrive, acDesc, 1024); _tprintf(" Description for %s %s ", pszDrive, acDesc); system("pause"); }