//Is it the desired device? MyDeviceDetected = FALSE; if (Attributes.VendorID == VendorID) { if (Attributes.ProductID == ProductID) { //Both the Product and Vendor IDs match. //MyDeviceDetected = TRUE; //Get the device's capablities. GetDeviceCapabilities(); ReadHandle=CreateFile \ (detailData->DevicePath, \ GENERIC_READ|GENERIC_WRITE, \ FILE_SHARE_READ|FILE_SHARE_WRITE, \ NULL, \ OPEN_EXISTING, \ 0, \ NULL);
BOOL test=HidD_GetManufacturerString(ReadHandle,ManufcturerStringBuffer, 26); if(CompareStr(ManufcturerStringBuffer)==true) { InitBuffer(); MyDeviceDetected=true; } else { CloseHandle(DeviceHandle); MyDeviceDetected = false; } } //if (Attributes.ProductID == ProductID) else //The Product ID doesn't match. CloseHandle(DeviceHandle); } //if (Attributes.VendorID == VendorID) else //The Vendor ID doesn't match. CloseHandle(DeviceHandle); //Free the memory used by the detailData structure (no longer needed). free(detailData); } //if (Result != 0) else //SetupDiEnumDeviceInterfaces returned 0, so there are no more devices to check. LastDevice=TRUE; //If we haven't found the device yet, and haven't tried every available device, //try the next one. MemberIndex = MemberIndex + 1; } //do while ((LastDevice == FALSE) && (MyDeviceDetected == FALSE)); SetupDiDestroyDeviceInfoList(hDevInfo); // DisplayData("SetupDiDestroyDeviceInfoList"); if(ManufcturerStringBuffer) { delete[]ManufcturerStringBuffer; } return MyDeviceDetected; }
用SetupAPI枚举USB设备和端口,得到需要设备名称,然后用CreateFile打开驱动程序,用readfile writefile就可以实现USB通讯了,另外驱动程序中可能会带一些设备控制码,这个要去问写驱动程序的人了
{
HIDD_ATTRIBUTES Attributes;
SP_DEVICE_INTERFACE_DATA devInfoData;
LONG Result;
int MemberIndex = 0;
PSP_DEVICE_INTERFACE_DETAIL_DATA detailData;
bool LastDevice = FALSE;
bool MyDeviceDetected;
PWORD ManufcturerStringBuffer;
ManufcturerStringBuffer= new WORD[26]; //得到HID类的GUID
HidD_GetHidGuid(&HidGuid);
//返回包含一个特定类的所有设备的设备信息组
hDevInfo=SetupDiGetClassDevs \
(&HidGuid, \
NULL, \
NULL, \
DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
devInfoData.cbSize = sizeof(devInfoData);
do
{
MyDeviceDetected=FALSE; //返回关于设备信息组中一个设备的信息
Result=SetupDiEnumDeviceInterfaces \
(hDevInfo, \
0, \
&HidGuid, \
MemberIndex, \
&devInfoData); if (Result != 0)
{
//返回一个设备路径名(得到Length)
Result = SetupDiGetDeviceInterfaceDetail \
(hDevInfo, \
&devInfoData, \
NULL, \
0, \
&Length, \
NULL); detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); //返回一个设备路径名(得到detailData)
Result = SetupDiGetDeviceInterfaceDetail \
(hDevInfo, \
&devInfoData, \
detailData, \
Length, \
&Required, \
NULL); //打开一个设备
DeviceHandle=CreateFile \
(detailData->DevicePath, \
GENERIC_READ|GENERIC_WRITE, \
FILE_SHARE_READ|FILE_SHARE_WRITE, \
NULL, \
OPEN_EXISTING, \
0, \
NULL); //返回销售商ID,产品ID和版本
Result = HidD_GetAttributes \
(DeviceHandle, \
&Attributes);
// DisplayLastError("HidD_GetAttributes: ");
//Is it the desired device?
MyDeviceDetected = FALSE;
if (Attributes.VendorID == VendorID)
{
if (Attributes.ProductID == ProductID)
{
//Both the Product and Vendor IDs match.
//MyDeviceDetected = TRUE;
//Get the device's capablities.
GetDeviceCapabilities();
ReadHandle=CreateFile \
(detailData->DevicePath, \
GENERIC_READ|GENERIC_WRITE, \
FILE_SHARE_READ|FILE_SHARE_WRITE, \
NULL, \
OPEN_EXISTING, \
0, \
NULL);
BOOL test=HidD_GetManufacturerString(ReadHandle,ManufcturerStringBuffer, 26); if(CompareStr(ManufcturerStringBuffer)==true)
{
InitBuffer();
MyDeviceDetected=true;
}
else
{
CloseHandle(DeviceHandle);
MyDeviceDetected = false;
} } //if (Attributes.ProductID == ProductID)
else
//The Product ID doesn't match.
CloseHandle(DeviceHandle);
} //if (Attributes.VendorID == VendorID)
else
//The Vendor ID doesn't match.
CloseHandle(DeviceHandle); //Free the memory used by the detailData structure (no longer needed).
free(detailData);
} //if (Result != 0)
else
//SetupDiEnumDeviceInterfaces returned 0, so there are no more devices to check.
LastDevice=TRUE; //If we haven't found the device yet, and haven't tried every available device,
//try the next one.
MemberIndex = MemberIndex + 1; } //do
while ((LastDevice == FALSE) && (MyDeviceDetected == FALSE)); SetupDiDestroyDeviceInfoList(hDevInfo);
// DisplayData("SetupDiDestroyDeviceInfoList");
if(ManufcturerStringBuffer)
{
delete[]ManufcturerStringBuffer;
}
return MyDeviceDetected;
}
unresolved external symbol "unsigned char __stdcall HidD_GetAttributes(void *,struct _HIDD_ATTRIBUTES *)" (?HidD_GetAttributes@@YGEPAXPAU_HIDD_ATTRIBUTES@@@Z)
USBPortDlg.obj : error LNK2001: unresolved external symbol "void __stdcall HidD_GetHidGuid(struct _GUID *)" (?HidD_GetHidGuid@@YGXPAU_GUID@@@Z)
你也可以直接把HID.LIB添加到project中