keri_syml 2013年06月03日
开发USB HID的源码,调用SetupDiGetDeviceInterfaceDetail出错?
调用第一次SetupDiGetDeviceInterfaceDetail出错,返回122.这没有问题.可是为何第二次调用怎么还是出现1784-提供给请求操作的用户缓冲区无效呢.
我网上查阅的大量例子,发现我的程序是没有问题的啊,请高手帮忙看一下呢


private: // User declarations

DWORD ActualBytesRead;
DWORD BytesRead;
HIDP_CAPS Capabilities;
DWORD cbBytesRead;
PSP_DEVICE_INTERFACE_DETAIL_DATA detailData;
HANDLE DeviceHandle;
DWORD dwError;
HANDLE hEventObject;
HANDLE hDevInfo;
GUID HidGuid;
OVERLAPPED HIDOverlapped;
ULONG Length;
DWORD RequiredSize; //add
LPOVERLAPPED lpOverLap;
bool MyDeviceDetected;
AnsiString MyDevicePathName;
DWORD NumberOfBytesRead;
HANDLE ReadHandle;
DWORD ReportType;
ULONG Required;
AnsiString ValueToDisplay;
HANDLE WriteHandle;
char InputReport[256];
char OutputReport[256];

boolean TUSBHID::HidUsb_Conn(int VendorID,int ProductID)
{
HIDD_ATTRIBUTES Attributes;
// DWORD DeviceUsage;
SP_DEVICE_INTERFACE_DATA devInfoData;
bool LastDevice = FALSE;
int MemberIndex = 0;
LONG Result;


char Buffer[256];

memset(Buffer,0,sizeof(Buffer));
memset(ManufacturerString,0,sizeof(ManufacturerString));
memset(ProductString,0,sizeof(ProductString));

Length = 0;
detailData = NULL;
DeviceHandle=NULL;

//获取HID GUID
HidD_GetHidGuid(&HidGuid);
//获取指定类的设备
hDevInfo=SetupDiGetClassDevs
(&HidGuid,
NULL,
NULL,
DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);

devInfoData.cbSize = sizeof(devInfoData);

MemberIndex = 0;
LastDevice = FALSE;

do
{
Result=SetupDiEnumDeviceInterfaces
(hDevInfo,
0,
&HidGuid,
MemberIndex,
&devInfoData);

if (Result != 0)
{
Result = SetupDiGetDeviceInterfaceDetail
(hDevInfo,
&devInfoData,
NULL,
0,
&RequiredSize,//&Length,
NULL);
if (!Result) {
//得到错误代码
DWORD dwError = GetLastError();
*/
}

detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize);//Length);

detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
Result = SetupDiGetDeviceInterfaceDetail
(hDevInfo,
&devInfoData,
detailData,
RequiredSize,//Length,
&Required,
NULL);
if (!Result) {
DWORD dwError = GetLastError();
}

//创建设备对象
DeviceHandle=CreateFile
(detailData->DevicePath,
0,
FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
0,
NULL);

Attributes.Size = sizeof(Attributes);

//获取指定对象的属性
Result = HidD_GetAttributes
(DeviceHandle,
&Attributes);

MyDeviceDetected = FALSE;

//根据VIP,PID判断是否为所需设备
if (Attributes.VendorID == VendorID)
{
if (Attributes.ProductID == ProductID)
{
MyDeviceDetected = TRUE;
MyDevicePathName = detailData->DevicePath;
=

}

else
CloseHandle(DeviceHandle);
}

else
CloseHandle(DeviceHandle);

free(detailData);

}
else
LastDevice=TRUE;

MemberIndex = MemberIndex + 1;

}

while ((LastDevice == FALSE) && (MyDeviceDetected == FALSE));

SetupDiDestroyDeviceInfoList(hDevInfo);

return MyDeviceDetected;

}
...全文
439 点赞 收藏 4
写回复
4 条回复

还没有回复,快来抢沙发~

发动态
发帖子
VCL组件使用和开发
创建于2007-08-02

438

社区成员

1.3w+

社区内容

C++ Builder VCL组件使用和开发
社区公告
暂无公告