AddPrinterConnection failing to add network printer to remote pc

  • Thread starter Thread starter veeralp
  • Start date Start date
V

veeralp

I have been playing with WMI to add a network printer connection to a
Windows XP pc. My environment consists of a server running Windows
Server 2003 and Visual Studio 2005 and a test pc running windows xp.
Further, I have setup a full domain controller environment.

I have managed to write the code to add the network printer and it
works fine when installing the printer on the same machine the code is
executing from i.e the server or xp pc. However, if I run the code
from the server to install the network printer onto the test pc, it
does not work.

An exception is generated stating "Not Supported, the inner exception
is blank. The exception occurs when the mgtClass.InvokeMethod is
executed, see code.

I can manually install the network printer on the test pc via the
control panel, thus proving that the connectivity between the two
machines is ok.

Questions:

1) If I specifiy the ip address of the test pc in the ManagementScope
object and when an object of Management class is instantiated does
this imply that the network printer will be added to the test pc?

2)Using the AddPrinterConnection method from the WIN32_PRINTER class,
is this the only way to add a network printer ? Or is there an
alternative way I can achieve this.

3) To install the network printer on the server itself, omit the IP
address,ipAddressOfClientPC, of the server from the ManagementScope
object and it installs fine, provided the code itself is running of
the server. So this leads me to believe its an issue with
ManagementScope. Note, if installing on the same machine as the code
is running on then the username and password need not to be defined.
So what do I need to do to the Management Scope object to make it
work?

4) The prnadmin.dll which ships as part of WIndows Server 2003 SDK was
designed to be used with scripting lanuages for programmatic printer
manipulation. Within prnadmin.dll there is a method called
AddPrinterConnectionEx which has the ability to install network
printers on remote pc. My question is that which API is the
AddPrinterConnectionEx calling in order for the network printer to be
installed on a remote computer ?

5)A suggested alternative was this command line method : "rundll32
printui.dll,PrintUIEntry /?" Which api does the printui.dll call
inorder for network printers to be installed on remote computers?

I used the WMI code generated by the WMI Code Generator,
http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e,

I have added my code below.

If you can point me in the right direction as to the mistake I am
making, I would appreciate it.

thanks
Veeral

using System;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;

namespace SampleApp
{
public class AddPrinterConnection
{
public static void Main()
{
//only required if need to install network printer on another
machine.
string username = "administrator";
string password = "password";

string ipAddressOfClientPC = "192.168.0.22";
ManagementScope scope = new ManagementScope("\\\\" +
ipAddressOfClientPC + "\\root\\cimv2");


scope.Options.Username = username;
scope.Options.Password = password;
scope.Options.Impersonation = ImpersonationLevel.Impersonate;

scope.Connect();

ManagementPath path = new ManagementPath("Win32_Printer");
ManagementClass mgtClass = new ManagementClass(scope, path, null);
using (mgtClass)
{
try
{
ManagementBaseObject inputParameters =
mgtClass.GetMethodParameters("AddPrinterConnection");

string sharedPrinterAddress = "\\\\192.168.0.1\\printer2";
inputParameters["Name"] = sharedPrinterAddress;

ManagementBaseObject outputParameters =
mgtClass.InvokeMethod("AddPrinterConnection", inputParameters, null);
uint errorCode = (uint)outputParameters["ReturnValue"];

Console.WriteLine(errorCode.ToString());
}
catch (Exception exception)
{
Console.WriteLine(exception.Message + "\n" +
exception.InnerException);
}
Console.ReadLine();
}
}
}
}
 
WMI only adds a user connection which occurs in the logged on user session.
Adding a connection should be triggered by a local logon.

You might be better off adding a "machine connection" to each machine, then
whomever logs into the machine will get that connection.

in order to delete the machine connection you have to use the /gd switch,
the connection will be removed at the next logon for each user.

http://members.shaw.ca/bsanders/NetPrinterAllUsers.htm


--
Alan Morris
Windows Printing Team
Search the Microsoft Knowledge Base here:
http://support.microsoft.com/default.aspx?scid=fh;[ln];kbhowto

This posting is provided "AS IS" with no warranties, and confers no rights.

