Pukaai
Pukaai

Reputation: 377

HidLibrary, Get Serial Number from USB device

I am using the following toolbox: HidLibrary

Because I am beginner I just like to know how to get Serial Number from USB device.

Inside .dll is defined:

public bool ReadSerialNumber(out byte[] data)
        {
            data = new byte[64];
            IntPtr hidHandle = IntPtr.Zero;
            bool success = false;
            try
            {
                if (IsOpen)
                    hidHandle = Handle;
                else
                    hidHandle = OpenDeviceIO(_devicePath, NativeMethods.ACCESS_NONE);

                success = NativeMethods.HidD_GetSerialNumberString(hidHandle, ref data[0], data.Length);
            }
            catch (Exception exception)
            {
                throw new Exception(string.Format("Error accessing HID device '{0}'.", _devicePath), exception);
            }
            finally
            {
                if (hidHandle != IntPtr.Zero && hidHandle != Handle)
                    CloseDeviceIO(hidHandle);
            }

            return success;
        }

How to call the method to get the SN of device? I try but without success. I did not yet works with "out byte[]". What is meaning behind "out byte"?

Please for help!

Upvotes: 2

Views: 2112

Answers (1)

Daniel Kelley
Daniel Kelley

Reputation: 7737

To call the method you can use the following:

var hidDevice = ...;
byte[] data;

var result = hidDevice.ReadSerialNumber(out data);

I assume result will be true if the method succeeded, and false if it failed. If it succeeded I'd expect the data variable to be populated with the data you need.

Also, if you are unsure of what out means in this context see the MSDN page on the topic.

Upvotes: 2

Related Questions