Retrieves the serial number string from a connected device.
[DllImport("hid.dll", SetLastError=true)]
static extern bool HidD_GetSerialNumberString(IntPtr HidDeviceObject, byte[] Buffer, Int32 BufferLength);
Declare Function HidD_GetSerialNumberString Lib "hid.dll" (TODO) As TODO
None.
Do you know one? Please contribute it!
None.
The string that is returned is unicode and therefore needs to be converted for most use cases.
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'));