Recent

Author Topic: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows  (Read 795 times)

kovalsky

  • Newbie
  • Posts: 4
Try to use FPC-USB-HID library https://github.com/LongDirtyAnimAlf/FPC-USB-HID
I found that the test examples do not work when compiled to 64-bit under Windows. The problem is that
SizeOf(TSPDeviceInterfaceDetailData) returns 7 instead of 8 in the enumeration HID device method.
This library use modified version of JvSetupApi.pas unit in which a definition for alignment has been added:
Code: Pascal  [Select][+][-]
  1. {$IFDEF FPC}
  2.   {$IFDEF CPU64}
  3.     {$PACKRECORDS 8}
  4.   {$ELSE}
  5.     {$PACKRECORDS 1}
  6.   {$ENDIF CPU64}
  7. {$ELSE}
  8. {$A-}
  9. {$ENDIF FPC}
Why SizeOf() still returns 7 as if the alignment is 1 byte?

jamie

  • Hero Member
  • *****
  • Posts: 5068
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #1 on: December 01, 2022, 02:24:32 pm »
https://learn.microsoft.com/en-us/windows/win32/api/setupapi/ns-setupapi-sp_device_interface_detail_data_a

Looks like it can't be aligned, the first field must contain the current size of the record which includes the field plus a string that is NULL terminated.
The only true wisdom is knowing you know nothing

kovalsky

  • Newbie
  • Posts: 4
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #2 on: December 01, 2022, 10:28:04 pm »
Looks like it can't be aligned, the first field must contain the current size of the record which includes the field plus a string that is NULL terminated.
Does this mean that the SP_DEVICE_INTERFACE_DETAIL_DATA record should be declared withoud packed keyword?
I removed it from record declaration in JvSetupApi.pas and the example started working fine in 64-bit.

jamie

  • Hero Member
  • *****
  • Posts: 5068
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #3 on: December 01, 2022, 10:57:45 pm »
Looking at it, I would say it's a bad translation.

 You should be setting the field size to 0 and then pass that image to the Setup function for the details, it will fail because the record won't be large enough, but the return value will report to you how large it should be.

 With your now known value, you can either give it a record that has more than enough space or allocate one dynamically of the requested record size and then pass that pointer to the setup function.

 That isn't a fixed size record and I suppose if there is enough space in it the function will fill it up to the amount of space it needs, leaving the remainder undefined.

 That is a variable record with a variant tail size and the idea is that you can daisy chain these devices with a NULL separator, and in most cases, two NULLs will indicate the end of the line.


The only true wisdom is knowing you know nothing

avra

  • Hero Member
  • *****
  • Posts: 2431
    • Additional info
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #4 on: December 02, 2022, 09:50:18 am »
I removed it from record declaration in JvSetupApi.pas and the example started working fine in 64-bit.
Could you please report your findings at https://github.com/LongDirtyAnimAlf/FPC-USB-HID/issues so that others can also benefit if that turns out to be the right solution?
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

kovalsky

  • Newbie
  • Posts: 4
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #5 on: December 02, 2022, 11:00:14 pm »
Could you please report your findings at https://github.com/LongDirtyAnimAlf/FPC-USB-HID/issues so that others can also benefit if that turns out to be the right solution?
I have reported this issue.  It looks like the alignment {$PACKRECORDS 8} is not working.

jamie

  • Hero Member
  • *****
  • Posts: 5068
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #6 on: December 02, 2022, 11:38:07 pm »
because that isn't supposed to be aligned within the record which is why Packed is there.
 The problem stims from the issue the record is defined wrong, there shouldn't be any TFiler at the end of that definition.

The Any_Size thing means basically that. Any size.

 The size field indicates the actual size of the record which is most cases is going to be all over the place.

 So when enumerating devices for example, the names are not going to be the same size. What matters here is to ensure the record is large enough and when the API returns you can use the return value to set the true size of the record.

  I've seen far too much hacking like this, and you can get it to work but not always, which is why you see this stuff on the net for free.!\

 Oh well, I'll go back to my key pounding if making BSOD apps.

The only true wisdom is knowing you know nothing

Graham1

  • New Member
  • *
  • Posts: 45
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #7 on: December 03, 2022, 05:48:00 am »
If you don't have any luck getting that one to work, an alternative is https://github.com/dioannidis/HIDAPI.pas.

Works fine for me in Win32, Win64 and Linux but it does also need the HIDAPI.DLL file from https://github.com/libusb/hidapi.
Windows 10 Home 64-bit
Lazarus 2.0.12 / FPC 3.2.0

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1594
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #8 on: December 03, 2022, 07:31:00 am »
The issue should be solved. Using a very brute approach : hardcoding the record size.
However.
AFAIK, using {$PACKRECORDS 8} on a packed record makes no sense. The packed keywork will pack a record regardless of the $packrecords setting. But I stand corrected if wrong.
As I do not want to mess to much with the jvcl sources, I used fixed record sizes for both win32 as well as win64.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1594
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #9 on: December 03, 2022, 08:34:33 am »
Well. My fix did not work for unicode.
So, after extensive testing:
* remove the packed keyword.
* remove the filler.
* add better delphiunicode support.
On my system (Win11, 32/64), it works.
Please test latest sources:
https://github.com/LongDirtyAnimAlf/FPC-USB-HID/commit/59d157812cc58c73fcd62a124926cdc50e94da4c

d.ioannidis

  • Full Member
  • ***
  • Posts: 202
    • Nephelae
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #10 on: December 03, 2022, 10:06:48 am »
Hi,

If you don't have any luck getting that one to work, an alternative is https://github.com/dioannidis/HIDAPI.pas.

if you use Lazarus you can use the Online Package Manager to install the HIDAPI.pas library.

regards,
« Last Edit: December 03, 2022, 10:10:25 am by d.ioannidis »

d.ioannidis

  • Full Member
  • ***
  • Posts: 202
    • Nephelae
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #11 on: December 03, 2022, 10:12:50 am »
Hi,

Works fine for me in Win32, Win64 and Linux but it does also need the HIDAPI.DLL file from https://github.com/libusb/hidapi.

or from the official libusb hidapi releases repo.

regards,

kovalsky

  • Newbie
  • Posts: 4
Re: FPC-USB-HID library doesn't work when compiled to 64-bit under Windows
« Reply #12 on: December 03, 2022, 03:45:20 pm »
Well. My fix did not work for unicode.
So, after extensive testing:
* remove the packed keyword.
* remove the filler.
* add better delphiunicode support.
On my system (Win11, 32/64), it works.
Please test latest sources:
https://github.com/LongDirtyAnimAlf/FPC-USB-HID/commit/59d157812cc58c73fcd62a124926cdc50e94da4c

Many thanks to everyone for the tips and suggestions!
Especially DonAlfredo for the corrections in the library. Now the examples work in 64-bit.

 

TinyPortal © 2005-2018