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 code | Description |
|---|---|
|
Success |
|
Access Denied |
|
Invalid Printer Name |
|
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 |
|
|
DLL |
|
See also
Send comments about this topic to Microsoft
Build date: 3/9/2012
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();
- 8/12/2011
- pcbbc
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 = strPrinterNameobjPrinter.DriverName = "Generic / Text Only"
objPrinter.PortName =
"c:\temp\file.prn"objPrinter.DeviceID = strPrinterName
'objPrinter.Location = "Athens, Greece"objPrinter.Network =
FalseobjPrinter.Shared =
False'objPrinter.ShareName = "MyShareName"objPrinter.Put_()
Hope that helps!
Isidoros (MCTS)
Athens, Greece
- 11/7/2009
- Isidoros