views:

242

answers:

3

On win32, using winapi, is there anyway to know which comports (from com0 upwards) actually exist as devices?

At the moment I am just attemping to open them all (0 to 9), but I can't figure out the difference of failure between one not existing, and one not simply being available for use because someone else is using it. Both situations seem to return the same last error, so I was wondering if I could list all the comports available on the system.

+1  A: 

I believe you can call QueryDosDevice() and pass null for the first parameter and then parse the results.

jeffamaphone
+1  A: 

Search google for "enumerate com ports". This is an example link.

kgiannakakis
A: 

The name is unfortunate, but "SetupAPI" is the relevant part of the Windows API. Call SetupDiCreateDeviceInfoList once for device interface class GUID_DEVINTERFACE_COMPORT. Then call SetupDiEnumDeviceInfo repeatedly, starting at index 0 until GetLastError()==ERROR_NO_MORE_ITEMS.

MSalters