Creating Printer Port in VB

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

I need to create a file printer port. I can do it using the PRNADMIN.DLL
like so:

Public Sub CreateFilePort(ByVal FilePath As String)
Dim PRNADMIN As New PRNADMINLib.PrintMaster
Dim port As New PRNADMINLib.Port
port.PortName = FilePath
port.PortType = 3 'Standard Local Port
PRNADMIN.PortAdd(port)
End Sub

Is there any way I can do it without using this COM assembly?
 
WMI will do it.

You could also use the Microsoft scripting runtime (is also COM but will
almost allways be installed)

There might also be a command line way with PRINTUI.DLL
 
I've searched through WMI but only found Win32_Printer and
Win32_TCPIPPrinterPort, neither will let me add the kind of port that I
want. Do you know what WMI class will let me add the port?

The PRNADMIN.DLL, according to its documentation, is a wrapper around
PRINTUI.DLL. I tried to figure out how to add the port directly with
PRINTUI.DLL but was unsuccessful there as well.

Ideally, I'd like to be able to do it using WMI or Win32Api, that way I
don't have to rely on an external DLL.
 
I am sure I gave you the WMI code for this arounf 10 days ago Tery. If it
wasn't you then it was another user
 
You didn't give me code, you gave me links to WMI reference pages. I've
searched through all those pages. I used the WMI Code Creator extensively to
search for anything related to adding a printer port but came up empty. If
you have a specific class for me to look at, i'd appreciate it.

I did, however, get it to work using the AddPortEx API call, though this
call is "obsolete" according to MSDN. Here's the code:

<StructLayout(LayoutKind.Sequential)> _
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, ByRef lpBuffer As
PORT_INFO_1, ByVal pMonitorName As String) As Integer

Dim p1 As PORT_INFO_1
p1.pPortName = "C:\PrinterFile.txt"
API.apiAddPortEx(Nothing, 1, p1, "Local Port")

Now I need to figure out how to delete a port. There is a DeletePort API
call, but it throws up a dialog for the user. I want to delete the port
without user intervention. There isn't a DeletePortEx call that I can find
(as a counterpart to the AddPortEx call).
 
Back
Top