How to install a shared network printer with VB.NET and WMI

Today I show you an example of how to install a network printer with VB.NET and WMI by using the AddPrinterConnection method of the wmi Win32_Printer class. Just replace hostname with the name of the printserver/workstation and queuename with the shared printer queuename.

        Try

            Dim scope As ManagementScope = New ManagementScope("root\CIMV2")
            scope.Options.Impersonation = ImpersonationLevel.Impersonate
            scope.Connect()

            Dim path As New ManagementPath("Win32_Printer")
            Dim mgtClass As New ManagementClass(scope, path, Nothing)

            Dim inputParameters As ManagementBaseObject = mgtClass.GetMethodParameters("AddPrinterConnection")
            inputParameters("Name") = "\\hostname\queuename"

            Dim outputParameters As ManagementBaseObject = mgtClass.InvokeMethod("AddPrinterConnection", inputParameters, Nothing)
            Dim returnValue As UInteger = UInteger.Parse(outputParameters("ReturnValue"))

            If (returnValue = 0) Then
                Console.WriteLine("Success")
            ElseIf (returnValue = 5) Then
                Console.WriteLine("Access Denied")
            ElseIf (returnValue = 1801) Then
                Console.WriteLine("Invalid Printer Name")
            ElseIf (returnValue = 1930) Then
                Console.WriteLine("Incompatible Printer Driver")
            End If

        Catch ex As Exception

        End Try

Troubleshoooting device driver installation with setupapilog.txt

Today I tried to install the ”HP Universal Driver PCL6” (32-bit) on a Windows XP machine with WMI and VB.NET. All I got was the errorcode 2 and a failed installation. What does that mean? Windows errorcodes is not my (anyones?) speciality…

After some googling I found this very good post that describes the problem and a possible solution.

The key to solving this puzzle and possibly other device installation problems is the file setupapi.log in the windows folder. After running my code again I got this in my setupapi.log file (in swedish):

[SetupAPI Log]
OS-version = 5.1.2600 Service Pack 3
Plattforms-ID = 2 (NT)
Service Pack = 3.0
Svit = 0x0100
Produkttyp = 1
Arkitektur = 2006157608
[2010/01/22 14:20:41 5392.4 Driver Install]
#-198 Bearbetad kommandorad: C:\WINNT\system32\wbem\wmiprvse.exe
#I060 Ange vald drivrutin.
#I060 Ange vald drivrutin.
[2010/01/22 14:20:41 5392.5]
#-198 Bearbetad kommandorad: C:\WINNT\system32\wbem\wmiprvse.exe
#-167 SPFILENOTIFY_NEEDMEDIA: Tagg = p6i2svww.cab, Beskrivning= PDL_LANG, Källsökväg = C:\CentralPrintArea\FKPrint\HP\Universal\32bit\HP Universal, Källfil = UNIDRV.HLP, Flaggor = 0x00000000.
#E169 SPFILENOTIFY_NEEDMEDIA: returnerade FILEOP_ABORT. Fel 2: Det går inte att hitta filen.
#W187 Installationen misslyckades. Försök att återställa originalfiler utförs.

It says that I got an errorcode 2 (missing file) and which file that is missing. In this case it is missing the file ”UNIDRV.HLP” that is not available in swedish. All I had to do is to extract this file from the cab-file ”p6i2svww.cab” to the root of the driverfolder. After doing this and running the program once more everything is working and the driver installs. After this I get the following output in setupapi.log.

[SetupAPI Log]
OS-version = 5.1.2600 Service Pack 3
Plattforms-ID = 2 (NT)
Service Pack = 3.0
Svit = 0x0100
Produkttyp = 1
Arkitektur = 2006157608
[2010/01/22 14:28:43 4784.4 Driver Install]
#-198 Bearbetad kommandorad: C:\WINNT\system32\wbem\wmiprvse.exe
#I060 Ange vald drivrutin.
#I060 Ange vald drivrutin.

That looks good I suppose! At least better then before. And the driver is installed ok!

You can read and download information about setupapilog.txt at the Microsoft website. If you read the whitepaper you could possibly solve many kinds of STRANGE device driver installation problems.

This is Microsofts description of setupapilog.txt

Windows XP and later versions log system operations more extensively than previous versions of Windows do. One of the most useful log files for debugging is the SetupAPI log file (setupapi.log). This plain-text file maintains the information that SetupAPI records about device installation, service-pack installation, and hotfix installation. Specifically, the file maintains a record of device and driver changes, as well as major system changes,

If you wnat more verbose logging to the setupapi.txt file you can edit the windows registry key ”HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\LogLevel”. If you raise this value to 255 you get a lot of logging which can be useful at times.

I hope this helps someone and that HP fixes the broken driver. It must be some sort of localization problem within this driver. Or maybe that I have the wrong driver…

The path to this file in Windows XP is %WINDIR%\Setupapi.log. On Windows Server 2008R2 the log files are found in %WINDIR%\Inf\ and are called setupapi.app and setupapi.dev.

There is always something new to learn in the Windows world! Happy troubleshooting!

Install printerports with WMI and VB.NET

This is an example of how you install TCP/IP printerports with .NET and WMI (Windows Management Instrumentation).

A list of properties can be found at MSDN Win32_TCPIPPrinterPort

Other topics:
How to install printerdrivers with wmi and VB.NET
How to create a printerqueu with .NET and WMI

Imports System.Management

Try
  Dim mp As ManagementPath = New ManagementPath("Win32_TCPIPPrinterPort")
  Dim co As ConnectionOptions = New ConnectionOptions()
  co.EnablePrivileges = True

  Dim ms As ManagementScope = New ManagementScope("\\" + Environment.MachineName + "\root\cimv2", co)

  Dim port As ManagementObject = New ManagementClass(ms, mp, Nothing).CreateInstance()
  port.SetPropertyValue("Name", "portname")
  port.SetPropertyValue("Protocol", 1)
  port.SetPropertyValue("HostAddress", "10.15.12.12")
  port.SetPropertyValue("PortNumber", "9100")

  Dim po As PutOptions = New PutOptions()
  po.UseAmendedQualifiers = True
  po.Type = PutType.UpdateOrCreate
  port.Put(po)
Catch ex As Exception
  ' do something
End Try

Install printerdrivers with WMI and VB.NET

This is an example of how you install printerdrivers with .NET and WMI (Windows Management Instrumentation). Most examples on the Internet shows how this can be done with vbscript, printui.dll and such…

A list of properties can be found at MSDN Win32_PrinterDriver

Other topics:
How to install printerports with WMI and VB.NET
How to create a printerqueu with .NET and WMI

Imports System.Management

Dim infPath  as String = "c:\driver\printerdriver.inf"
Dim mp As ManagementPath = New ManagementPath("Win32_PrinterDriver")

Dim co As ConnectionOptions = New ConnectionOptions()
co.EnablePrivileges = True

Dim ms As ManagementScope = New ManagementScope("\\" + Environment.MachineName + "\root\cimv2", co)

Dim mcPrinterDriver As New ManagementClass(ms, mp, Nothing)
mcPrinterDriver.SetPropertyValue("Name", "drivername")
mcPrinterDriver.SetPropertyValue("SupportedPlatform", "Windows NT x86") ' x86-architecture
'mcPrinterDriver.SetPropertyValue("SupportedPlatform", "Windows x64") ' x64-architecture
mcPrinterDriver.SetPropertyValue("Version", 3)
mcPrinterDriver.SetPropertyValue("FilePath", System.IO.Path.GetDirectoryName(infPath))
mcPrinterDriver.SetPropertyValue("InfName", infPath)

Dim inParams As System.Management.ManagementBaseObject = Nothing
inParams = mcPrinterDriver.GetMethodParameters("AddPrinterDriver")
inParams("DriverInfo") = CType(mcPrinterDriver, System.Management.ManagementBaseObject)
Dim outParams As System.Management.ManagementBaseObject = mcPrinterDriver.InvokeMethod("AddPrinterDriver", inParams, Nothing)

Dim uiReturnValue As UInteger = System.Convert.ToUInt32(outParams.Properties("ReturnValue").Value)

I have tested this in Windows XP, Windows 7 and Windows 2008R2. Make sure your account has privileges to install drivers.

On Windows XP the drivers are installed physically to:
C:\WINNT\system32\spool\drivers

And in the registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments