How to get GetDefaultCommConfig to work with VB.NET?

  • Thread starter Thread starter Joe HM
  • Start date Start date
J

Joe HM

Hello -

I am trying to detect whether a computer has a COM2 Port. I found the
following non-VB.NET Code on the Internet ...

Private Type DCB
DCBlength As Long
BaudRate As Long
fBitFields As Long
wReserved As Integer
XonLim As Integer
XoffLim As Integer
ByteSize As Byte
Parity As Byte
StopBits As Byte
XonChar As Byte
XoffChar As Byte
ErrorChar As Byte
EofChar As Byte
EvtChar As Byte
wReserved1 As Integer
End Type

Private Type COMMCONFIG
dwSize As Long
wVersion As Integer
wReserved As Integer
dcbx As DCB
dwProviderSubType As Long
dwProviderOffset As Long
dwProviderSize As Long
wcProviderData As Byte
End Type

Private Declare Function GetDefaultCommConfig Lib "kernel32" _
Alias "GetDefaultCommConfigA" (ByVal lpszName _
As String, lpCC As COMMCONFIG, _
lpdwSize As Long) As Long

Public Function ComPortExists(ByVal ComPort As Integer) _
As Boolean
Dim udtComConfig As COMMCONFIG
Dim lUDTSize As Long
Dim lRet As Long

lUDTSize = LenB(udtComConfig)
lRet = GetDefaultCommConfig("COM" + Trim(Str(ComPort)) + _
Chr(0), udtComConfig, lUDTSize)
ComPortExists = lRet <> 0
End Function

How can I get this work in VB.NET. I have tried the following ...

<StructLayout(LayoutKind.Sequential)> Public Structure DCB
Public DCBlength As Int32
Public BaudRate As Int32
Public fBitFields As Int32
Public wReserved As Integer
Public XonLim As Integer
Public XoffLim As Integer
Public ByteSize As Byte
Public Parity As Byte
Public StopBits As Byte
Public XonChar As Byte
Public XoffChar As Byte
Public ErrorChar As Byte
Public EofChar As Byte
Public EvtChar As Byte
Public wReserved1 As Integer
End Structure

<StructLayout(LayoutKind.Sequential)> Public Structure COMMCONFIG
Public dwSize As Int32
Public wVersion As Integer
Public wReserved As Integer
Public dcbx As DCB
Public dwProviderSubType As Int32
Public dwProviderOffset As Int32
Public dwProviderSize As Int32
Public wcProviderData As Byte
End Structure

Declare Function GetDefaultCommConfig Lib "kernel32" _
Alias "GetDefaultCommConfigA" ( _
<MarshalAs(UnmanagedType.LPStr), [In]()> ByVal
lpszName As String, _
ByVal lpCC As COMMCONFIG, _
ByVal lpdwSize As Int32) As Int32

Tester ...

Dim cc As COMMCONFIG
Dim ccsize As Int32

ccsize = Marshal.SizeOf(cc) 'gets the size of COMMCONFIG
structure

Console.WriteLine("COM2 = " & GetDefaultCommConfig("COM2" +
Chr(0), cc, ccsize))

This should be non-0 if the COM2 Ports exists but for whatever reason
it is not working right. I assume that I did something wrong when I
mapped the Types to the Structure.

Any suggestions?

Thansk!
Joe
 
Hello -

I have actually figured out how to get it to work ... with the help of
the Internet and playing with it a little bit ... here we go ...

<StructLayout(LayoutKind.Sequential, Pack:=1)> Private Structure DCB
Public DCBlength As Integer ' = System.Int32
Public BaudRate As Integer
Public Bits1 As Integer
Public wReserved As Short ' = System.Int16
Public XonLim As Short
Public XoffLim As Short
Public ByteSize As Byte
Public Parity As Byte
Public StopBits As Byte
Public XonChar As Char
Public XoffChar As Char
Public ErrorChar As Char
Public EofChar As Char
Public EvtChar As Char
Public wReserved2 As Short
End Structure

<StructLayout(LayoutKind.Sequential, Pack:=8)> Private Structure
COMMCONFIG
Public dwSize As Integer
Public wVersion As Short
Public wReserved As Short
Public dcbx As DCB
Public dwProviderSubType As Integer
Public dwProviderOffset As Integer
Public dwProviderSize As Integer
Public wcProviderData As Short
End Structure

<DllImport("kernel32.dll", SetlastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function GetDefaultCommConfig(ByVal lpszName As String,
ByRef lpCC As COMMCONFIG, ByRef lpdwSize As Integer) As Boolean
End Function

Private Shared Function isCOMPortAvailable(ByVal aPortNumber As
Integer) As Boolean

If aPortNumber <= 0 Then
Return False
Else
Dim lCOMMCONFIG As COMMCONFIG
Dim lCOMMDONFIGSize As Integer = Marshal.SizeOf(lCOMMCONFIG)

lCOMMCONFIG.dwSize = lCOMMDONFIGSize

Dim lResult As Boolean = GetDefaultCommConfig("COM" +
aPortNumber.ToString, lCOMMCONFIG, lCOMMDONFIGSize)

Return lResult
End If
End Function
 
Back
Top