views:

1158

answers:

2

We have industrial automation application developed in .NET 3.5 framework.

I have to list out the available serial ports in the computer for our user. As part this, if any of the serial port is already opened, I have to show "In Use" status. I tried google but no luck.

Can someone help me please?

Regards,

Krishgy

A: 

There are undocumented APIs to find all the handles that are open in the system, and then you could check which ones are open to serial ports. That's if the user will always be an administrator and you don't care about staying with .Net and documented APIs.

There are documented APIs to find all the COM ports that exist, and then you could try opening all of them. This sounds pretty crude and slow, but it's less evil than the alternative.

Windows programmer
+1  A: 

Try opening the com port with an exclusive lock, if that fails, flag it as open.

Paul Nathan
That's what I do and it works.
Peter Wone