Results 1 to 6 of 6

Thread: Retrieving the numbers of comports

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    Denmark
    Posts
    96

    Retrieving the numbers of comports

    Hi All.

    Is there a way to retrieve the numbers of Comports there is on a PC in a windows environment ??.

    Best regards from.

    Chris101
    Best Regards from.

    Chris101

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Yup! You can call on the EnumPorts API call. This will list all of the ports such as LPT printer ports, com ports etc on the local pc.

    There's a sample over at www.allapi.net too!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    Denmark
    Posts
    96
    Isn't there another way ?, this finds ALL the ports on the PC (not only the com ports) and more, by more i mean that it says that my PC has com1 - 4 but it has only com 1 - 2 in the real world.

    Regards from.

    Chris101
    Best Regards from.

    Chris101

  4. #4
    Addicted Member darrenl's Avatar
    Join Date
    Jul 2000
    Location
    Portsmouth, UK
    Posts
    148
    Ahh... the old QueryDosDevice Solution...

    Copy this code into a blank Form

    Code:
    Private Declare Function QueryDosDevice Lib "kernel32" Alias "QueryDosDeviceA" (ByVal lpDeviceName As String, ByVal lpTargetPath As String, ByVal ucchMax As Long) As Long
    
    
    Sub Form_Load()
    
    Dim lngCount   As Long
    Dim s          As String
    Dim Ret        As Long
    Dim PortCount  As Long
    
          For lngCount = 1 To 100
             s = String$(255, Chr$(0))
             Ret = QueryDosDevice("COM" & CStr(lngCount), s, Len(s))
             s = Mid$(s, 1, InStr(s, Chr$(0)) - 1)
             
             If s <> "" Then
                Debug.Print "Found >COM" & cstr(lngCount)
                PortCount = PortCount + 1
             End If
          Next      
          MsgBox("Found " & cstr(PortCount) & " Com Port(s)")
    
    End Sub

    You will notice that the Debug.Print tells you exactly what Ports are around...

    Good Luck..
    Dazzer

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    Denmark
    Posts
    96
    Hi Dazzer.

    This is exactly what I was looking for, do you know if this also will work on XP ??.

    Best regards from.

    Chris101

  6. #6
    Addicted Member darrenl's Avatar
    Join Date
    Jul 2000
    Location
    Portsmouth, UK
    Posts
    148
    Yes.
    Dazzer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width