views:

268

answers:

1

I am writing a .NET application that can communicate with HID devices. I want to be able to retrieve and parse the Manufactures String from the device firmware. I know that in the hid.dll there is a method called HidD_GetManufacturerString. MSDN describes this method as follows:

 BOOLEAN   HidD_GetManufacturerString(
     IN HANDLE  HidDeviceObject,
     OUT PVOID  Buffer,
     IN ULONG  BufferLength
     );

The wrapper i am using for this method looks like this...

[DllImport("hid.dll", SetLastError = true)]
        internal static extern Boolean HidD_GetManufacturerString(
            SafeFileHandle hFile,
            Byte[] SerialNumber,
            Int32 SerianNumberLength);

I need to know two things. First, how do I know what size to initialize the SerialNumber buffer to? When I do operations like GetFeatureReport I know what size to make the buffer because I can retrieve the max Feature Report Length from the device Attributes using HidD_GetAttributes(); Unfortunatly, this method doesn't give me any info of the length of the Manufacturer string, serial number string, product name string, etc.

My second question is, what is the correct way to parse the byte array that is returned into a string? I tried using

System.Text.Encoding.ASCII.GetString(MfrBuffer)

but I get a strange result. For a product manufacturered by Dell my string looks like this "D\0e\0l\0l\0\0\0\0\0..." the "\0" continue on to the end of the buffer that I passed. How do I parse this to "Dell"?

+1  A: 

It looks like "Dell" is coming back as a unicode string, (you can tell by the \0 between each character). so you would use.

System.Text.Encoding.Unicode.GetString(MfrBuffer)

As for the buffer length, the documentation for this function says

The maximum possible number of characters in an embedded string is device specific. For USB devices, the maximum string length is 126 wide characters (not including the terminating NULL character).

So the buffer needs to be (126+1) * 2 bytes in size.

Then you can use TrimEnd("\0".ToCharArray()) to remove the extra trailing \0s

John Knoeller
Great. So how do I trim off all the zero's for devices that don't use the entire 126 wide chars?
Jordan S
Looks like this did it... raw.TrimEnd("\0".ToCharArray());
Jordan S