Solved

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

Posted on 2004-10-15
3
306 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
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
tdyck12345 earned 500 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now