Link to home
Start Free TrialLog in
Avatar of Gery Wauters
Gery Wauters

asked on

WMI call to add TCP/IP Printer port

Hi,

I have a vbscript that adds a printer.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True 

Install "192.168.5.150" 

sub Install(strIP)
    InstallPrinterPort strIP
end Sub 

strBasePrinter = "Copier"
strPrinterName = "RICOH IM C6000 PCL 6"
strINFPath = "C:\Printers\IMC6000\disk1\OEMSETUP.INF"
strIPPort = "IP_192.168.5.150"
Set objShell = CreateObject("WScript.Shell")
strCommand = "cmd /c rundll32 printui.dll,PrintUIEntry /if /b """ & strBasePrinter & """ /f " & strINFPath & " /r """ & strIPPort & """ /m """ & strPrinterName & """ & /Z"
objShell.Run strCommand, 1, True

Sub InstallPrinterPort(strIP)


    Set colInstalledPorts =  objWMIService.ExecQuery _
        ("Select Name from Win32_TCPIPPrinterPort")
    For each objPort in colInstalledPorts
        If objPort.Name="IP_" & strIP then exit sub 
    Next


    Set objNewPort = objWMIService.Get _
        ("Win32_TCPIPPrinterPort").SpawnInstance_
    objNewPort.Name = "IP_" & strIP
    objNewPort.Protocol = 1
    objNewPort.HostAddress = strIP
    objNewPort.PortNumber = "9100"
    objNewPort.SNMPEnabled = False
    objNewPort.Put_
end Sub

Open in new window

I'm trying to do the same now in vb.NET, with these two functions:

Private Function CreatePrinterIP(ByVal IPPort As String, ByVal IPPortName As String)
        Try
            Dim searcher As New ManagementObjectSearcher(
                    "root\CIMV2",
                    "SELECT * FROM Win32_TCPIPPrinterPort")

            For Each queryObj As ManagementObject In searcher.Get()

                If queryObj("Name") = IPPortName Then
                    Return 1
                    Exit Function
                End If
            Next

            Dim objNewPort As ManagementObject = searcher.Get("Win32_TCPIPPrinterPort").SpawnInstance()

            objNewPort("Name") = IPPortName

            objNewPort("Protocol") = 1

            objNewPort("HostAddress") = IPPort

            objNewPort("PortNumber") = "9100"

            objNewPort("SNMPEnabled") = False

            objNewPort.Put()
            Return 1
        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            Return 0
        End Try
    End Function

    Private Sub CreatePrinter(ByVal IPPortName As String, ByVal PrinterName As String, ByVal Location As String, ByVal Drivername As String)
        Dim cmdToSend As String = "rundll32 printui.dll,PrintUIEntry /if /b """ & Printername & """ /f " & Location & " /r """ & IPPortName & """ /m """ & Drivername & """ & /Z"
        Shell(cmdToSend)
    End Sub

Open in new window

The CreatePrinterIP function worked once.
I then deleted the IP adress manually. And tried again. Now I receive an error that
The conversion of string Win32_TCPIPPrinterPort to type Integer is invalid.

Open in new window

on line
Dim objNewPort As ManagementObject = searcher.Get("Win32_TCPIPPrinterPort").SpawnInstance()

Open in new window

Does anyone have an idea how to solve this?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Gery Wauters
Gery Wauters

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial