Author Topic: Question about one of your code samples  (Read 8877 times)

DavidClarke

  • Member
  • ***
  • Posts: 24
Question about one of your code samples
« on: June 10, 2019, 07:02:16 pm »
Hi
In the code below, why do you need a separate a read and write handle?  I would have thought the overlapped one could do both.
Thanks!

Code: [Select]
//  Get handles to use in requesting Input and Output reports.

                        readHandle = FileIO.CreateFile(myDevicePathName, FileIO.GENERIC_READ, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, FileIO.FILE_FLAG_OVERLAPPED, 0);

                        functionName = "CreateFile, ReadHandle";
                        Debug.WriteLine(MyDebugging.ResultOfAPICall(functionName));
                        Debug.WriteLine( "  Returned handle: " + readHandle.ToString() );
                       
                        if ( readHandle.IsInvalid )
                        {                             
                            exclusiveAccess = true;
                            lstResults.Items.Add( "The device is a system " + hidUsage + "." );
                            lstResults.Items.Add( "Windows 2000 and Windows XP obtain exclusive access to Input and Output reports for this devices." );
                            lstResults.Items.Add( "Applications can access Feature reports only." );
                            ScrollToBottomOfListBox();                             
                        }
                        else
                        {
                            writeHandle = FileIO.CreateFile(myDevicePathName, FileIO.GENERIC_WRITE, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0);

                            functionName = "CreateFile, WriteHandle";
                            Debug.WriteLine(MyDebugging.ResultOfAPICall(functionName));
                            Debug.WriteLine( "  Returned handle: " + writeHandle.ToString() );
                           
                            //  Flush any waiting reports in the input buffer. (optional)
                           
                            MyHid.FlushQueue( readHandle );     

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Question about one of your code samples
« Reply #1 on: June 10, 2019, 08:56:53 pm »
I believe it would work with one handle. I think one reason was to demonstrate both overlapped (read) and non-overlapped (write) communication.

DavidClarke

  • Member
  • ***
  • Posts: 24
Re: Question about one of your code samples
« Reply #2 on: June 12, 2019, 04:59:40 pm »
Thanks!