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


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

    转来的,代码:

    /*
    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");
    }

  • 相关阅读:
    GKB版本与UTF-8版本有什么区别(转载)
    如何与项目开发方对接
    php查询数据集的几种方式(mysql_unbuffered_query()与mysql_query()的区别)
    怎么去执行文件读写 ?
    Python中(集合、元祖、字典)等词汇基本语法
    Python入门~list 相关语法
    Python入门-基础语法实践
    newman + postman 环境搭建
    萌新驾到,请多多关照!
    切片、非空即真
  • 原文地址:https://www.cnblogs.com/imzhstar/p/4121468.html
Copyright © 2020-2023  润新知