delphi - 在 64 位应用程序中使用 SetupAPI 枚举 USB HID 设备

标签 delphi 64-bit usb hid jvcl

我正在使用 Delphi XE2 并尝试将我们的 USB 通讯 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;

最佳答案

现在的更改在JVCL中,请使用最新的SVN内容。

基本上,需要修复 SetupApi,以便它使用 x64 中的“填充符”进行对齐。

这已经过测试并且在这里运行良好。

关于delphi - 在 64 位应用程序中使用 SetupAPI 枚举 USB HID 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8049889/

相关文章:

delphi - IdTCPServerExecute 运行但未接收数据

installation - 安装程序问题: "Program Files" or "Program Files (x86)"?

linux - SUSE Linux上安装oracle11g-x64-ent检查错误较多

java - Android 设备的 USB 检测代码崩溃

android - 如何使本地 USB 端口与 Azure 虚拟机配合使用

delphi - 将setter添加到后代类的属性中

delphi - Delphi中如何向TRichEdit插入图片?

linux - 通过 ttyACM 传输二进制数据

delphi - 如何将方法作为回调传递给 Windows API 调用(后续)?

c++ - 先前运行正常时 QApplication 构造函数中的永久 sigabrt