Visual Basic System Services

AddPort: Adding and Deleting Application-Defined Ports
     
Posted:   Friday September 17, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     
Related:  

 

AddPort: Adding and Deleting Application-Defined Ports
AddPrinter: Add/Delete Local/Remote Printers using Existing Drivers
EnumPorts: Identify Windows' Available Ports
     
 Prerequisites
None.

This page shows how to add and delete application-defined ports.  It was developed under WinNT4 SP5, but should function under Win9x as well.

The AddPort function adds the name of a port to the list of supported ports. AddPortEx creates a port and adds it to the list of ports currently supported by the specified monitor in the spooler environment.

AddPort prompts the user to name the port being added. The AddPortEx methods show how to perform this programmatically. As shown here, the server is set as vbNullString, which represents the local machine.  See the MSDN documentation for AddPortEx for information on extending these methods to create remote ports.

 BAS Module Code
None.

 Form Code
On a form add four text boxes (Text1-Text4), and array of option buttons (Option1(0)-Option(2)), and two command buttons (Command1, Command2). Add a label (Lable1 (in blue)) and the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'PORT_INFO_2S Description
'pPortName: Pointer to a null-terminated string that
'identifies a supported printer port (for example, "LPT1:").

'pMonitorName: Pointer to a null-terminated string that
'identifies an installed monitor (for example, "PJL monitor").

'pDescription: Pointer to a null-terminated string that
'describes the port in more detail (for example, if
'pPortName is "LPT1:", pDescription is "printer port").

'fPortType: Handle to the type of port. Can be one
'of these values:
'  PORT_TYPE_WRITE
'  PORT_TYPE_READ
'  PORT_TYPE_REDIRECTED
'  PORT_TYPE_NET_ATTACHED
'
'Note: PORT_INFO_2S is identical to the PORT_INFO_2 UDT 
'provided by the api viewer. I have renamed the type for 
'this example because the EnumPorts demo uses PORT_INFO_2 
'defined with all members as Long.
Private Type PORT_INFO_2S
   pPortName As String
   pMonitorName As String
   pDescription As String
   fPortType As Long
   Reserved As Long
End Type

'PORT_INFO_1 Description
'pPortName: Pointer to a null-terminated string that
'identifies a supported printer port (for example, "LPT1:").
Private Type PORT_INFO_1
   pPortName As String
End Type

Private Enum PortTypes
  PORT_TYPE_WRITE = &H1
  PORT_TYPE_READ = &H2
  PORT_TYPE_REDIRECTED = &H4
  PORT_TYPE_NET_ATTACHED = &H8
End Enum

'The AddPort function browses the network to find
'existing ports, and displays a dialog box for the
'user. The AddPort function should validate the port
'name entered by the user by calling EnumPorts to
'ensure that no duplicate names exist.

'The caller of the AddPort function must have
'SERVER_ACCESS_ADMINISTER access to the server to
'which the port is connected.
Private Declare Function AddPort Lib "winspool.drv" _
   Alias "AddPortA" _
  (ByVal pName As String, _
   ByVal hwnd As Long, _
   ByVal pMonitorName As String) As Long

Private Declare Function DeletePort Lib "winspool.drv" _
   Alias "DeletePortA" _
  (ByVal pName As String, _
   ByVal hwnd As Long, _
   ByVal pPortName As String) As Long
 
Private Declare Function AddPortEx Lib "winspool.drv" _
   Alias "AddPortExA" _
  (ByVal pName As String, _
   ByVal pLevel As Long, _
   lpBuffer As Any, _
   ByVal pMonitorName As String) As Long
   
         

Private Sub Form_Load()

   Command1.Enabled = False   'the Add Port button

End Sub


Private Sub Option1_Click(Index As Integer)

  'Server passed as vbNullString (local machine)
   Text1.Enabled = False
   Text2.Enabled = Index <> 0
   Text3.Enabled = Index <> 0
   Text4.Enabled = Index <> 0
   
   Command1.Enabled = Index > -1
   
End Sub


Private Sub Command1_Click()

   Dim success As Boolean
   Dim Port1 As PORT_INFO_1
   Dim Port2 As PORT_INFO_2S
   
  '''''''''''''''''''''''''''''''''''''
  'use AddPort
   If Option1(0).Value = True Then
   
      Call AddPort(vbNullString, 0&, "Local Port")
      
   End If
   
  '''''''''''''''''''''''''''''''''''''
  'use AddPortEx with Port Info Level 1
   If Option1(1).Value = True Then
   
      Port1.pPortName = Text4
      success = AddPortEx(vbNullString, 1, Port1, "Local Port")
   
   End If
   
  '''''''''''''''''''''''''''''''''''''
  'use AddPortEx with Port Info Level 2
   If Option1(2).Value = True Then
   
     'pMonitorName must point to a valid installed monitor
      With Port2
         .fPortType = PORT_TYPE_WRITE Or PORT_TYPE_READ
         .pMonitorName = Text2
         .pPortName = Text4
      End With
      
      success = AddPortEx(vbNullString, 1, Port2, "Local Port")
   
   End If
   
  '''''''''''''''''''''''''''''''''''''
  'results (valid for index 1 and 2 only)
   
   If Option1(0).Value = True Then
      
     'docs state that if the AddPort function
     'succeeds the return value is a nonzero value.
     'and if it fails the return value is zero.
     'However canceling the dialog also returns
     'non-zero, making a check of a returned
     'value unreliable.
      If success Then
         Label1.Caption = "AddPort returned non-zero"
      Else
         Label1.Caption = "AddPort returned zero"
      End If
     
     
   Else
   
      If success Then
         Label1.Caption = "Port '" & Text4 & "' added"
      Else
         Label1.Caption = "AddPortEx failed"
      End If
      
   End If
   
End Sub


Private Sub Command2_Click()

   Dim success As Boolean

  'This routine is only coded to handle ports added 
  'via AddPortEx, as there is no way of telling (without 
  'enumerating the ports before and after adding), what 
  'the name of a user-named port is. Therefore if the 
  'AddPort was used, you will need to modify the line 
  'below to indicate the name of the port to delete.
   success = DeletePort("", 0, (Text4))
   
   If success Then
      Label1.Caption = "Port '" & Text4 & "' has been deleted."
   Else
      Label1.Caption = "Port not deleted or nothing to do."
   End If

End Sub
 Comments
To view the current set of available ports, open the properties for any printer and select Ports (this is not the same as the Ports control panel applet). The printer properties do not respond to broadcast messages, so you will need to close and reopen the properties to see additions/deletions from the port list.

Also note that this is but one step in installing a new port type. Without first creating a new Monitor, you are restricted to using the monitors already installed on the system. See the MSDN for more information about Ports and Monitors.


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter