码迷,mamicode.com
首页 > 编程语言 > 详细

C++获取设备 PID,VID 信息

时间:2019-10-11 20:22:49      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:device   code   als   style   tail   turned   upd   syntax   max   

可直接编译(设置成:使用多字节字符集)

转来的,代码:

  1 /*
  2 http://www.experts-exchange.com/Programming/Editors_IDEs/Q_24506125.html
  3 */
  4  
  5 #include <stdio.h>
  6 #include <windows.h>
  7 #include <setupapi.h>
  8 #include <devguid.h>
  9 #include <winioctl.h>
 10 #include <regstr.h>
 11 #include <tchar.h>
 12 #include <string>
 13 using namespace std;
 14  
 15 #pragma comment(lib,"setupapi.lib")
 16  
 17 /*
 18 获取设备数
 19     路径: 如G:
 20     设备数
 21 */
 22 BOOL GetDeviceNumber(LPCTSTR pszDevPath, DWORD& dwDevNum) {
 23  
 24     BOOL bRC = FALSE;
 25  
 26     HANDLE hDrive = CreateFile(
 27         pszDevPath,
 28         0,
 29         FILE_SHARE_READ | FILE_SHARE_WRITE,
 30         NULL,
 31         OPEN_EXISTING, 
 32         NULL, 
 33         NULL
 34         );
 35  
 36     if (INVALID_HANDLE_VALUE != hDrive) {
 37  
 38         STORAGE_DEVICE_NUMBER sdn;
 39         DWORD dwBytesReturned = 0;
 40  
 41         bRC = DeviceIoControl(
 42             hDrive,
 43             IOCTL_STORAGE_GET_DEVICE_NUMBER,
 44             NULL, 
 45             0, 
 46             &sdn, 
 47             sizeof(sdn),
 48             &dwBytesReturned, 
 49             NULL
 50             );
 51  
 52  
 53         if (bRC) dwDevNum = sdn.DeviceNumber;
 54  
 55         CloseHandle(hDrive);
 56     }else printf("Error:[%d] (%s)\n",GetLastError(),pszDevPath);
 57  
 58     return bRC;    
 59 }
 60  
 61  
 62 int GetDeviceDescription(LPCTSTR pszDrive, TCHAR* pszDesc, const size_t szDescSize)
 63 {
 64     TCHAR acDevName[MAX_PATH];
 65     //  QueryDosDevice(pszDrive,acDevName,MAX_PATH);
 66     //  printf("Test: %s\n",acDevName);
 67     _stprintf(acDevName,"\\\\.\\%s", pszDrive);
 68     DWORD dwNumOfInterest = -1;;
 69     GetDeviceNumber(acDevName,dwNumOfInterest);
 70     //printf("DeviceNumber:[%d]\n",dwNumOfInterest);
 71     GUID* guid = (GUID*)&GUID_DEVINTERFACE_DISK;
 72     // Get device interface info set handle
 73     // for all devices attached to system
 74     HDEVINFO hDevInfo = SetupDiGetClassDevs(guid, NULL, NULL,
 75         DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
 76     if (hDevInfo == INVALID_HANDLE_VALUE)  {
 77         return 0;
 78     }
 79  
 80     // Retrieve a context structure for a device interface
 81     // of a device information set.
 82     DWORD dwIndex = 0;
 83     BOOL bRet = FALSE;
 84  
 85     BYTE Buf[1024];
 86     PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd =
 87         (PSP_DEVICE_INTERFACE_DETAIL_DATA)Buf;
 88     SP_DEVICE_INTERFACE_DATA         spdid;
 89     SP_DEVINFO_DATA                  spdd;
 90     DWORD                            dwSize;
 91     spdid.cbSize = sizeof(spdid);
 92     while ( true )  {
 93         bRet = SetupDiEnumDeviceInterfaces(hDevInfo, NULL,
 94             guid, dwIndex, &spdid);
 95  
 96         if (!bRet) {
 97             break;
 98         }
 99  
100         dwSize = 0;
101         SetupDiGetDeviceInterfaceDetail(hDevInfo,
102             &spdid, NULL, 0, &dwSize, NULL);
103  
104         if ( dwSize!=0 && dwSize<=sizeof(Buf) ) {
105             pspdidd->cbSize = sizeof(*pspdidd); // 5 Bytes!
106  
107  
108             ZeroMemory((PVOID)&spdd, sizeof(spdd));
109             spdd.cbSize = sizeof(spdd);
110  
111             long res =
112                 SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, pspdidd,
113                 dwSize, &dwSize,
114                 &spdd);
115  
116             printf("Result:[%s]\n",pspdidd->DevicePath); // <--------- here!
117  
118             DWORD dwType;
119             LPTSTR buffer = NULL;
120             DWORD buffersize = 0;
121  
122             while (!SetupDiGetDeviceRegistryProperty(
123                 hDevInfo,
124                 &spdd,
125                 SPDRP_FRIENDLYNAME,
126                 &dwType,
127                 (PBYTE)buffer,
128                 buffersize,
129                 &buffersize))
130             {
131                 if (GetLastError() == 
132                     ERROR_INSUFFICIENT_BUFFER)
133                 {
134                     // Change the buffer size.
135                     if (buffer) LocalFree(buffer);
136                     // Double the size to avoid problems on 
137                     // W2k MBCS systems per KB 888609. 
138                     buffer =(LPTSTR) LocalAlloc(LPTR,buffersize * 2);
139                 }
140                 else
141                 {
142                     // Insert error handling here.
143                     break;
144                 }
145             }
146  
147             DWORD dwNum = 0;;
148             GetDeviceNumber(pspdidd->DevicePath,dwNum);
149             //printf("DeviceNumber:[%d]\n",dwNum);
150  
151             //if (dwNumOfInterest == dwNum) _stprintf(pszDesc,_T("%s"),buffer);
152             if (dwNumOfInterest == dwNum) _stprintf(pszDesc,_T("%s"),pspdidd->DevicePath);
153  
154             //printf("Result:[%s]\n",buffer);     
155             if (buffer) LocalFree(buffer); 
156  
157         }
158         dwIndex++;
159     }
160     SetupDiDestroyDeviceInfoList(hDevInfo);
161     return 0;
162 }
163  
164 void main (int argc, char** argv) {
165     TCHAR acDesc[1024] = "<unknown>";
166     TCHAR* pszDrive;
167     if (2 != argc) {
168  
169         printf("Usage: usbdev.exe <drive letter> (ex.: ‘usbdev f:‘)\n");
170     }
171     pszDrive = *(argv + 1);
172     GetDeviceDescription(pszDrive, acDesc, 1024);
173     _tprintf("\nDescription for %s\n\n\t%s\n", pszDrive, acDesc);
174     system("pause");
175 }<br><br>

 

 
分类: C/C++

C++获取设备 PID,VID 信息

标签:device   code   als   style   tail   turned   upd   syntax   max   

原文地址:https://www.cnblogs.com/acmexyz/p/11656127.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!