How to get list of COM Ports in Win32
-
Is there any API which gives me list of Seial Ports available on system. I am trying to use QueryDosDevice () but its failed to provide any result . Thanks
-
Is there any API which gives me list of Seial Ports available on system. I am trying to use QueryDosDevice () but its failed to provide any result . Thanks
pandit84 wrote:
Is there any API which gives me list of Seial Ports available on system.
SerialPort.GetPortNames Method[^] is available as a.Net library method. You could also try Another serial port enumerator[^] which uses a different approach to find serial ports by enumerating registry keys. -OR- Enumerating serial ports - W2K style [^]- Enumerating the serial ports using the SetupDi* API provided with Win2K and later
pandit84 wrote:
QueryDosDevice () but its failed to provide any result .
I have also found this support page [^] on why QueryDosDevice fails.
-
Is there any API which gives me list of Seial Ports available on system. I am trying to use QueryDosDevice () but its failed to provide any result . Thanks
The CreateFile function creates or opens a file or I/O device. The following code lists COM Ports in Win32..
#include #include void main(){
char port_name[16];
for (int port_number = 1; port_number < 256; ++port_number)
{
port_number < 10 ?
sprintf(port_name, "COM%d", port_number) :
sprintf(port_name, "\\\\.\\COM%d", port_number);
HANDLE hFile = ::CreateFile(port_name,
GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
std::cout << port_name << std::endl;
else
{
DWORD err = GetLastError();
if (err == ERROR_ACCESS_DENIED ||
err == ERROR_SHARING_VIOLATION)
std::cout << port_name << std::endl;
}
CloseHandle(hFile);
}}
-
pandit84 wrote:
Is there any API which gives me list of Seial Ports available on system.
SerialPort.GetPortNames Method[^] is available as a.Net library method. You could also try Another serial port enumerator[^] which uses a different approach to find serial ports by enumerating registry keys. -OR- Enumerating serial ports - W2K style [^]- Enumerating the serial ports using the SetupDi* API provided with Win2K and later
pandit84 wrote:
QueryDosDevice () but its failed to provide any result .
I have also found this support page [^] on why QueryDosDevice fails.
Good answer overall, but considering this is the "C/C++/MFC" forum, don't think pointing out things in the .Net library are applicable. +5 for the other info though...
-
Good answer overall, but considering this is the "C/C++/MFC" forum, don't think pointing out things in the .Net library are applicable. +5 for the other info though...
@Albert,thanks!
-
The CreateFile function creates or opens a file or I/O device. The following code lists COM Ports in Win32..
#include #include void main(){
char port_name[16];
for (int port_number = 1; port_number < 256; ++port_number)
{
port_number < 10 ?
sprintf(port_name, "COM%d", port_number) :
sprintf(port_name, "\\\\.\\COM%d", port_number);
HANDLE hFile = ::CreateFile(port_name,
GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
std::cout << port_name << std::endl;
else
{
DWORD err = GetLastError();
if (err == ERROR_ACCESS_DENIED ||
err == ERROR_SHARING_VIOLATION)
std::cout << port_name << std::endl;
}
CloseHandle(hFile);
}}
What if a COM port is in use at the time this code is run? It won't get listed.
The difficult we do right away... ...the impossible takes slightly longer.
-
What if a COM port is in use at the time this code is run? It won't get listed.
The difficult we do right away... ...the impossible takes slightly longer.
Also, under Vista (and probably Weven), you need 'run as admin' to use the "\\\\.\\COM11" form. Stoopid bug I ran into when Vista was first released. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994.
-
Is there any API which gives me list of Seial Ports available on system. I am trying to use QueryDosDevice () but its failed to provide any result . Thanks
Hi pandit84, I highly recommend that you have a look at EnumSerialPorts[^] written by MVP PJ Naughter[^]. Its been in development since around 1998 and supports every enumeration type... WMI, registry, setupapi and QueryDosDevice. Best Wishes, -David Delaune
-
Hi pandit84, I highly recommend that you have a look at EnumSerialPorts[^] written by MVP PJ Naughter[^]. Its been in development since around 1998 and supports every enumeration type... WMI, registry, setupapi and QueryDosDevice. Best Wishes, -David Delaune
-
@Albert,thanks!
Thank you for contributing... :) :thumbsup:
-
Also, under Vista (and probably Weven), you need 'run as admin' to use the "\\\\.\\COM11" form. Stoopid bug I ran into when Vista was first released. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994.
Good info... I hate having to deal with all these "run as admin" issues... :mad:
-
What if a COM port is in use at the time this code is run? It won't get listed.
The difficult we do right away... ...the impossible takes slightly longer.
This may not be so bad though... in most programs, if a COM port is already in use, then you can't use it for something else... so it shouldn't be listed anyway. Depends on what the OP needs to list the COM ports for.
-
What if a COM port is in use at the time this code is run? It won't get listed.
The difficult we do right away... ...the impossible takes slightly longer.
umm... yea ;)
-
pandit84 wrote:
Is there any API which gives me list of Seial Ports available on system.
SerialPort.GetPortNames Method[^] is available as a.Net library method. You could also try Another serial port enumerator[^] which uses a different approach to find serial ports by enumerating registry keys. -OR- Enumerating serial ports - W2K style [^]- Enumerating the serial ports using the SetupDi* API provided with Win2K and later
pandit84 wrote:
QueryDosDevice () but its failed to provide any result .
I have also found this support page [^] on why QueryDosDevice fails.
Have a look at Device Information