-
September 30th, 2008, 02:21 AM
#1
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
-
February 1st, 2009, 10:07 PM
#2
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
-
February 2nd, 2009, 03:19 AM
#3
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
-
February 2nd, 2009, 05:32 AM
#4
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.
-
February 2nd, 2009, 06:58 AM
#5
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
-
February 2nd, 2009, 07:04 AM
#6
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.
-
June 16th, 2009, 09:02 PM
#7
Re: UnauthorizedAccessException when accessing Virtual COM port
Originally Posted by MontgomeryBurns
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.
-
November 6th, 2009, 01:56 PM
#8
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?
-
November 6th, 2009, 04:21 PM
#9
Re: UnauthorizedAccessException when accessing Virtual COM port
Originally Posted by denizkartan
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
-
December 23rd, 2009, 06:03 PM
#10
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#?
-
February 23rd, 2010, 12:16 PM
#11
Re: UnauthorizedAccessException when accessing Virtual COM port
Mr. burns' solution to add the config file to the project worked ! thank you.
-
February 23rd, 2010, 12:33 PM
#12
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 ?
-
September 30th, 2010, 04:56 AM
#13
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
-
September 30th, 2010, 11:58 AM
#14
Re: UnauthorizedAccessException when accessing Virtual COM port
Originally Posted by AlecJames
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|