hidd_getserialnumberstring (hid)
Last changed: -216.133.126.74

.

Retrieves the serial number string from a connected device.

C# Signature:

[DllImport("hid.dll", SetLastError=true)]
static extern bool HidD_GetSerialNumberString(IntPtr HidDeviceObject, byte[] Buffer, Int32 BufferLength);

VB Signature:

Declare Function HidD_GetSerialNumberString Lib "hid.dll" (TODO) As TODO

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

The string that is returned is unicode and therefore needs to be converted for most use cases.

Sample Code:

string devicePath = "<your device path>";
// Open the device
deviceHandle = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);

string sn = "";
byte[] buffer = new byte[128];
// attempt to read the serial number string
if (HidD_GetSerialNumberString(HIDDeviceHandle, buffer, buffer.Length))
{
     // convert from unicode to the default encoding
     sn = Encoding.Default.GetString(Encoding.Convert(Encoding.Unicode, Encoding.Default, buffer));
}
// Trim the string down by removing any '\0' characters
sn = sn.Remove(sn.IndexOf('\0'));

Documentation