AddPortEx Trouble

T

Terry Olsen

Trying to use AddPortEx but I'm getting an error. I modified the code I
found that Randy Birch did for VB6.


Public Structure PORT_INFO_1
Dim pPortName As String
End Structure

Public Declare Function AddPortEx Lib "winspool.drv" Alias "AddPortExA"
(ByVal pName As String, ByVal pLevel As Integer, ByVal lpBuffer As Object,
ByVal pMonitorName As String) As Integer

Dim PortInfo1 As PORT_INFO_1
PortInfo1.pPortName = FilePath
Dim x As Integer = AddPortEx(Nothing, 1, PortInfo1, "Local Port")
<--Exception thrown here

The error i'm getting is "Value does not fall within the expected range"

Looking for help getting this to work.

Thanks.
 
T

Tom Shelton

Trying to use AddPortEx but I'm getting an error. I modified the code I
found that Randy Birch did for VB6.


Public Structure PORT_INFO_1
Dim pPortName As String
End Structure

Public Declare Function AddPortEx Lib "winspool.drv" Alias "AddPortExA"
(ByVal pName As String, ByVal pLevel As Integer, ByVal lpBuffer As Object,
ByVal pMonitorName As String) As Integer

Dim PortInfo1 As PORT_INFO_1
PortInfo1.pPortName = FilePath
Dim x As Integer = AddPortEx(Nothing, 1, PortInfo1, "Local Port")
<--Exception thrown here

The error i'm getting is "Value does not fall within the expected range"

Looking for help getting this to work.

Thanks.

I can't find any information on AddPortEx. The closest I can find is a
definition for AddPortEx that looks different from the above declare, and was
made obolete with NT4. That funciton looks like:

Public Auto Function AddPortEx Lib "winspool.drv" ( _
ByVal hMonitor As IntPtr,
ByVal pName As String, _
ByVal Level As Integer, _
ByRef lpBuffer As PORT_INFO_1, _
ByVal lpMonitorName As String) As Boolean


So, my advice to you... Declare the function Auto and loose the alias. That
isn't needed in VB.NET for A/W functions.

You should declare the PORT_INFO_1 function like:

<StructLayout (StructLayout := LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure PORT_INFO_1
Public pPortName As String
End Structure

Anyway, calling it:

If AddPortEx (IntPtr.Zero, Nothing, 1, PortInfo1, "Local Port") Then
'Do cool stuff
Else
' don't do cool stuff
End If

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top