阅读:1502回复:3
usb通讯程序的编译的问题extern "C" { #include "hidsdi.h" #include <setupapi.h> } HANDLE connectToIthUSBHIDDevice (DWORD deviceIndex) { GUID hidGUID; HDEVINFO hardwareDeviceInfoSet; SP_DEVICE_INTERFACE_DATA deviceInterfaceData; PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail; ULONG requiredSize; HANDLE deviceHandle = INVALID_HANDLE_VALUE; DWORD result; //Get the HID GUID value - used as mask to get list of devices HidD_GetHidGuid (&hidGUID); //Get a list of devices matching the criteria (hid interface, present) hardwareDeviceInfoSet = SetupDiGetClassDevs (&hidGUID, NULL, // Define no enumerator (global) NULL, // Define no (DIGCF_PRESENT | // Only Devices present DIGCF_DEVICEINTERFACE)); // Function class devices. deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); //Go through the list and get the interface data result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet, NULL, //infoData, &hidGUID, //interfaceClassGuid, deviceIndex, &deviceInterfaceData); /* Failed to get a device - possibly the index is larger than the number of devices */ if (result == FALSE) { SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet); return INVALID_HANDLE_VALUE; } //Get the details with null values to get the required size of the buffer SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet, &deviceInterfaceData, NULL, //interfaceDetail, 0, //interfaceDetailSize, &requiredSize, 0); //infoData)) //Allocate the buffer deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredSize); deviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); //Fill the buffer with the device details if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet, &deviceInterfaceData, deviceDetail, requiredSize, &requiredSize, NULL)) { SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet); free (deviceDetail); return INVALID_HANDLE_VALUE; } //Open file on the device deviceHandle = CreateFile (deviceDetail->DevicePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, // no SECURITY_ATTRIBUTES structure OPEN_EXISTING, // No special create flags 0, NULL); // No template file SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet); free (deviceDetail); return deviceHandle; } HANDLE connectToUSBHIDDevice (DWORD *vendorID, DWORD *productID, DWORD *versionNumber) { HANDLE deviceHandle = INVALID_HANDLE_VALUE; DWORD index = 0; HIDD_ATTRIBUTES deviceAttributes; BOOL matched = FALSE; while (!matched && (deviceHandle = connectToIthUSBHIDDevice (index)) != INVALID_HANDLE_VALUE) { if (!HidD_GetAttributes (deviceHandle, &deviceAttributes)) return INVALID_HANDLE_VALUE; if ((vendorID == 0 || deviceAttributes.VendorID == *vendorID) && (productID == 0 || deviceAttributes.ProductID == *productID) && (versionNumber == 0 || deviceAttributes.VersionNumber == *versionNumber)) return deviceHandle; /* matched */ CloseHandle (deviceHandle); /* not a match - close and try again */ index++; } return INVALID_HANDLE_VALUE; } void main() { DWORD MemberIndex=0; connectToIthUSBHIDDevice (MemberIndex); } 编译的时候总是出现 --------------------Configuration: usb - Win32 Debug-------------------- Compiling... ff.cpp d:\测试\abc\hidsdi.h(32) : error C2146: syntax error : missing ';' before identifier 'NTSTATUS' d:\测试\abc\hidsdi.h(32) : fatal error C1004: unexpected end of file found Error executing cl.exe. ff.obj - 2 error(s), 0 warning(s) 请高手指点迷精! |
|
沙发#
发布于:2004-07-09 10:04
问题解决了,还有一个问题:
result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet, NULL, //infoData, &hidGUID, //interfaceClassGuid, deviceIndex, &deviceInterfaceData); 中的deviceIndex是什么啊? |
|
板凳#
发布于:2004-07-30 15:10
deviceIndex是指定设备信息集钟的设备接口列表中一个基于0的索引。
当检测到一个新设备时,Windows OS就会到KEY_LOCAL_MACHINE\ControlSet001下去搜索,如果此类设备已经注册,那么就此子键下增加一个子键,这个子键的名称是顺序递增的,如果当前该类设备中最大子键名称为0005,那么新设备的就是0006,0000,0001等就是设备的序列号。如果发现这个设备没有注册,那么OS就会以该设备对应的驱动程序安装文件.inf中的ClassGuid为名称来创建一个键,并将此被检测到的设备的序列号为0,在该键下创建一个子键,并且命名为0000来存储该设备的相关信息,之所以命名为0000,因为该设备是该类设备的第一个。 |
|
地板#
发布于:2007-04-12 21:02
lz的第一个问题我也想知道为什么阿?有谁知道阿?
即: --------------------Configuration: usb - Win32 Debug-------------------- Compiling... ff.cpp d:\测试\abc\hidsdi.h(32) : error C2146: syntax error : missing ';' before identifier 'NTSTATUS' d:\测试\abc\hidsdi.h(32) : fatal error C1004: unexpected end of file found Error executing cl.exe. ff.obj - 2 error(s), 0 warning(s) |
|