I have been playing with WMI to add a network printer connection to a
Windows XP pc. My environment consists of a server running Windows
Server 2003 and Visual Studio 2005 and a test pc running windows xp.
Further, I have setup a full domain controller environment.

I have managed to write the code to add the network printer and it
works fine when installing the printer on the same machine the code is
executing from i.e the server or xp pc. However, if I run the code
from the server to install the network printer onto the test pc, it
does not work.

An exception is generated stating "Not Supported, the inner exception
is blank. The exception occurs when the mgtClass.InvokeMethod is
executed, see code.

I can manually install the network printer on the test pc via the
control panel, thus proving that the connectivity between the two
machines is ok.

Questions:

1) If I specifiy the ip address of the test pc in the ManagementScope
object and when an object of Management class is instantiated does
this imply that the network printer will be added to the test pc?

2)Using the AddPrinterConnection method from the WIN32_PRINTER class,
is this the only way to add a network printer ? Or is there an
alternative way I can achieve this.

3) To install the network printer on the server itself, omit the IP
address,ipAddressOfClientPC, of the server from the ManagementScope
object and it installs fine, provided the code itself is running of
the server. So this leads me to believe its an issue with
ManagementScope. Note, if installing on the same machine as the code
is running on then the username and password need not to be defined.
So what do I need to do to the Management Scope object to make it
work?

4) The prnadmin.dll which ships as part of WIndows Server 2003 SDK was
designed to be used with scripting lanuages for programmatic printer
manipulation. Within prnadmin.dll there is a method called
AddPrinterConnectionEx which has the ability to install network
printers on remote pc. My question is that which API is the
AddPrinterConnectionEx calling in order for the network printer to be
installed on a remote computer ?

5)A suggested alternative was this command line method : "rundll32
printui.dll,PrintUIEntry /?" Which api does the printui.dll call
inorder for network printers to be installed on remote computers?

I used the WMI code generated by the WMI Code Generator,
http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e,

I have added my code below.

If you can point me in the right direction as to the mistake I am
making, I would appreciate it.

thanks
Veeral

using System;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;

namespace SampleApp
{
public class AddPrinterConnection
{
public static void Main()
{
//only required if need to install network printer on another
machine.
string username = "administrator";
string password = "password";

string ipAddressOfClientPC = "192.168.0.22";
ManagementScope scope = new ManagementScope("\\\\" +
ipAddressOfClientPC + "\\root\\cimv2");


scope.Options.Username = username;
scope.Options.Password = password;
scope.Options.Impersonation = ImpersonationLevel.Impersonate;

scope.Connect();

ManagementPath path = new ManagementPath("Win32_Printer");
ManagementClass mgtClass = new ManagementClass(scope, path, null);
using (mgtClass)
{
try
{
ManagementBaseObject inputParameters =
mgtClass.GetMethodParameters("AddPrinterConnection");

string sharedPrinterAddress = "\\\\192.168.0.1\\printer2";
inputParameters["Name"] = sharedPrinterAddress;

ManagementBaseObject outputParameters =
mgtClass.InvokeMethod("AddPrinterConnection", inputParameters, null);
uint errorCode = (uint)outputParameters["ReturnValue"];

Console.WriteLine(errorCode.ToString());
}
catch (Exception exception)
{
Console.WriteLine(exception.Message + "\n" +
exception.InnerException);
}
Console.ReadLine();
}
}
}
}
 
Hi Allan

Thanks for responding. I strongly believe MS should make this very
clear on their documentation,http://msdn2.microsoft.com/en-us/library/
aa394363.aspx for the AddPrinterConnection method.

The confusing part for me is that you have rundll32
printui.dll,PrintUIEntry /? which also permits you to add printer
connections but to remote computers as well. Why not include this
WMI ? It just does not make sense to me.

Thanks
Veeral

WMI only adds a user connection which occurs in the logged on user session.
Adding a connection should be triggered by a local logon.

You might be better off adding a "machine connection" to each machine, then
whomever logs into the machine will get that connection.

in order to delete the machine connection you have to use the /gd switch,
the connection will be removed at the next logon for each user.

http://members.shaw.ca/bsanders/NetPrinterAllUsers.htm


--
Alan Morris
Windows Printing Team
Search the Microsoft Knowledge Base here:
http://support.microsoft.com/default.aspx?scid=fh;[ln];kbhowto

