Exemple #1
0
            internal void PrepareForOverlappedTransfer(ref kernel32.OVERLAPPED hidOverlapped, ref IntPtr eventObject)
            {
                // Purpose    : Creates an event object for the overlapped structure used with
                //            : ReadFile.
                //            ; Called before the first call to ReadFile.

                kernel32.SECURITY_ATTRIBUTES Security = new kernel32.SECURITY_ATTRIBUTES();

                try
                {
                    // Values for the SECURITY_ATTRIBUTES structure:
                    Security.lpSecurityDescriptor = 0;
                    Security.bInheritHandle       = System.Convert.ToInt32(true);
                    Security.nLength = Marshal.SizeOf(Security);

                    // ***
                    // API function: CreateEvent
                    // Purpose: Creates an event object for the overlapped structure used with ReadFile.
                    // Accepts:
                    // A security attributes structure.
                    // Manual Reset = False (The system automatically resets the state to nonsignaled
                    // after a waiting thread has been released.)
                    // Initial state = True (signaled)
                    // An event object name (optional)
                    // Returns: a handle to the event object
                    // ***

                    eventObject = kernel32.CreateEvent(ref Security, System.Convert.ToInt32(false), System.Convert.ToInt32(true), "");

                    Debug.WriteLine(Debugging.ResultOfAPICall("CreateEvent"));
                    Debug.WriteLine("");

                    // Set the members of the overlapped structure.
                    hidOverlapped.Offset       = 0;
                    hidOverlapped.OffsetHigh   = 0;
                    hidOverlapped.hEvent       = eventObject;
                    ReadyForOverlappedTransfer = true;
                }
                catch (Exception ex)
                {
                    HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
                }
            }
Exemple #2
0
            protected override void ProtectedRead(IntPtr readHandle, IntPtr hidHandle, IntPtr writeHandle, ref bool myDeviceDetected, ref byte[] inputReportBuffer, ref bool success)
            {
                // Purpose    : reads an Input report from the device using interrupt transfers.

                // Accepts    : readHandle - the handle for reading from the device.
                //              hidHandle - the handle for other device communications.
                //              myDeviceDetected - tells whether the device is currently attached.
                //              readBuffer - contains the requested report.
                //              success - read success

                IntPtr EventObject = IntPtr.Zero;

                kernel32.OVERLAPPED HIDOverlapped = new kernel32.OVERLAPPED();
                int  NumberOfBytesRead            = 0;
                long Result = 0;

                try
                {
                    // If it's the first attempt to read, set up the overlapped structure for ReadFile.
                    if (!ReadyForOverlappedTransfer)
                    {
                        PrepareForOverlappedTransfer(ref HIDOverlapped, ref EventObject);
                    }

                    // ***
                    // API function: ReadFile
                    // Purpose: Attempts to read an Input report from the device.
                    // Accepts:
                    // A device handle returned by CreateFile
                    // (for overlapped I/O, CreateFile must have been called with FILE_FLAG_OVERLAPPED),
                    // A pointer to a buffer for storing the report.
                    // The Input report length in bytes returned by HidP_GetCaps,
                    // A pointer to a variable that will hold the number of bytes read.
                    // An overlapped structure whose hEvent member is set to an event object.
                    // Returns: the report in ReadBuffer.
                    // The overlapped call returns immediately, even if the data hasn't been received yet.
                    // To read multiple reports with one ReadFile, increase the size of ReadBuffer
                    // and use NumberOfBytesRead to determine how many reports were returned.
                    // Use a larger buffer if the application can't keep up with reading each report
                    // individually.
                    // ***
                    Debug.Write("input report length = " + inputReportBuffer.Length);

                    Result = kernel32.ReadFile(readHandle, ref inputReportBuffer[0], inputReportBuffer.Length, ref NumberOfBytesRead, ref HIDOverlapped);

                    Debug.WriteLine(Debugging.ResultOfAPICall("ReadFile"));
                    Debug.WriteLine("");
                    Debug.WriteLine("waiting for ReadFile");

                    // API function: WaitForSingleObject
                    // Purpose: waits for at least one report or a timeout.
                    // Used with overlapped ReadFile.
                    // Accepts:
                    // An event object created with CreateEvent
                    // A timeout value in milliseconds.
                    // Returns: A result code.

                    Result = kernel32.WaitForSingleObject(EventObject, 3000);
                    Debug.WriteLine(Debugging.ResultOfAPICall("WaitForSingleObject"));
                    Debug.WriteLine("");

                    // Find out if ReadFile completed or timeout.
                    switch (Result)
                    {
                    case kernel32.WAIT_OBJECT_0:
                        // ReadFile has completed
                        Debug.WriteLine("");
                        success = true;
                        Debug.WriteLine("ReadFile completed successfully.");
                        break;

                    case kernel32.WAIT_TIMEOUT:
                        // Cancel the operation on timeout
                        CancelTransfer(readHandle, hidHandle);
                        Debug.WriteLine("Readfile timeout");
                        Debug.WriteLine("");
                        success          = false;
                        myDeviceDetected = false;
                        break;

                    default:
                        // Cancel the operation on other error.
                        CancelTransfer(readHandle, hidHandle);
                        Debug.WriteLine("");
                        Debug.WriteLine("Readfile undefined error");
                        success          = false;
                        myDeviceDetected = false;
                        break;
                    }

                    success = (Result == 0) ? true : false;
                }
                catch (Exception ex)
                {
                    HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
                }
            }