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