資源簡介
HID USB通信的動態(tài)庫,根據(jù)ProductID和VendorID來識別設(shè)備,可以直接調(diào)用使用;
壓縮包包含源代碼,VC++開發(fā),應(yīng)用于Window系統(tǒng)中程序和HID設(shè)備通信;
代碼片段和文件信息
//?HIDUSB.cpp?:?定義?DLL?應(yīng)用程序的入口點。
//
#include?“stdafx.h“
#include?“hidusb.h“
extern?“C“?{
//?Declare?the?C?libraries?used
#include?“setupapi.h“ //?Must?link?in?setupapi.lib
#include?“hidsdi.h“ //?Must?link?in?hid.lib
}
#ifdef?_MANAGED
#pragma?managed(push?off)
#endif
BOOL?APIENTRY?DllMain(?HMODULE?hModule
???????????????????????DWORD??ul_reason_for_call
???????????????????????LPVOID?lpReserved
?)
{
????return?TRUE;
}
BOOL?__stdcall?DLLAPI?SearchHIDDev(unsigned?short?nVendorID?unsigned?short?nProductID?int?*?piDevNum?char?*?acInfo){
struct?_GUID?GUID;
int?iRetErr;
SP_INTERFACE_DEVICE_DATA?DeviceInterfaceData;
HANDLE?PnPHandle=NULLHIDHandle=NULL;
struct?{
DWORD?cbSize;?
char?DevicePath[256];
}?FunctionClassDeviceData;
HIDD_ATTRIBUTES?HIDAttributes;
SECURITY_ATTRIBUTES?SecurityAttributes;
int?Device;
ULONG?BytesReturned;
bool?Success;
*?piDevNum?=?0;
????
//?Initialize?the?GUID?array?and?setup?the?security?attributes?for?Win2000
HidD_GetHidGuid(&GUID);
SecurityAttributes.nLength?=?sizeof(SECURITY_ATTRIBUTES);?
SecurityAttributes.lpSecurityDescriptor?=?NULL;?
SecurityAttributes.bInheritHandle?=?false;?
//?Get?a?handle?for?the?Plug?and?Play?node?and?request?currently?active?devices
PnPHandle?=?SetupDiGetClassDevs(&GUID?NULL?NULL?DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
if?(int(PnPHandle)?==?-1)?{
strcpy(acInfo“Could?not?attach?to?PnP?node.“);//printf(“Could?not?attach?to?PnP?node“);?
iRetErr?=?GetLastError();
return?iRetErr;?
}
//?Lets?look?for?a?maximum?of?22?Devices
for?(Device?=?0;?Device?22;?Device++)?{
//?Initialize?our?data
DeviceInterfaceData.cbSize?=?sizeof(DeviceInterfaceData);
//?Is?there?a?device?at?this?table?entry
Success?=?SetupDiEnumDeviceInterfaces(PnPHandle?NULL?&GUID?Device?&DeviceInterfaceData);
if?(Success)?{
//?There?is?a?device?here?get?it‘s?name
FunctionClassDeviceData.cbSize?=?5;
Success?=?SetupDiGetDeviceInterfaceDetail(PnPHandle?&DeviceInterfaceData?
(PSP_INTERFACE_DEVICE_DETAIL_DATA)&FunctionClassDeviceData?256?&BytesReturned?NULL);
if?(!Success)?{
strcpy(acInfo“Could?not?find?the?system?name?for?this?device.“);
iRetErr?=?GetLastError();
if(PnPHandle)
????????????????SetupDiDestroyDeviceInfoList(PnPHandle);
return?iRetErr;?
}
//?Can?now?open?this?device
HIDHandle?=?CreateFile(FunctionClassDeviceData.DevicePath?GENERIC_READ|GENERIC_WRITE?
FILE_SHARE_READ|FILE_SHARE_WRITE?&SecurityAttributes?OPEN_EXISTING?0?NULL);
if?(HIDHandle?==?INVALID_HANDLE_VALUE)?
sprintf(acInfo“Could?not?open?HID?#%d?Errorcode?=?%d\n“?Device?GetLastError());
else?{
//?Get?the?information?about?this?HID
Success?=?HidD_GetAttributes(HIDHandle?&HIDAttributes);
if?(!Success)?{?
strcpy(acInfo“Could?not?get?HID?attributes.“);?
iRetErr?=?GetLastError();
if(PnPHandle)
????????????????SetupDiDestroyDeviceInfoList(PnPHa
評論
共有 條評論