我正在使用 Delphi XE2 并尝试将我们的 usb comms dll 升级到 64 位。我们正在使用 JVCL SetupAPI 和 Hid 单元。一切都使用 32 位编译器完美运行,并且可以看到我连接的 HID 设备。我切换到 64 位,我再也看不到我知道已连接的任何 HID 设备。
我遇到过有人提到需要针对 64 位以不同方式调整某些数据结构的大小(请参阅https://forums.embarcadero.com/thread.jspa?messageID=408473#408473),这有所帮助,但我现在正式难住了。
目前我的代码正在返回从 SetupDiGetDeviceInterfaceDetail 函数读取的 0 个字节。注释掉的 SizeOf() 适用于 32 位,但不适用于 64 位。
任何帮助将非常感激。
repeat
TmpDeviceInterfaceData.cbSize := SizeOf(TSPDeviceInterfaceData);
TmpDeviceInterfaceData.cbSize := 32; // SizeOf(TmpDeviceInterfaceData);
TmpSuccess := SetupDiEnumDeviceInterfaces(TmpDevInfo, nil, TmpDevHidGuid, TmpDevn, TmpDeviceInterfaceData);
if TmpSuccess then
begin
TmpDevData.cbSize := 32; //SizeOf(TmpDevData);
showmessage(inttostr(tmpdevdata.cbsize));
TmpBytesReturned := 0;
SetupDiGetDeviceInterfaceDetail(TmpDevInfo, @TmpDeviceInterfaceData, nil, 0, TmpBytesReturned, @TmpDevData);
showmessage('bytes returned = ' + inttostr(TmpBytesReturned));
if (TmpBytesReturned <> 0) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
begin
// showmessage('hello');
TmpFunctionClassDeviceData := AllocMem(TmpBytesReturned);
TmpFunctionClassDeviceData.cbSize := sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
TmpFunctionClassDeviceData.cbSize := 8;
// showmessage(inttostr(TmpFunctionClassDeviceData.cbSize));
if SetupDiGetDeviceInterfaceDetail(TmpDevInfo, @TmpDeviceInterfaceData, TmpFunctionClassDeviceData, TmpBytesReturned, TmpBytesReturned, @TmpDevData) then
begin
// showmessage('here');
try
begin
//try to obtain PID and VID information about the HID devices
TmpDeviceHandle := CreateFile(@TmpFunctionClassDeviceData.DevicePath,
GENERIC_READ OR GENERIC_WRITE,
FILE_SHARE_READ OR FILE_SHARE_WRITE,
NIL, OPEN_EXISTING, 0 , 0);
TmpAttributes.Size := Sizeof(TmpAttributes);
HidD_GetAttributes(TmpDeviceHandle, TmpAttributes);
If (vid = TmpAttributes.VendorID) then
begin
PIDlistStr := PIDlistStr + Inttostr(TmpAttributes.ProductID) + ',';
end ;
if TmpDeviceHandle <> INVALID_HANDLE_VALUE then
begin
CloseHandle(TmpDeviceHandle);
TmpAttributes.ProductID := 0;
TmpAttributes.VendorID := 0;
end;
TmpDeviceHandle := INVALID_HANDLE_VALUE;
end
except
// ignore device if unreadable
end;
Inc(TmpDevn);
end
else
showmessage('error in SetupDiGetDeviceInterfaceDetails');
FreeMem(TmpFunctionClassDeviceData);
end;
end;
until not TmpSuccess;