
怎样获得已连接到计算机的USB设备的列表?
我想在程序中列举出当前已连接到计算机的USB设备的设备名或其他信息,有什么api可以用?或者其他方法?谢谢...
我想在程序中列举出当前已连接到计算机的USB设备的设备名或其他信息,有什么api可以用?或者其他方法?谢谢
展开
3个回答
2010-06-11
展开全部
// DeviceManager.cpp : 定义控制台应用程序的入口点。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <setupapi.h>
#define DIRECTINPUT_VERSION 0x0800
#include <Dinput.h>
extern "C"
{
#include <hidsdi.h>
}
#include <tchar.h>
#pragma comment(lib, "Dinput8.lib")
#pragma comment(lib, "Dinput.lib")
#pragma comment(lib, "Setupapi.lib")
#pragma comment(lib, "hid.lib")
GUID myGuid;
LPDIRECTINPUT8 m_lpDI; // DI8接口指针
LPDIENUMDEVICESCALLBACK DIEnumDevicesCallback(const DIDEVICEINSTANCE* lpddi, VOID* pvRef)
{
*(GUID*) pvRef = lpddi->guidInstance;
return DIENUM_STOP;
}
int main(void)
{
GUID HidGuid;
// 查找本系统中HID类的GUID标识
HidD_GetHidGuid(&HidGuid);
printf("系统中HID类的GUID标识为:%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
HidGuid.Data1,HidGuid.Data2 ,HidGuid.Data3 ,
HidGuid.Data4[0],HidGuid.Data4[1],HidGuid.Data4[2],
HidGuid.Data4[3],HidGuid.Data4[4],HidGuid.Data4[5],
HidGuid.Data4[6],HidGuid.Data4[7]);
// 准备查找符合HID规范的USB设备
HDEVINFO hDevInfo = SetupDiGetClassDevs(
&HidGuid,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("符合HID规范的USB设备发生错误 \n");
return 0;
}
printf("正在查找可用的USB设备。。。。 \n");
DWORD MemberIndex = 0;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
BOOL bSuccess = FALSE;
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
do
{
bSuccess = SetupDiEnumDeviceInterfaces(
hDevInfo,
NULL,
&HidGuid,
MemberIndex,
&DeviceInterfaceData);
if ((!bSuccess) && (GetLastError() == ERROR_NO_MORE_ITEMS))
{
if(MemberIndex == 0)
{
printf("抱歉,未找到可用的USB设备! \n");
}
else
{
printf("没有更多的可用的USB设备! \n");
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
printf("找到了一个USB设备: \n");
// 若找到了一个USB设备,则获取该设备的细节信息
PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData;
DWORD Length = 0;
SetupDiGetDeviceInterfaceDetail(
hDevInfo,
&DeviceInterfaceData,
NULL,
0,
&Length,
NULL);
pDeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); //MUST BE!!!
if (!SetupDiGetDeviceInterfaceDetail(
hDevInfo,
&DeviceInterfaceData,
pDeviceInterfaceDetailData,
Length,
NULL,
NULL))
{
printf("查找路径设备时出错! \n");
}
else
{
printf("设备路径:%s ", pDeviceInterfaceDetailData->DevicePath);
}
//打开设备句柄
HANDLE hDeviceHandle = CreateFile(pDeviceInterfaceDetailData->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDeviceHandle == INVALID_HANDLE_VALUE)
{
printf("打开设备路径出错! \n");
// printf("%d", GetLastError());
}
else
{
HIDD_ATTRIBUTES Attributes;
HidD_GetAttributes(hDeviceHandle,&Attributes);
/**************************************************/
/*
long result;
int ReadBuffer[10];
int flag;
do
{
ReadBuffer[9] = 0;
ReadBuffer[8] = 0;
ReadBuffer[7] = 0;
ReadBuffer[6] = 0;
ReadBuffer[5] = 0;
ReadBuffer[4] = 0;
ReadBuffer[3] = 0;
ReadBuffer[2] = 0;
ReadBuffer[1] = 0;
ReadBuffer[0] = 0;
result = HidD_GetFeature(
hDeviceHandle,
(PVOID)ReadBuffer,
0x09);
// for (int i = 0; i < ReadBuffer[1]; i++)
{
//printf("%d", ReadBuffer[i]);
}
}
//scanf("%d", &flag);
while(1);
*/
HRESULT hr;
HINSTANCE hInstance = GetModuleHandle(NULL) ;//获取实例句柄;
//建立DirectInput接口
if(NULL == m_lpDI)
{
hr = DirectInput8Create(hInstance,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&m_lpDI, //接口取值
NULL);
hr = m_lpDI->EnumDevices(DI8DEVCLASS_GAMECTRL,
(LPDIENUMDEVICESCALLBACK)DIEnumDevicesCallback, //回调函数
&myGuid, //赋值GUID
DIEDFL_ATTACHEDONLY); //扫描安装好的和连接好的设备
}
/**************************************************/
//将有关该设备的标识显示出来
printf("供应商ID :0X%04X ",Attributes.VendorID);
printf("产品ID :0X%04X ",Attributes.ProductID);
printf("产品版本号:0X%04X ",Attributes.VersionNumber);
WCHAR mString[256];
TCHAR Buffer[256];
HidD_GetManufacturerString(hDeviceHandle,mString,sizeof(mString));
if (wcstombs((char *)Buffer,mString,256) == -1) // fail
{
Buffer[0] = NULL;
}
printf("生产商: %s ",Buffer);
HidD_GetProductString(hDeviceHandle,mString,sizeof(mString));
if (wcstombs((char *)Buffer,mString,256) == -1)
{
Buffer[0] = NULL;
}
printf("产品名称: %s ",Buffer);
// 通信:
PHIDP_PREPARSED_DATA pHidpPreparsedData;
HIDP_CAPS hidPCaps;
if (!HidD_GetPreparsedData(hDeviceHandle, &pHidpPreparsedData))
{
printf("获取 HID PREPARED DATA 失败! \n");
return 0;
}
NTSTATUS status = HidP_GetCaps(pHidpPreparsedData,&hidPCaps);
if (status == HIDP_STATUS_SUCCESS)
{
printf("CAP信息如下: \n");
printf(" InputReportByteLength %d ", hidPCaps.InputReportByteLength);
printf(" OutputReportByteLength %d ", hidPCaps.OutputReportByteLength);
}
DWORD nReadBytes = 0;
BYTE *pInputReport = new BYTE[hidPCaps.InputReportByteLength];
memset(pInputReport,0,hidPCaps.InputReportByteLength);
do
{
ReadFile(hDeviceHandle,
pInputReport,
hidPCaps.InputReportByteLength,
&nReadBytes,
NULL);
if (hidPCaps.InputReportByteLength == nReadBytes)
{
for (unsigned int i = 0; i< nReadBytes - 1;i++)
{
printf("%02x-",pInputReport[i]);
}
printf("%02x ",pInputReport[nReadBytes-1]);
}
// if (pInputReport[nReadBytes-2] == 0x20) //break the loop when pressing a specific key
{
// printf(" \n");
break;
}
Sleep(10);
}while (hidPCaps.InputReportByteLength == nReadBytes);
// 释放句柄资源
CloseHandle(hDeviceHandle);
}
MemberIndex++;
}while(bSuccess);
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <setupapi.h>
#define DIRECTINPUT_VERSION 0x0800
#include <Dinput.h>
extern "C"
{
#include <hidsdi.h>
}
#include <tchar.h>
#pragma comment(lib, "Dinput8.lib")
#pragma comment(lib, "Dinput.lib")
#pragma comment(lib, "Setupapi.lib")
#pragma comment(lib, "hid.lib")
GUID myGuid;
LPDIRECTINPUT8 m_lpDI; // DI8接口指针
LPDIENUMDEVICESCALLBACK DIEnumDevicesCallback(const DIDEVICEINSTANCE* lpddi, VOID* pvRef)
{
*(GUID*) pvRef = lpddi->guidInstance;
return DIENUM_STOP;
}
int main(void)
{
GUID HidGuid;
// 查找本系统中HID类的GUID标识
HidD_GetHidGuid(&HidGuid);
printf("系统中HID类的GUID标识为:%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
HidGuid.Data1,HidGuid.Data2 ,HidGuid.Data3 ,
HidGuid.Data4[0],HidGuid.Data4[1],HidGuid.Data4[2],
HidGuid.Data4[3],HidGuid.Data4[4],HidGuid.Data4[5],
HidGuid.Data4[6],HidGuid.Data4[7]);
// 准备查找符合HID规范的USB设备
HDEVINFO hDevInfo = SetupDiGetClassDevs(
&HidGuid,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("符合HID规范的USB设备发生错误 \n");
return 0;
}
printf("正在查找可用的USB设备。。。。 \n");
DWORD MemberIndex = 0;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
BOOL bSuccess = FALSE;
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
do
{
bSuccess = SetupDiEnumDeviceInterfaces(
hDevInfo,
NULL,
&HidGuid,
MemberIndex,
&DeviceInterfaceData);
if ((!bSuccess) && (GetLastError() == ERROR_NO_MORE_ITEMS))
{
if(MemberIndex == 0)
{
printf("抱歉,未找到可用的USB设备! \n");
}
else
{
printf("没有更多的可用的USB设备! \n");
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
printf("找到了一个USB设备: \n");
// 若找到了一个USB设备,则获取该设备的细节信息
PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData;
DWORD Length = 0;
SetupDiGetDeviceInterfaceDetail(
hDevInfo,
&DeviceInterfaceData,
NULL,
0,
&Length,
NULL);
pDeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); //MUST BE!!!
if (!SetupDiGetDeviceInterfaceDetail(
hDevInfo,
&DeviceInterfaceData,
pDeviceInterfaceDetailData,
Length,
NULL,
NULL))
{
printf("查找路径设备时出错! \n");
}
else
{
printf("设备路径:%s ", pDeviceInterfaceDetailData->DevicePath);
}
//打开设备句柄
HANDLE hDeviceHandle = CreateFile(pDeviceInterfaceDetailData->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDeviceHandle == INVALID_HANDLE_VALUE)
{
printf("打开设备路径出错! \n");
// printf("%d", GetLastError());
}
else
{
HIDD_ATTRIBUTES Attributes;
HidD_GetAttributes(hDeviceHandle,&Attributes);
/**************************************************/
/*
long result;
int ReadBuffer[10];
int flag;
do
{
ReadBuffer[9] = 0;
ReadBuffer[8] = 0;
ReadBuffer[7] = 0;
ReadBuffer[6] = 0;
ReadBuffer[5] = 0;
ReadBuffer[4] = 0;
ReadBuffer[3] = 0;
ReadBuffer[2] = 0;
ReadBuffer[1] = 0;
ReadBuffer[0] = 0;
result = HidD_GetFeature(
hDeviceHandle,
(PVOID)ReadBuffer,
0x09);
// for (int i = 0; i < ReadBuffer[1]; i++)
{
//printf("%d", ReadBuffer[i]);
}
}
//scanf("%d", &flag);
while(1);
*/
HRESULT hr;
HINSTANCE hInstance = GetModuleHandle(NULL) ;//获取实例句柄;
//建立DirectInput接口
if(NULL == m_lpDI)
{
hr = DirectInput8Create(hInstance,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&m_lpDI, //接口取值
NULL);
hr = m_lpDI->EnumDevices(DI8DEVCLASS_GAMECTRL,
(LPDIENUMDEVICESCALLBACK)DIEnumDevicesCallback, //回调函数
&myGuid, //赋值GUID
DIEDFL_ATTACHEDONLY); //扫描安装好的和连接好的设备
}
/**************************************************/
//将有关该设备的标识显示出来
printf("供应商ID :0X%04X ",Attributes.VendorID);
printf("产品ID :0X%04X ",Attributes.ProductID);
printf("产品版本号:0X%04X ",Attributes.VersionNumber);
WCHAR mString[256];
TCHAR Buffer[256];
HidD_GetManufacturerString(hDeviceHandle,mString,sizeof(mString));
if (wcstombs((char *)Buffer,mString,256) == -1) // fail
{
Buffer[0] = NULL;
}
printf("生产商: %s ",Buffer);
HidD_GetProductString(hDeviceHandle,mString,sizeof(mString));
if (wcstombs((char *)Buffer,mString,256) == -1)
{
Buffer[0] = NULL;
}
printf("产品名称: %s ",Buffer);
// 通信:
PHIDP_PREPARSED_DATA pHidpPreparsedData;
HIDP_CAPS hidPCaps;
if (!HidD_GetPreparsedData(hDeviceHandle, &pHidpPreparsedData))
{
printf("获取 HID PREPARED DATA 失败! \n");
return 0;
}
NTSTATUS status = HidP_GetCaps(pHidpPreparsedData,&hidPCaps);
if (status == HIDP_STATUS_SUCCESS)
{
printf("CAP信息如下: \n");
printf(" InputReportByteLength %d ", hidPCaps.InputReportByteLength);
printf(" OutputReportByteLength %d ", hidPCaps.OutputReportByteLength);
}
DWORD nReadBytes = 0;
BYTE *pInputReport = new BYTE[hidPCaps.InputReportByteLength];
memset(pInputReport,0,hidPCaps.InputReportByteLength);
do
{
ReadFile(hDeviceHandle,
pInputReport,
hidPCaps.InputReportByteLength,
&nReadBytes,
NULL);
if (hidPCaps.InputReportByteLength == nReadBytes)
{
for (unsigned int i = 0; i< nReadBytes - 1;i++)
{
printf("%02x-",pInputReport[i]);
}
printf("%02x ",pInputReport[nReadBytes-1]);
}
// if (pInputReport[nReadBytes-2] == 0x20) //break the loop when pressing a specific key
{
// printf(" \n");
break;
}
Sleep(10);
}while (hidPCaps.InputReportByteLength == nReadBytes);
// 释放句柄资源
CloseHandle(hDeviceHandle);
}
MemberIndex++;
}while(bSuccess);
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}

2025-01-03 广告
要从电脑的一个串行口接收数据并将其从另一个串行口发送出去,你可以使用以下步骤:1. 确定你要使用的两个串行口。在大多数计算机上,串行口通常被称为COM1、COM2等。确保你了解每个串行口的名称或编号。2. 编写一个程序来读取来自一个串行口的...
点击进入详情页
本回答由迪凯特科技(北京)有限公司_提供
展开全部
HANDLE MyHid::OpenDevice(bool isOverLapped)
{
GUID hidGuid;
HidD_GetHidGuid(&hidGuid);
hDevInfo = SetupDiGetClassDevs(&hidGuid,NULL,NULL,(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
if (hDevInfo == INVALID_HANDLE_VALUE)
{
return INVALID_HANDLE_VALUE;
}
SP_DEVICE_INTERFACE_DATA devInfoData;
devInfoData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
int deviceNo = 0;
SetLastError(NO_ERROR);
while (GetLastError() != ERROR_NO_MORE_ITEMS)
{
if (SetupDiEnumInterfaceDevice (hDevInfo,0,&hidGuid,deviceNo,&devInfoData))
{
ULONG requiredLength = 0;
SetupDiGetInterfaceDeviceDetail(hDevInfo,&devInfoData,NULL,0,&requiredLength,NULL);
PSP_INTERFACE_DEVICE_DETAIL_DATA devDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc (requiredLength);
devDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if(!SetupDiGetInterfaceDeviceDetail(hDevInfo,&devInfoData,devDetail,requiredLength,NULL,NULL))
{
free(devDetail);
SetupDiDestroyDeviceInfoList(hDevInfo);
return INVALID_HANDLE_VALUE;
}
if (isOverLapped)
{
hidHandle = CreateFile(devDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
}
else
{
hidHandle = CreateFile(devDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
}
free(devDetail);
if (hidHandle==INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hDevInfo);
free(devDetail);
return INVALID_HANDLE_VALUE;
}
_HIDD_ATTRIBUTES hidAttributes;
if(!HidD_GetAttributes(hidHandle, &hidAttributes))
{
CloseHandle(hidHandle);
SetupDiDestroyDeviceInfoList(hDevInfo);
return INVALID_HANDLE_VALUE;
}
if (USB_VID == hidAttributes.VendorID&& USB_PID == hidAttributes.ProductID)
{
m_isDevicesOK=true;
break;
}
else
{
CloseHandle(hidHandle);
++deviceNo;
m_isDevicesOK=false;
}
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return hidHandle;
}
#define USB_VID 0x08fe
#define USB_PID 0x0003
//自定义的设备号.
你可以在设备管理器里面找到你自己的需要的 ID 然后 但看实例句柄.
{
GUID hidGuid;
HidD_GetHidGuid(&hidGuid);
hDevInfo = SetupDiGetClassDevs(&hidGuid,NULL,NULL,(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
if (hDevInfo == INVALID_HANDLE_VALUE)
{
return INVALID_HANDLE_VALUE;
}
SP_DEVICE_INTERFACE_DATA devInfoData;
devInfoData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
int deviceNo = 0;
SetLastError(NO_ERROR);
while (GetLastError() != ERROR_NO_MORE_ITEMS)
{
if (SetupDiEnumInterfaceDevice (hDevInfo,0,&hidGuid,deviceNo,&devInfoData))
{
ULONG requiredLength = 0;
SetupDiGetInterfaceDeviceDetail(hDevInfo,&devInfoData,NULL,0,&requiredLength,NULL);
PSP_INTERFACE_DEVICE_DETAIL_DATA devDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc (requiredLength);
devDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if(!SetupDiGetInterfaceDeviceDetail(hDevInfo,&devInfoData,devDetail,requiredLength,NULL,NULL))
{
free(devDetail);
SetupDiDestroyDeviceInfoList(hDevInfo);
return INVALID_HANDLE_VALUE;
}
if (isOverLapped)
{
hidHandle = CreateFile(devDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
}
else
{
hidHandle = CreateFile(devDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
}
free(devDetail);
if (hidHandle==INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hDevInfo);
free(devDetail);
return INVALID_HANDLE_VALUE;
}
_HIDD_ATTRIBUTES hidAttributes;
if(!HidD_GetAttributes(hidHandle, &hidAttributes))
{
CloseHandle(hidHandle);
SetupDiDestroyDeviceInfoList(hDevInfo);
return INVALID_HANDLE_VALUE;
}
if (USB_VID == hidAttributes.VendorID&& USB_PID == hidAttributes.ProductID)
{
m_isDevicesOK=true;
break;
}
else
{
CloseHandle(hidHandle);
++deviceNo;
m_isDevicesOK=false;
}
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return hidHandle;
}
#define USB_VID 0x08fe
#define USB_PID 0x0003
//自定义的设备号.
你可以在设备管理器里面找到你自己的需要的 ID 然后 但看实例句柄.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
HANDLE MyHid::OpenDevice(bool isOverLapped)
{
GUID hidGuid;
HidD_GetHidGuid(&hidGuid);
hDevInfo = SetupDiGetClassDevs(&hidGuid,NULL,NULL,(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
if (hDevInfo == INVALID_HANDLE_VALUE)
{
return INVALID_HANDLE_VALUE;
}
SP_DEVICE_INTERFACE_DATA devInfoData;
devInfoData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
int deviceNo = 0;
SetLastError(NO_ERROR);
while (GetLastError() != ERROR_NO_MORE_ITEMS)
{
if (SetupDiEnumInterfaceDevice (hDevInfo,0,&hidGuid,deviceNo,&devInfoData))
{
ULONG requiredLength = 0;
SetupDiGetInterfaceDeviceDetail(hDevInfo,&devInfoData,NULL,0,&requiredLength,NULL);
PSP_INTERFACE_DEVICE_DETAIL_DATA devDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc (requiredLength);
devDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if(!SetupDiGetInterfaceDeviceDetail(hDevInfo,&devInfoData,devDetail,requiredLength,NULL,NULL))
{
free(devDetail);
SetupDiDestroyDeviceInfoList(hDevInfo);
return INVALID_HANDLE_VALUE;
}
if (isOverLapped)
{
hidHandle = CreateFile(devDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
}
else
{
hidHandle = CreateFile(devDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
}
free(devDetail);
if (hidHandle==INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hDevInfo);
free(devDetail);
return INVALID_HANDLE_VALUE;
}
_HIDD_ATTRIBUTES hidAttributes;
if(!HidD_GetAttributes(hidHandle, &hidAttributes))
{
CloseHandle(hidHandle);
SetupDiDestroyDeviceInfoList(hDevInfo);
return INVALID_HANDLE_VALUE;
}
if (USB_VID == hidAttributes.VendorID&& USB_PID == hidAttributes.ProductID)
{
m_isDevicesOK=true;
break;
}
else
{
CloseHandle(hidHandle);
++deviceNo;
m_isDevicesOK=false;
}
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return hidHandle;
}
#define USB_VID 0x08fe
#define USB_PID 0x0003
//自定义的设备号.
你可以在设备管理器里面找到你自己的需要的 ID 然后 但看实例句柄.
你要进行操作的话 需要DDK 里面的 几个文件.需要的话 我传给你.
{
GUID hidGuid;
HidD_GetHidGuid(&hidGuid);
hDevInfo = SetupDiGetClassDevs(&hidGuid,NULL,NULL,(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
if (hDevInfo == INVALID_HANDLE_VALUE)
{
return INVALID_HANDLE_VALUE;
}
SP_DEVICE_INTERFACE_DATA devInfoData;
devInfoData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
int deviceNo = 0;
SetLastError(NO_ERROR);
while (GetLastError() != ERROR_NO_MORE_ITEMS)
{
if (SetupDiEnumInterfaceDevice (hDevInfo,0,&hidGuid,deviceNo,&devInfoData))
{
ULONG requiredLength = 0;
SetupDiGetInterfaceDeviceDetail(hDevInfo,&devInfoData,NULL,0,&requiredLength,NULL);
PSP_INTERFACE_DEVICE_DETAIL_DATA devDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc (requiredLength);
devDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if(!SetupDiGetInterfaceDeviceDetail(hDevInfo,&devInfoData,devDetail,requiredLength,NULL,NULL))
{
free(devDetail);
SetupDiDestroyDeviceInfoList(hDevInfo);
return INVALID_HANDLE_VALUE;
}
if (isOverLapped)
{
hidHandle = CreateFile(devDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
}
else
{
hidHandle = CreateFile(devDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
}
free(devDetail);
if (hidHandle==INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hDevInfo);
free(devDetail);
return INVALID_HANDLE_VALUE;
}
_HIDD_ATTRIBUTES hidAttributes;
if(!HidD_GetAttributes(hidHandle, &hidAttributes))
{
CloseHandle(hidHandle);
SetupDiDestroyDeviceInfoList(hDevInfo);
return INVALID_HANDLE_VALUE;
}
if (USB_VID == hidAttributes.VendorID&& USB_PID == hidAttributes.ProductID)
{
m_isDevicesOK=true;
break;
}
else
{
CloseHandle(hidHandle);
++deviceNo;
m_isDevicesOK=false;
}
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return hidHandle;
}
#define USB_VID 0x08fe
#define USB_PID 0x0003
//自定义的设备号.
你可以在设备管理器里面找到你自己的需要的 ID 然后 但看实例句柄.
你要进行操作的话 需要DDK 里面的 几个文件.需要的话 我传给你.
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询