コード例 #1
0
        protected void ReadThread()
        {
            //        FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttrUnused = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();
            IntPtr overlapEvent = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, "");

            FileIOApiDeclarations.OVERLAPPED overlapped = new FileIOApiDeclarations.OVERLAPPED();

            overlapped.Offset       = 0; //IntPtr.Zero;
            overlapped.OffsetHigh   = 0;
            overlapped.hEvent       = (Int32)overlapEvent;
            overlapped.Internal     = 0; //IntPtr.Zero;
            overlapped.InternalHigh = 0; //IntPtr.Zero;
            if (inputReportSize == 0)
            {
                errCodeR  = 302;
                errCodeRE = 302;
                return;
            }
            errCodeR  = 0;
            errCodeRE = 0;

            byte[]   buffer = new byte[inputReportSize];
            GCHandle gch    = GCHandle.Alloc(buffer, GCHandleType.Pinned); //onur March 2009 - pinning is reuired

            while (readThreadActive)
            {
                int dataRead = 0;

                if (0 == FileIOApiDeclarations.ReadFile(readFileHandle, gch.AddrOfPinnedObject(), inputReportSize, ref dataRead, ref overlapped))     //ref readFileBuffer[0]
                {
                    int result = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                    if (result != FileIOApiDeclarations.ERROR_IO_PENDING)     //|| result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED)
                    {
                        errCodeR  = 307;
                        errCodeRE = 307;
                        goto EXit;
                    }
                    else     //if (result != .ERROR_IO_PENDING)
                    {
                        // gch.Free(); //onur
                        while (readThreadActive)
                        {
                            result = FileIOApiDeclarations.WaitForSingleObject(overlapEvent, 50);
                            if (FileIOApiDeclarations.WAIT_OBJECT_0 == result)
                            {
                                if (0 == FileIOApiDeclarations.GetOverlappedResult(readFileHandle, ref overlapped, ref dataRead, 0))
                                {
                                    result = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                                    if (result == FileIOApiDeclarations.ERROR_INVALID_HANDLE || result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED)
                                    {
                                        errCodeR  = 307;
                                        errCodeRE = 307;
                                        goto EXit;
                                    }
                                }
                                // buffer[0] = 89;
                                goto ReadCompleted;
                            }
                        } //while
                    }     //if (result != .ERROR_IO_PENDING)...else
                    continue;
                }
                //buffer[0] = 90;
ReadCompleted:
                if (dataRead != inputReportSize)
                {
                    errCodeR = 310; errCodeRE = 310; goto EXit;
                }

                if (suppressDuplicateReports)
                {
                    int r = readRing.putIfDiff(buffer);
                    if (r == 0)
                    {
                        FileIOApiDeclarations.SetEvent(readEvent);
                    }
                }
                else
                {
                    readRing.put(buffer);
                    FileIOApiDeclarations.SetEvent(readEvent);
                }
            }    //while

EXit:
            FileIOApiDeclarations.CancelIo(readFileHandle);
            readFileHandle = null;
            gch.Free();     //onur
            return;
        }
コード例 #2
0
        }        //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);
            }
        }
コード例 #3
0
//-------------------------------------------------------------------------------------------
        /// <summary>
        /// Write Thread
        /// </summary>
        protected void WriteThread()
        {
            //   FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttrUnused = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();
            IntPtr overlapEvent = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, "");

            FileIOApiDeclarations.OVERLAPPED overlapped = new FileIOApiDeclarations.OVERLAPPED();

            overlapped.Offset       = 0; //IntPtr.Zero;
            overlapped.OffsetHigh   = 0;
            overlapped.hEvent       = (Int32)overlapEvent;
            overlapped.Internal     = 0; //IntPtr.Zero;
            overlapped.InternalHigh = 0; //IntPtr.Zero;
            if (outputReportSize == 0)
            {
                return;
            }

            byte[]   buffer = new byte[outputReportSize];
            GCHandle wgch   = GCHandle.Alloc(buffer, GCHandleType.Pinned); //onur March 2009 - pinning is reuired

            int byteCount = 0;;

            //  int loopCount = 0;

            errCodeW  = 0;
            errCodeWE = 0;
            while (writeThreadActive)
            {
                if (writeRing == null)
                {
                    errCodeW = 407; errCodeWE = 407; goto Error;
                }
                while (writeRing.get((byte[])buffer) == 0)
                {
                    if (0 == FileIOApiDeclarations.WriteFile(
                            writeFileHandle,
                            wgch.AddrOfPinnedObject(),
                            outputReportSize,
                            ref byteCount,
                            ref overlapped))
                    {
                        int result = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                        if (result != FileIOApiDeclarations.ERROR_IO_PENDING)
                        // if ((result == FileIOApiDeclarations.ERROR_INVALID_HANDLE) ||
                        //     (result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED))
                        {
                            if (result == 87)
                            {
                                errCodeW  = 412;
                                errCodeWE = 412;
                                goto Error;
                            }
                            errCodeW  = 408;
                            errCodeWE = 408;
                            goto Error;
                        }   //if (result ==
                        else
                        {
                            // loopCount = 0;
                            // while (writeThreadActive)
                            // {

                            result = FileIOApiDeclarations.WaitForSingleObject(overlapEvent, 1000);
                            if (result == FileIOApiDeclarations.WAIT_OBJECT_0)
                            {
                                // errCodeWE=1400+loopCount;
                                goto WriteCompleted;
                            }
                            //  loopCount++;
                            //  if (loopCount > 10)
                            // {
                            errCodeW  = 411;
                            errCodeWE = 411;

                            goto Error;

                            //  }

                            /*    if (0 == FileIOApiDeclarations.GetOverlappedResult(readFileHandle, ref overlapped, ref byteCount, 0))
                             *  {
                             *      result = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                             *      // if (result == ERROR_INVALID_HANDLE || result == ERROR_DEVICE_NOT_CONNECTED)
                             *      if (result != FileIOApiDeclarations.ERROR_IO_INCOMPLETE)
                             *      {
                             *          errCodeW = 409;
                             *          errCodeWE = 409;
                             *
                             *          goto Error;
                             *      }//if(result == ERROR_INVALID_HANDLE
                             *  }//if(!GetOverlappedResult*/
                            //  }//while
                            //   goto Error;
                        }   //else if(result==ERROR_IO_PENDING){
                            // continue;
                    }
                    else
                    {
                        if ((long)byteCount != outputReportSize)
                        {
                            errCodeW  = 410;
                            errCodeWE = 410;
                        }
                        //iface->errCodeWE=1399;
                    }
                    WriteCompleted :;
                }   //while(get==
                FileIOApiDeclarations.WaitForSingleObject(writeEvent, 100);
                FileIOApiDeclarations.ResetEvent(writeEvent);
                // System.Threading.Thread.Sleep(100);
            }
Error:
            wgch.Free(); //onur

            return;
        }