Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

HidD_GetSerialNumberString (hid)
 
.

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'));

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));

}

// Clear out the '\0' characters

sn = sn.Remove(sn.IndexOf('\0'));

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions