-2

I am having problems getting HidD_GetSerialNumberString to simply give me the serialnumber of some devices. On some devices it works fine, but on others all I get is "ÿÿÿ" as the serialnumber. I am suspecting it has something to do with Delphi 2009 and it's unicode, but I simply cannot get my head around it. Is there a solution to this?

Edit (declaration) :

type
THidD_GetSerialNumberString = function(HidDeviceObject: THandle; Buffer: PWideChar; BufferLength: Integer): LongBool; stdcall;

var
HidD_GetSerialNumberString: THidD_GetSerialNumberString;

//in the LoadHid-function
@HidD_GetSerialNumberString := GetModuleSymbolEx(HidLib, 'HidD_GetSerialNumberString', Result);

Usage :

var
  test : PWideChar;
...

GetMem(test, 512);
if HidD_GetSerialNumberString(HidFileHandle, Test, 512) then
  FSerialNumber := test; //FSerialNumber is WideString
FreeMem(test);
Lars Bargmann
  • 127
  • 1
  • 7

1 Answers1

0

Your code is fine in all versions of Delphi. Although clearly it involves a UTF-16 to ANSI conversion when compiled by a pre-Unicode Delphi. However, since you use Delphi 2009 that is not an issue.

There are no problems regarding Unicode conversions. The HidD_GetSerialNumberString function fills out the buffer that you supply with a null-terminated UTF-16 string. And since you declared test to be exactly that, Delphi will generate code necessary to copy to a Delphi string variable.

Is it possible that HidD_GetSerialNumberString is returning false and so FSerialNumber is not initialized properly? Other than that, the only conclusions that one can make are that:

  • HidD_GetSerialNumberString is lying to you when it returns True.
  • The serial number returned by calling HidD_GetSerialNumberString really is ÿÿÿ.
David Heffernan
  • 572,264
  • 40
  • 974
  • 1,389
  • I don't understand that comment. Do you mean that the answer to my final sentence is no? Do you mean that `HidD_GetSerialNumberString` always returns `True` or that `FSerialNumber` is always initialized. – David Heffernan Jun 17 '13 at 11:01
  • Oh, yes, I meant my code is actually checking for true/false result, but I simplified the code here to make it easier to read. – Lars Bargmann Jun 17 '13 at 11:02
  • Anyway, if your call to `HidD_GetSerialNumberString` returns `True`, then the device's serial number really is what your code says it is, at least according to `HidD_GetSerialNumberString`. In other words, if there is an error, it's not in the code in the question – David Heffernan Jun 17 '13 at 11:06
  • That was what I was afraid of. Thanks for your help. – Lars Bargmann Jun 17 '13 at 11:08
  • Are you looking for more help on this? – David Heffernan Jun 17 '13 at 11:51
  • It would be lovely if someone had some knowledge to share on this, yes. – Lars Bargmann Jun 17 '13 at 12:21
  • Ask yourself how we could provide more. Your code is clearly correct. Your expectations from us seem somewhat high. – David Heffernan Jun 17 '13 at 12:23
  • Well, StackExchange has proven, time and time again, that some *really* bright minds are present. I am simply hoping for someone to come along and recognize this particular situation, and share some insight. – Lars Bargmann Jun 17 '13 at 12:28
  • ...also, though my code may be correct, I might do something else wrong or neglect to do something that is necessary for this to work porperly... – Lars Bargmann Jun 17 '13 at 12:29
  • Well, I'm very sorry that I've not been able to help you and provide that insight. It would seem that you need to ask another different question asking specifically for that. – David Heffernan Jun 17 '13 at 12:30