protected Int32 SendSausageCommands(ushort[] commandSequence)
        {
            if (outputReportSize != 2 || hidUsagePage != 1 || hidUsage != 6)             // hid page 1, usage 6 is keyboard
            {
                return(1302);
            }

            FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttributes = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();
            securityAttributes.lpSecurityDescriptor = IntPtr.Zero;
            securityAttributes.bInheritHandle       = System.Convert.ToInt32(true);
            securityAttributes.nLength = Marshal.SizeOf(securityAttributes);

            SafeFileHandle hFile = FileIOApiDeclarations.CreateFile(path,
                                                                    FileIOApiDeclarations.GENERIC_WRITE,
                                                                    FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE,
                                                                    IntPtr.Zero,
                                                                    FileIOApiDeclarations.OPEN_EXISTING,
                                                                    0,
                                                                    0);

            if (hFile.IsInvalid)
            {
                return(1301);;
            }
            FileIOApiDeclarations.OVERLAPPED overlapped = new FileIOApiDeclarations.OVERLAPPED();
            overlapped.hEvent = 0; // IntPtr.Zero;
            overlapped.Offset = 0; // ;
            // overlapped.OffsetHigh = 0;
            foreach (ushort command in commandSequence)
            {
                uint cmd           = ((uint)command) << 16;
                uint bytesReturned = 0;

                if (!DeviceManagementApiDeclarations.DeviceIoControl(hFile, (uint)0x000b0008, ref cmd, 4, IntPtr.Zero, 0, ref bytesReturned, ref overlapped))
                {
                    int result = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                    return(result);
                }
            }
            hFile.Close();
            return(0);
        }
        /// <summary>
        /// Enumerates all valid USB devics of the specified Vid.
        /// </summary>
        /// <returns>list of all devices found, ordered by USB port connection</returns>
        public static PIEDevice[] EnumeratePIE(Int32 vid)
        {
            LinkedList <PIEDevice> devices = new LinkedList <PIEDevice>();

            // Get all device paths
            Guid guid = Guid.Empty;

            HidApiDeclarations.HidD_GetHidGuid(ref guid);
            IntPtr deviceInfoSet = DeviceManagementApiDeclarations.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero,
                                                                                       DeviceManagementApiDeclarations.DIGCF_PRESENT
                                                                                       | DeviceManagementApiDeclarations.DIGCF_DEVICEINTERFACE);

            DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DATA();
            deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); //28;

            LinkedList <String> paths = new LinkedList <String>();

            for (int i = 0; 0 != DeviceManagementApiDeclarations.SetupDiEnumDeviceInterfaces(
                     deviceInfoSet, 0, ref guid, i, ref deviceInterfaceData); i++)
            {
                int buffSize = 0;
                DeviceManagementApiDeclarations.SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
                                                                                ref deviceInterfaceData, IntPtr.Zero, 0, ref buffSize, IntPtr.Zero);
                // Use IntPtr to simulate detail data structure
                IntPtr detailBuffer = Marshal.AllocHGlobal(buffSize);
                // Simulate setting cbSize to 4 bytes + one character (seems to be what everyone has always done, even though it makes no sense)
                //onur modified for 64-bit compatibility - March 2009
                if (IntPtr.Size == 8) //64-bit
                {
                    Marshal.WriteInt32(detailBuffer, Marshal.SizeOf(typeof(IntPtr)));
                }
                else //32-bit
                {
                    Marshal.WriteInt32(detailBuffer, 4 + Marshal.SystemDefaultCharSize);
                }

                if (DeviceManagementApiDeclarations.SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
                                                                                    ref deviceInterfaceData, detailBuffer, buffSize, ref buffSize, IntPtr.Zero))
                {
                    // convert buffer (starting past the cbsize variable) to string path
                    paths.AddLast(Marshal.PtrToStringAuto(new IntPtr(detailBuffer.ToInt32() + 4)));
                }
            }
            DeviceManagementApiDeclarations.SetupDiDestroyDeviceInfoList(deviceInfoSet);
            //Security attributes not used anymore - not necessary Onur March 2009
            // Open each device file and test for vid
            FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttributes = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();
            securityAttributes.lpSecurityDescriptor = IntPtr.Zero;
            securityAttributes.bInheritHandle       = System.Convert.ToInt32(true);
            securityAttributes.nLength = Marshal.SizeOf(securityAttributes);

            for (LinkedList <String> .Enumerator en = paths.GetEnumerator(); en.MoveNext();)
            {
                String path = en.Current;

                SafeFileHandle fileHandle = FileIOApiDeclarations.CreateFile(path, FileIOApiDeclarations.GENERIC_WRITE,
                                                                             FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE,
                                                                             IntPtr.Zero, FileIOApiDeclarations.OPEN_EXISTING, 0, 0);
                if (fileHandle.IsInvalid)
                {
                    // Bad handle, try next path
                    continue;
                }
                try
                {
                    HidApiDeclarations.HIDD_ATTRIBUTES hidAttributes = new HidApiDeclarations.HIDD_ATTRIBUTES();
                    hidAttributes.Size = Marshal.SizeOf(hidAttributes);
                    if (0 != HidApiDeclarations.HidD_GetAttributes(fileHandle, ref hidAttributes) &&
                        hidAttributes.VendorID == vid)
                    {
                        // Good attributes and right vid, try to get caps
                        IntPtr pPerparsedData = new IntPtr();
                        if (HidApiDeclarations.HidD_GetPreparsedData(fileHandle, ref pPerparsedData))
                        {
                            HidApiDeclarations.HIDP_CAPS hidCaps = new HidApiDeclarations.HIDP_CAPS();
                            if (0 != HidApiDeclarations.HidP_GetCaps(pPerparsedData, ref hidCaps))
                            {
                                // Got Capabilities, add device to list
                                byte[] Mstring = new byte[128];
                                String ssss    = "";;
                                if (0 != HidApiDeclarations.HidD_GetManufacturerString(fileHandle, ref Mstring[0], 128))
                                {
                                    for (int i = 0; i < 64; i++)
                                    {
                                        byte[] t = new byte[2];
                                        t[0] = Mstring[2 * i];
                                        t[1] = Mstring[2 * i + 1];
                                        if (t[0] == 0)
                                        {
                                            break;
                                        }
                                        ssss += System.Text.Encoding.Unicode.GetString(t);
                                    }
                                }
                                byte[] Pstring = new byte[128];
                                String psss    = "";
                                if (0 != HidApiDeclarations.HidD_GetProductString(fileHandle, ref Pstring[0], 128))
                                {
                                    // Pstring[0] = 0xa0;  Test unicode
                                    // Pstring[1] = 0x03;
                                    for (int i = 0; i < 64; i++)
                                    {
                                        byte[] t = new byte[2];
                                        t[0] = Pstring[2 * i];
                                        t[1] = Pstring[2 * i + 1];
                                        if (t[0] == 0)
                                        {
                                            break;
                                        }
                                        psss += System.Text.Encoding.Unicode.GetString(t);
                                    }
                                    // psss=(System.Text.Encoding.Unicode.GetString(Pstring)).Trim(new char[]{' '});
                                }

                                devices.AddLast(new PIEDevice(path, hidAttributes.VendorID, hidAttributes.ProductID, hidAttributes.VersionNumber,
                                                              hidCaps.Usage, hidCaps.UsagePage, hidCaps.InputReportByteLength, hidCaps.OutputReportByteLength,
                                                              ssss, psss));
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    continue;
                }
                finally
                {
                    fileHandle.Close();
                }
            }
            PIEDevice[] ret = new PIEDevice[devices.Count];
            devices.CopyTo(ret, 0);
            return(ret);
        }
        }        //DataEventThread()

//----------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------
        /// <summary>
        /// Sets connection to the enumerated device.
        /// If inputReportSize greater than zero it generates a read handle.
        /// If outputReportSize greater than zero it generates a write handle.
        /// </summary>
        /// <returns></returns>
        public Int32 SetupInterface()
        {
            int retin  = 0;
            int retout = 0;

            if (connected)
            {
                return(203);
            }
            if (inputReportSize > 0)
            {
                readFileHandle = FileIOApiDeclarations.CreateFile(path, FileIOApiDeclarations.GENERIC_READ,
                                                                  FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE,
                                                                  IntPtr.Zero, FileIOApiDeclarations.OPEN_EXISTING, FileIOApiDeclarations.FILE_FLAG_OVERLAPPED, 0);

                if (readFileHandle.IsInvalid)
                {
                    //readEvent = null;
                    //readFileHandle = null;
                    readRing = null;
                    //CloseInterface();
                    retin = 207;
                    goto outputinit;
                }
                readEvent        = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, "");
                readRing         = new RngBuf2(128, inputReportSize);
                readThreadHandle = new Thread(new ThreadStart(ReadThread));
                readThreadHandle.IsBackground = true;
                readThreadHandle.Name         = "PIEHidReadThread for " + pid;
                readThreadActive = true;
                readThreadHandle.Start();
            }
outputinit:
            if (outputReportSize > 0)
            {
                writeFileHandle = FileIOApiDeclarations.CreateFile(path, FileIOApiDeclarations.GENERIC_WRITE,
                                                                   FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE,
                                                                   IntPtr.Zero, FileIOApiDeclarations.OPEN_EXISTING,
                                                                   FileIOApiDeclarations.FILE_FLAG_OVERLAPPED,
                                                                   0);

                if (writeFileHandle.IsInvalid)
                {
                    // writeEvent = null;
                    // writeFileHandle = null;
                    writeRing = null;
                    //CloseInterface();
                    retout = 208;
                    goto ErrorOut;
                }
                writeEvent        = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, "");
                writeRing         = new RngBuf2(128, outputReportSize);
                writeThreadHandle = new Thread(new ThreadStart(WriteThread));
                writeThreadHandle.IsBackground = true;
                writeThreadHandle.Name         = "PIEHidWriteThread for " + pid;
                writeThreadActive = true;
                writeThreadHandle.Start();
            }
            connected = true;
ErrorOut:
            if ((retin == 0) && (retout == 0))
            {
                return(0);
            }
            if ((retin == 207) && (retout == 208))
            {
                return(209);
            }
            else
            {
                return(retin + retout);
            }
        }