473,847 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

WMI - AddPrinterConne ction failing to add network printer

2 New Member
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.Invoke Method 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 AddPrinterConne ction 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,ipAddre ssOfClientPC, 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.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Management;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  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";
  16. string ipAddressOfClientPC = "";
  17. ManagementScope scope = new ManagementScope("\\\\" + ipAddressOfClientPC + "\\root\\cimv2");
  20. scope.Options.Username = username;
  21. scope.Options.Password = password;
  22. scope.Options.Impersonation = ImpersonationLevel.Impersonate;
  24. scope.Connect();
  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");
  34. string sharedPrinterAddress = "\\\\\\printer2";
  35. inputParameters["Name"] = sharedPrinterAddress;
  37. ManagementBaseObject outputParameters = mgtClass.InvokeMethod("AddPrinterConnection", inputParameters, null);
  38. uint errorCode = (uint)outputParameters["ReturnValue"];
  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. }
Nov 13 '07 #1
1 9642
2 New Member
It seems that AddPrinterConne ction 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,Pri ntUIEntry /? 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

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

Similar topics

by: Maileen | last post by:
Hi, I have 2 questions : 1. - I want to print from my ASP page directly to my network printer. For this i use the following code, but everytime, my document to print is sent to local and physical port LPT1 not to network printer :( '---- to print report on network printer ---
by: ARaman | last post by:
How does one add a network printer on Windows 98 and Me? WNetAddConnection2 seems to pass,but the UI does not update to show the printer is installed. On NT systems,AddPrinterConnection method works fine. Is there a similar method that will work on 98/Me? Thanks in advance.
by: KohlerTommy | last post by:
In my application I need to give the user the ability to print duplex if the selected printer supports duplex printing. Many of the printer options do not make much sense in my application, and many of the settings in the common printer dialog would have a negative impact on my printing process. To handle this strict printing constraint on which my application imposes I do not want to show the common print dialog. I want to be able to...
by: Ken | last post by:
Hello Everyone, I think that there is no way to add a network printer in .Net. I have tried ,as an alternative, using windows API AddPrinter. This was not successful. Does anybody have a possible solution in .NET? Thank You in Advance for Your Help, Ken
by: Maileen | last post by:
Hi, I want to print from my ASP page directly to my network printer. For this i use the following code, but everytime, my document to print is sent to local and physical port LPT1 :( could you help me please ? thx, Maileen. '---- to print report on network printer --- ' RSP = stored procedure
by: Tessa | last post by:
Is there any security reason why you cannot print to a network printer from ASP.NET under IIS6 on Windows 2003 server? I'm using ASP.NET code to print to a server print queue using PrintDocument.Print() (.NET framework v 1.1) I can print to a local printer plugged into LPT1 on the web server, but not to a network printer. The same printing code to a network printer works in a .NET web app when
by: Dawn | last post by:
My application is coded to create a report using Excel and I would like to send it directly to a pre-determined network printer. (My intention is to pass the printer name in a variable.) However, there is a suffix that is added to the printer name and the printer suffix is not consistent ...... "on Ne00" on my pc "on Ne01" on another pc Is there a way to consistently code the printer name ? mxlApp.ActivePrinter = "Dell Laser...
by: jigsmshah | last post by:
i am working on a project (windows service using VB.Net and C#) which gets the check details and check images from the database and prints the check to a printer. Printer name is configured in a XML file. Printer names are names which are available to the system on which the service is running. Printers used are network printers and are added using Printers and Faxes -> Add a printer. To validate the printer I am using the following...
by: jawaid alam | last post by:
Hi all forum Member, I am facing very tropical type of problem. I devoloped a intranet web project. I want to print crystal report by selecting network printer. I accessed all network printer in dropdownlist in crystal report page. when i run crystal report page directly by set as startup page. Dropdownlist shows all network printer. but when i run my project through IIS Dropdownlist shows only localhost printers. its not showing
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.