This posting is provided "AS IS" with no warranties, and confers no rights.

I have been playing with WMI to add a network printer connection to a
Windows XP pc. My environment consists of a server running Windows
Server 2003 and Visual Studio 2005 and a test pc running windows xp.
Further, I have setup a full domain controller environment.

I have managed to write the code to add the network printer and it
works fine when installing the printer on the same machine the code is
executing from i.e the server or xp pc. However, if I run the code
from the server to install the network printer onto the test pc, it
does not work.

An exception is generated stating "Not Supported, the inner exception
is blank. The exception occurs when the mgtClass.InvokeMethod is
executed, see code.

I can manually install the network printer on the test pc via the
control panel, thus proving that the connectivity between the two
machines is ok.

Questions:

1) If I specifiy the ip address of the test pc in the ManagementScope
object and when an object of Management class is instantiated does
this imply that the network printer will be added to the test pc?

2)Using the AddPrinterConnection method from the WIN32_PRINTER class,
is this the only way to add a network printer ? Or is there an
alternative way I can achieve this.

3) To install the network printer on the server itself, omit the IP
address,ipAddressOfClientPC, of the server from the ManagementScope
object and it installs fine, provided the code itself is running of
the server. So this leads me to believe its an issue with
ManagementScope. Note, if installing on the same machine as the code
is running on then the username and password need not to be defined.
So what do I need to do to the Management Scope object to make it
work?

4) The prnadmin.dll which ships as part of WIndows Server 2003 SDK was
designed to be used with scripting lanuages for programmatic printer
manipulation. Within prnadmin.dll there is a method called
AddPrinterConnectionEx which has the ability to install network
printers on remote pc. My question is that which API is the
AddPrinterConnectionEx calling in order for the network printer to be
installed on a remote computer ?

5)A suggested alternative was this command line method : "rundll32
printui.dll,PrintUIEntry /?" Which api does the printui.dll call
inorder for network printers to be installed on remote computers?

I used the WMI code generated by the WMI Code Generator,
http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e,

I have added my code below.

If you can point me in the right direction as to the mistake I am
making, I would appreciate it.

thanks
Veeral

using System;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;

namespace SampleApp
{
public class AddPrinterConnection
{
public static void Main()
{
//only required if need to install network printer on another
machine.
string username = "administrator";
string password = "password";

string ipAddressOfClientPC = "192.168.0.22";
ManagementScope scope = new ManagementScope("\\\\" +
ipAddressOfClientPC + "\\root\\cimv2");


scope.Options.Username = username;
scope.Options.Password = password;
scope.Options.Impersonation = ImpersonationLevel.Impersonate;

scope.Connect();

ManagementPath path = new ManagementPath("Win32_Printer");
ManagementClass mgtClass = new ManagementClass(scope, path, null);
using (mgtClass)
{
try
{
ManagementBaseObject inputParameters =
mgtClass.GetMethodParameters("AddPrinterConnection");

string sharedPrinterAddress = "\\\\192.168.0.1\\printer2";
inputParameters["Name"] = sharedPrinterAddress;

ManagementBaseObject outputParameters =
mgtClass.InvokeMethod("AddPrinterConnection", inputParameters, null);
uint errorCode = (uint)outputParameters["ReturnValue"];

Console.WriteLine(errorCode.ToString());
}
catch (Exception exception)
{
Console.WriteLine(exception.Message + "\n" +
exception.InnerException);
}
Console.ReadLine();
}
}
}
}
 
WMI is a separate component. The remote calls are actually local calls on
the remote machine so you are stuck in the same place. WMI needs to be run
as admin on the remote machine and the admin is not usually the person to
whom you are making a connection.

--
Alan Morris
Windows Printing Team
Search the Microsoft Knowledge Base here:
http://support.microsoft.com/default.aspx?scid=fh;[ln];kbhowto

This posting is provided "AS IS" with no warranties, and confers no rights.

Hi Allan

Thanks for responding. I strongly believe MS should make this very
clear on their documentation,http://msdn2.microsoft.com/en-us/library/
aa394363.aspx for the AddPrinterConnection method.

The confusing part for me is that you have rundll32
printui.dll,PrintUIEntry /? which also permits you to add printer
connections but to remote computers as well. Why not include this
WMI ? It just does not make sense to me.

Thanks
Veeral

WMI only adds a user connection which occurs in the logged on user
session.
Adding a connection should be triggered by a local logon.

You might be better off adding a "machine connection" to each machine,
then
whomever logs into the machine will get that connection.

in order to delete the machine connection you have to use the /gd switch,
the connection will be removed at the next logon for each user.

http://members.shaw.ca/bsanders/NetPrinterAllUsers.htm


--
Alan Morris
Windows Printing Team
Search the Microsoft Knowledge Base here:
http://support.microsoft.com/default.aspx?scid=fh;[ln];kbhowto

This posting is provided "AS IS" with no warranties, and confers no
rights.

I have been playing with WMI to add a network printer connection to a
Windows XP pc. My environment consists of a server running Windows
Server 2003 and Visual Studio 2005 and a test pc running windows xp.
Further, I have setup a full domain controller environment.

I have managed to write the code to add the network printer and it
works fine when installing the printer on the same machine the code is
executing from i.e the server or xp pc. However, if I run the code
from the server to install the network printer onto the test pc, it
does not work.

An exception is generated stating "Not Supported, the inner exception
is blank. The exception occurs when the mgtClass.InvokeMethod is
executed, see code.

I can manually install the network printer on the test pc via the
control panel, thus proving that the connectivity between the two
machines is ok.

Questions:

1) If I specifiy the ip address of the test pc in the ManagementScope
object and when an object of Management class is instantiated does
this imply that the network printer will be added to the test pc?

2)Using the AddPrinterConnection method from the WIN32_PRINTER class,
is this the only way to add a network printer ? Or is there an
alternative way I can achieve this.

3) To install the network printer on the server itself, omit the IP
address,ipAddressOfClientPC, of the server from the ManagementScope
object and it installs fine, provided the code itself is running of
the server. So this leads me to believe its an issue with
ManagementScope. Note, if installing on the same machine as the code
is running on then the username and password need not to be defined.
So what do I need to do to the Management Scope object to make it
work?

4) The prnadmin.dll which ships as part of WIndows Server 2003 SDK was
designed to be used with scripting lanuages for programmatic printer
manipulation. Within prnadmin.dll there is a method called
AddPrinterConnectionEx which has the ability to install network
printers on remote pc. My question is that which API is the
AddPrinterConnectionEx calling in order for the network printer to be
installed on a remote computer ?

5)A suggested alternative was this command line method : "rundll32
printui.dll,PrintUIEntry /?" Which api does the printui.dll call
inorder for network printers to be installed on remote computers?

I used the WMI code generated by the WMI Code Generator,
http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e,

I have added my code below.

If you can point me in the right direction as to the mistake I am
making, I would appreciate it.

thanks
Veeral

using System;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;

namespace SampleApp
{
public class AddPrinterConnection
{
public static void Main()
{
//only required if need to install network printer on another
machine.
string username = "administrator";
string password = "password";

string ipAddressOfClientPC = "192.168.0.22";
ManagementScope scope = new ManagementScope("\\\\" +
ipAddressOfClientPC + "\\root\\cimv2");


scope.Options.Username = username;
scope.Options.Password = password;
scope.Options.Impersonation = ImpersonationLevel.Impersonate;

scope.Connect();

ManagementPath path = new ManagementPath("Win32_Printer");
ManagementClass mgtClass = new ManagementClass(scope, path, null);
using (mgtClass)
{
try
{
ManagementBaseObject inputParameters =
mgtClass.GetMethodParameters("AddPrinterConnection");

string sharedPrinterAddress = "\\\\192.168.0.1\\printer2";
inputParameters["Name"] = sharedPrinterAddress;

ManagementBaseObject outputParameters =
mgtClass.InvokeMethod("AddPrinterConnection", inputParameters, null);
uint errorCode = (uint)outputParameters["ReturnValue"];

Console.WriteLine(errorCode.ToString());
}
catch (Exception exception)
{
Console.WriteLine(exception.Message + "\n" +
exception.InnerException);
}
Console.ReadLine();
}
}
}
}
 
Back
Top