469,579 Members | 1,899 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

WMI - AddPrinterConnection failing to add network printer

2
Hi guys,

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 below.

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

1) If I specifiy the ip address of the test pc in the ManagementScope object does 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 ?

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. However, 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) I have used a similar approach for adding printer via specifying the printer driver, port etc and it works. So I believe my approach is correct in WMI.

I used the WMI code generated by the WMI Code Generator, http://www.microsoft.com/downloads/d...4-55bbc145c30e,

I have added my code below.

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

thanks
V
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Management;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5.  
  6. namespace SampleApp
  7. {
  8. public class AddPrinterConnection
  9. {
  10. public static void Main()
  11. {
  12. //only required if required to access another machine with different username and password
  13. string username = "administrator";
  14. string password = "password";
  15.  
  16. string ipAddressOfClientPC = "192.168.0.22";
  17. ManagementScope scope = new ManagementScope("\\\\" + ipAddressOfClientPC + "\\root\\cimv2");
  18.  
  19.  
  20. scope.Options.Username = username;
  21. scope.Options.Password = password;
  22. scope.Options.Impersonation = ImpersonationLevel.Impersonate;
  23.  
  24. scope.Connect();
  25.  
  26. ManagementPath path = new ManagementPath("Win32_Printer");
  27. ManagementClass mgtClass = new ManagementClass(scope, path, null);
  28. using (mgtClass)
  29. {
  30. try
  31. {
  32. ManagementBaseObject inputParameters = mgtClass.GetMethodParameters("AddPrinterConnection");
  33.  
  34. string sharedPrinterAddress = "\\\\192.168.0.1\\printer2";
  35. inputParameters["Name"] = sharedPrinterAddress;
  36.  
  37. ManagementBaseObject outputParameters = mgtClass.InvokeMethod("AddPrinterConnection", inputParameters, null);
  38. uint errorCode = (uint)outputParameters["ReturnValue"];
  39.  
  40. Console.WriteLine(errorCode.ToString());
  41. }
  42. catch (Exception exception)
  43. {
  44. Console.WriteLine(exception.Message + "\\n" + exception.InnerException);
  45. }
  46. Console.ReadLine();
  47. }
  48. }
  49. }
  50.  
Nov 13 '07 #1
1 9056
ekynox
2
It seems that AddPrinterConnection does not have the capacity to add a network printer to a remote pc. The alternative solution would be to utilise the rundll32 printui.dll,PrintUIEntry /? via DOS.

This commandline mechanism can be easily integrated into managed code in non-interactive mode.

Refer to Rick Strahl's blog:http://west-wind.com/weblog/posts/1440.aspx

Hope this is of some help.
Nov 16 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by ARaman | last post: by
reply views Thread by KohlerTommy | last post: by
1 post views Thread by Maileen | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.