CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Springfield
    Posts
    190

    UnauthorizedAccessException when accessing Virtual COM port

    Hi,
    My application opens a virtual COM port to access an USB device. (I'm using CDC ACM Port Driver by Thesycon GmbH).
    My app opens the port and closes it correctly, if I disconnect the USB cable from the device before calling SerialPort.Close(), then the UnauthorizedAccessException is thrown. Notice that it's not the Close() that throws the exception. It's a separate thread (which is not created from my code) that throws it.
    This is what the IDE's output window shows:
    Code:
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\Temp\WindowsFormsApplication3\WindowsFormsApplication3\bin\Debug\WindowsFormsApplication3.vshost.exe'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Data.DataSetExtensions\3.5.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Deployment\2.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll'
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll'
    The thread 0xfb4 has exited with code 0 (0x0).
    The thread 0xfbc has exited with code 0 (0x0).
    'WindowsFormsApplication3.vshost.exe' (Managed): Loaded 'C:\Temp\WindowsFormsApplication3\WindowsFormsApplication3\bin\Debug\WindowsFormsApplication3.exe', Symbols loaded.
    >>>>> System.IO.Ports.SerialPort.Open() returned without any error
    The thread 0xeec has exited with code 0 (0x0).
    >>>>> System.IO.Ports.SerialPort.Close() returned without any error
    The thread 0x814 has exited with code 0 (0x0).
    The thread 0x888 has exited with code 0 (0x0).
    A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.dll
    An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.dll
    
    Additional information: Access to the port is denied.
    
    The program '[0xF50] WindowsFormsApplication3.vshost.exe: Managed' has exited with code -532459699 (0xe0434f4d).
    Is there a way to prevent this exception to be thrown?
    Last edited by MontgomeryBurns; September 30th, 2008 at 02:25 AM.
    Mr. Burns

  2. #2
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: UnauthorizedAccessException when accessing Virtual COM port

    Ive got the exact same problem. Its occuring in a Win32 component when SetCommMask is called (which runs Async and is where the exception is coming from) and then WaitCommEvent is called. I wrote a small Win32 serial port class in C++ that acts right on the Windows API and didnt called either the SetCommMask or the WaitCommEvent functions and when I called ReadFile to read data from the buffer it would throw an exception that I could catch.

    I created a wrapper class for the C++ class in C# but I cannot get it to act reliably. My stack pointers and stuff keep getting moved because I am forced to use "unsafe" code.

    I guess thats the only REAL way to get around it. As far as I can tell its a bug (a NASTY one albeit) in the .NET Framework.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  3. #3
    Join Date
    Jul 2003
    Location
    Springfield
    Posts
    190

    Re: UnauthorizedAccessException when accessing Virtual COM port

    I solved my problem adding a config file to my Visual Studio C# project. My app.config file is this:
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    	<runtime>
    		<legacyUnhandledExceptionPolicy enabled="true"/>
    	</runtime>
    </configuration>
    as explained here:
    http://connect.microsoft.com/VisualS...edbackID=98348
    Mr. Burns

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: UnauthorizedAccessException when accessing Virtual COM port

    Or even better, I'd suggest trapping the exception (try/catch) and handling it. Microsoft (in the link posted by MontgomeryBurns) say that they thought it better to throw an exception to inform the client application that something was wrong - and I agree with them.

    If I were writing an application using a USB port I'd want it to know if someone pulled the plug out half way through operation...

    Also

    Code:
    My stack pointers and stuff keep getting moved because I am forced to use "unsafe" code.
    You're never FORCED to use unsafe code. You can p/invoke using IntPtr-s and pin arrays manually using GCHandle.Alloc(..., GCHandleType.Pinned). If you're using asynchronous operation you should then handle the completed event to GCHandle.Free the pinned pointer once the asynchronous write has finished.

    Darwen.
    Last edited by darwen; February 2nd, 2009 at 05:36 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Jul 2003
    Location
    Springfield
    Posts
    190

    Re: UnauthorizedAccessException when accessing Virtual COM port

    Or even better, I'd suggest trapping the exception (try/catch) and handling it.
    That's what I'd prefer, either. But you cannot trap the exception since it is thrown by a thread created somewhere else of my code. From the link:
    exception gets thrown on the event loop thread
    Mr. Burns

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: UnauthorizedAccessException when accessing Virtual COM port

    What's the 'event loop thread' ?

    If you want exceptions in a worker thread to be translated back to the thread which originally started the worker thread you have 2 options :

    (1) Trap them yourself using try...catch and implement a mechanism to pass it back to the main thread.
    (2) Use asynchronous delegates (i.e. BeginInvoke of a delegate). The exception is trapped by the framework on the thread that executes the delegate and automatically re-thrown in response to an EndInvoke on whichever thread EndInvoke is called on.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Jun 2009
    Posts
    1

    Thumbs up Re: UnauthorizedAccessException when accessing Virtual COM port

    Quote Originally Posted by MontgomeryBurns View Post
    I solved my problem adding a config file to my Visual Studio C# project. My app.config file is this:
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    	<runtime>
    		<legacyUnhandledExceptionPolicy enabled="true"/>
    	</runtime>
    </configuration>
    as explained here:
    http://connect.microsoft.com/VisualS...edbackID=98348

    Thanks to everybody for sharing your knowledge. MontgomeryBurns's solution fixed my problem. I wrote a GUI for a USB device that creates a virtual serial port. The GUI can detect the presence of the device and connect or reconnect automatically multiple times, but would raise the aformentioned exception upon exit.

  8. #8
    Join Date
    Nov 2008
    Posts
    15

    Re: UnauthorizedAccessException when accessing Virtual COM port

    I have a similar problem:
    My application connects to port, then I unplug the cable. Then I plug the cable again and reconnect to the port. Everything goes fine for a while (I can close and dispose SerialPort object after the unplug and can reconnect with the same object after replugging the cable. Even funnier I even can read port and write to it after replugging!) but after some time UnauthorizedAccessException saying "Access to the port is denied" is thrown.
    I tried setting legacyUnhandledExceptionPolicy to true in the application configuration file but it didn't solve my problem.
    Any suggestions?

  9. #9
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: UnauthorizedAccessException when accessing Virtual COM port

    Quote Originally Posted by denizkartan View Post
    I have a similar problem:
    My application connects to port, then I unplug the cable. Then I plug the cable again and reconnect to the port. Everything goes fine for a while (I can close and dispose SerialPort object after the unplug and can reconnect with the same object after replugging the cable. Even funnier I even can read port and write to it after replugging!) but after some time UnauthorizedAccessException saying "Access to the port is denied" is thrown.
    I tried setting legacyUnhandledExceptionPolicy to true in the application configuration file but it didn't solve my problem.
    Any suggestions?
    Its an exception that's thrown from within the .NET Framework and you cant handle it and keep the application running. I fixed it by using some interop with the kernel32 library. Research how to open and read/write serial ports in Win32 C++ and then call the functions from C# code using some interop.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  10. #10
    Join Date
    Sep 2008
    Posts
    2

    Re: UnauthorizedAccessException when accessing Virtual COM port

    Better yet, one guy said use c++ to open the com ports. I know nothing about interops. Does this mean you can call a C++ class from C#?

  11. #11
    Join Date
    Feb 2010
    Posts
    3

    Re: UnauthorizedAccessException when accessing Virtual COM port

    Mr. burns' solution to add the config file to the project worked ! thank you.

  12. #12
    Join Date
    Feb 2010
    Posts
    3

    Re: UnauthorizedAccessException when accessing Virtual COM port

    maybe i was a little too hasty posting that last post ! ..
    now the exception is thrown when my app exits....

    Does anyone have a solution for this ?

  13. #13
    Join Date
    Mar 2002
    Location
    Portsmouth, UK
    Posts
    121

    Re: UnauthorizedAccessException when accessing Virtual COM port

    Has anyone found a solution for this without going to c++ to access the serial port?
    Thanks

    A

  14. #14
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: UnauthorizedAccessException when accessing Virtual COM port

    Quote Originally Posted by AlecJames View Post
    Has anyone found a solution for this without going to c++ to access the serial port?
    Thanks

    A
    Nope... even a year later I am still using a wrapper class named ComPort which makes "unsafe" API calls from my C# code to prevent this error from occurring. I wonder why the heck MS hasn't fixed this issue... even in .NET 4.0.
    Last edited by RaleTheBlade; September 30th, 2010 at 12:03 PM.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured