1 out of 1 rated this helpful - Rate this topic

AddPrinterConnection method of the Win32_Printer Class

Applies to: desktop apps only

The AddPrinterConnection WMI class method provides a connection to an existing printer on the network, and adds it to the list of available printers.

This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.

Syntax

uint32 AddPrinterConnection(
  [in]  string Name
);

Parameters

Name [in]

Friendly name for the printer.

Return value

Return codeDescription
0

Success

5

Access Denied

1801

Invalid Printer Name

1930

Incompatible Printer Driver

 

Examples

For script code examples, see WMI Tasks for Scripts and Applications and the TechNet ScriptCenter Script Repository.

For C++ code examples, see WMI C++ Application Examples.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Namespace

\root\CIMV2

MOF

Cimwin32.mof

DLL

Cimwin32.dll

See also

Computer System Hardware Classes
Win32_Printer

 

 

Send comments about this topic to Microsoft

Build date: 3/9/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
As found on ObjectMix using the WMI classes from System.Management
http://objectmix.com/dotnet/109089-wmi-printer-mgmt.html

String portNumber = "9100";
String printerIP = "99.99.1.1";
String portName = "IP_" + printerIP;

ConnectionOptions options = new ConnectionOptions();
options.EnablePrivileges = true;
ManagementScope mscope = new ManagementScope(ManagementPath.DefaultPath, options);
mscope.Connect();

ManagementPath mpPort = new ManagementPath("Win32_TCPIPPrinterPort");
ManagementClass mcPort = new ManagementClass(mscope, mpPort, new ObjectGetOptions());
ManagementObject moPort = mcPort.CreateInstance();
moPort.Properties["Name"].Value = portName;
moPort.Properties["HostAddress"].Value = printerIP;
moPort.Properties["PortNumber"].Value = portNumber;
moPort.Properties["Protocol"].Value = 1;
moPort.Put();

ManagementPath mpPrinter = new System.Management.ManagementPath("Win32_Printer");
ManagementClass mcPrinter = new ManagementClass(mscope, mpPrinter, new ObjectGetOptions());
ManagementObject moPrinter = mcPrinter.CreateInstance();

moPrinter.Properties["Name"].Value = "My Printer";
moPrinter.Properties["DeviceID"].Value = "My Printer";
moPrinter.Properties["DriverName"].Value = "Generic / Text Only";
moPrinter.Properties["PortName"].Value = portName;
moPrinter.Properties["Network"].Value = true;
moPrinter.Properties["Shared"].Value = false;
moPrinter.Put();
How to add a local printer?
After a LOT OF search i found a .NET solution.
The following example adds a printer to your system.

Dim strPrinterName as String = "Isidoros Printer"
Dim strComputer AsString = My.Computer.Name Dim objWMIService, objPrinter AsObject

objWMIService = GetObject(

"winmgmts:" _

&

"{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

objPrinter = objWMIService.Get(

"Win32_Printer").SpawnInstance_ objPrinter.Name = strPrinterName
objPrinter.DriverName = "Generic / Text Only"

objPrinter.PortName =

"c:\temp\file.prn"

objPrinter.DeviceID = strPrinterName

'objPrinter.Location = "Athens, Greece"

objPrinter.Network =

False

objPrinter.Shared =

False'objPrinter.ShareName = "MyShareName"

objPrinter.Put_()

Hope that helps!
Isidoros (MCTS)
Athens, Greece