Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I check for a free COM port during an installation.

Posted on 2004-10-15
3
Medium Priority
?
376 Views
Last Modified: 2013-11-25
I have a project that currently writes a regkey to use COM Port 1 for an external item.  Is there a way to determine if a COM Port is in use and to set the registry to use the next free port?

Thanks!
0
Comment
Question by:dangerwill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
tdyck12345 earned 2000 total points
ID: 12321971
Create a console application with the following code, and run the application during the install

<DllImport("kernel32.dll", SetlastError:=True)> Private Shared Function CreateFile(<MarshalAs(UnmanagedType.LPStr)> ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer
End Function

<DllImport("kernel32.dll", SetlastError:=True)> Private Shared Function CloseHandle(ByVal hObject As Integer) As Integer
End Function

Private Const GENERIC_READ As Integer = &H80000000
Private Const GENERIC_WRITE As Integer = &H40000000
Private Const FILE_OPEN_EXISTING As Integer = 3
Private Const FILE_FLAG_OVERLAPPED As Integer = &H40000000

Public Sub Main()
      for count as integer = 0 to 10
            if COMAvailable(count) then
                  ' write the value to the registry
            end if
      next
End Sub

Public Function COMAvailable(ByVal COMNum As Integer) As Boolean
        Dim hCOM As Long
        Dim ret As Long

        'try to open the COM port
        hCOM = CreateFile("\\.\COM" & COMNum, GENERIC_READ Or GENERIC_WRITE, 0, 0, FILE_OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0)
        If hCOM = -1 Then
            Return False
        Else
            'close the COM port
            ret = CloseHandle(hCOM)
            Return True
        End If
End Function


Troy
0
 

Author Comment

by:dangerwill
ID: 12321981
Thanks a bunch.  I'll try this out and let you know if it works.
0
 

Author Comment

by:dangerwill
ID: 12324050
Worked like a charm!  Thank you sooo much!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Progress

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question