Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get list of COM Ports in Win32

How to get list of COM Ports in Win32

Scheduled Pinned Locked Moved C / C++ / MFC
comjsontutorial
15 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pandit84
    wrote on last edited by
    #1

    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

    A M S L 4 Replies Last reply
    0
    • P pandit84

      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

      A Offline
      A Offline
      App_
      wrote on last edited by
      #2

      Have a look there

      1 Reply Last reply
      0
      • P pandit84

        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

        M Offline
        M Offline
        Madhu Nair 0
        wrote on last edited by
        #3

        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.

        A J 2 Replies Last reply
        0
        • P pandit84

          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

          S Offline
          S Offline
          Software_Developer
          wrote on last edited by
          #4

          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);
          }

          }

          Richard Andrew x64R 1 Reply Last reply
          0
          • M Madhu Nair 0

            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.

            A Offline
            A Offline
            Albert Holguin
            wrote on last edited by
            #5

            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...

            M 1 Reply Last reply
            0
            • A Albert Holguin

              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...

              M Offline
              M Offline
              Madhu Nair 0
              wrote on last edited by
              #6

              @Albert,thanks!

              A 1 Reply Last reply
              0
              • S Software_Developer

                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);
                }

                }

                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #7

                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.

                P A S 3 Replies Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  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.

                  P Offline
                  P Offline
                  Peter_in_2780
                  wrote on last edited by
                  #8

                  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.

                  A 1 Reply Last reply
                  0
                  • P pandit84

                    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

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    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

                    P 1 Reply Last reply
                    0
                    • L Lost User

                      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

                      P Offline
                      P Offline
                      pandit84
                      wrote on last edited by
                      #10

                      Thanks a lot to All, I have successfully enumerated registry and got list of COM ports available . If we have any virtual port software which can create multiple ports, then also we can enumerate registry and read those port number.

                      1 Reply Last reply
                      0
                      • M Madhu Nair 0

                        @Albert,thanks!

                        A Offline
                        A Offline
                        Albert Holguin
                        wrote on last edited by
                        #11

                        Thank you for contributing... :) :thumbsup:

                        1 Reply Last reply
                        0
                        • P Peter_in_2780

                          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.

                          A Offline
                          A Offline
                          Albert Holguin
                          wrote on last edited by
                          #12

                          Good info... I hate having to deal with all these "run as admin" issues... :mad:

                          1 Reply Last reply
                          0
                          • Richard Andrew x64R Richard Andrew x64

                            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.

                            A Offline
                            A Offline
                            Albert Holguin
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • Richard Andrew x64R Richard Andrew x64

                              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.

                              S Offline
                              S Offline
                              Software_Developer
                              wrote on last edited by
                              #14

                              umm... yea ;)

                              1 Reply Last reply
                              0
                              • M Madhu Nair 0

                                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.

                                J Offline
                                J Offline
                                Jonathan Davies
                                wrote on last edited by
                                #15

                                Have a look at Device Information

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups