Today I researched how to implement in C# a method that forces a COM port to close when it is open by another process. This was found to be a complex task that involves using undocumented Win32 API functions. I didn’t finish it because I found another way to workaround the problem but the idea is the following:
1) Enumarate all Win32 processes.
2) In each process enumerate all its handles and find the handle to be closed (e.g. \Device\Serial0).
3) Close the handle.
The first task is trivial. We can use System.Diagnostics.Process.GetProcesses(). The second is possible with the Windows NT undocumented function NtQuerySystemInformation. The third task is simple. Once we have the process ID (pid) and the handle of the open COM port, we can close it with the following CloseHandleEx function:
Posted by nakov as blog at 6:09 PM EEST
Comments Off
I am working on an ActiveX control that allows using a device for scanning personal ID cards in a Web application (based on ASP.NET). The card scanning device (called RTE reader) is accessed using a native DLL that communicates with the device through the COM port by using a proprietary protocol. It is pretty complicated to get this working in all Web browsers so we conviced the customer to use Internet Explorer only.
The typical way to implement this functionality in Internet Explorer is to write an ActiveX control. I created the ActiveX control in C# using .NET Framework 2.0, Windows Forms and few native DLLs. How to create an ActiveX in C# in another interesting story, but once I got ready with the ActiveX control, I needed to write an installation program that changes the security policy of Internet Explorer to ensure the control is allowed to run without security restrictions. The control uses native DLL calls so it needs special permissions that can be assigned by the .NET Framework 2.0 Configuration Tool or its console version (caspol.exe).
There are two steps needed to get running an ActiveX control implemented as .NET assembly without security restrictions in Internet Explorer:
- Add the Web site to the “Trusted sites” security zone in Internet Explorer. This allows this Web site to install and run ActiveX controls.
- Assign full trust permission set to the assembly implementing the ActiveX in the .NET Security Policy.
While the first task is less challengeable (we just need to add few registry values), the second requires to run the following command:
The caspol.exe is standard part of .NET Framework 2.0 and hence is available in any machine that has .NET 2.0, 3.0 or 3.5 (because all of the use CRL 2.0). It is typically located here:
The following source code (implemented as Windows Forms desktop application) performs the above mentioned two steps to allow an ActiveX written in C# to run without security limitations in Internet Explorer:
Posted by nakov as blog at 2:12 AM EEST
Comments Off
I needed to enumerate all COM ports in C# and get their name (e.g. COM1, COM2, …) and their description shown in the Windows Device Manager (e.g. “Communications Port”, “RT USB MRTD Reader”). You could find lots of unmanaged code for doing this but I think this C# code could be helpful to people who perform communication through the serial interface in .NET:
Posted by nakov as .net at 6:17 PM EEST
Comments Off