PORTS Forum

Ports and Interfaces => USB => Topic started by: DavidClarke on June 10, 2019, 07:02:16 pm

Title: Question about one of your code samples
Post by: DavidClarke 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 );     
Title: Re: Question about one of your code samples
Post by: Jan Axelson 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.
Title: Re: Question about one of your code samples
Post by: DavidClarke on June 12, 2019, 04:59:40 pm
Thanks!