I recently made a c# printer management tool that uses a WCF service which contains WMI Win32_Printer methods like AddPrinterConnection and SetDefaultPrinter.

Everything works fine if both the client and the WCF service run on the same machine. But if i try to move the WCF service on another machine, the methods stop working but nothing crashes.

The AddPrinterConnection for example returns 0 which means success but no printer is really added on the local machine. The default printer even if is set to default returns false, etc. The printers list is somehow accesible.

The WCF service behaves the same even if is hosted in IIS with an administrator identity for the application pool or hosted in ASP development server on the server.

But with the same identity on the local machine all is fine. The printer name and local machine address are sent as parameters to the WCF methods.

Is this a rights, delegation or impersonation issue? Is this a limitation of the WMI? Does anyone encountered this problem?

Here is some code (note that the method returns 0(success)in both cases:local instalation and remote one but in the second case nothing really happens):

public static string AddPhysicalPrinter(string sPrinterName, string address)
    {
        try
        {
            ConnectionOptions options = new ConnectionOptions();
            options.Impersonation = ImpersonationLevel.Impersonate;
            options.Authentication = AuthenticationLevel.PacketPrivacy;
            options.EnablePrivileges = true;
            options.Username = "username";
            options.Password = "password";

            oManagementScope = new ManagementScope(new ManagementPath("\\\\" + address + "\\root" + "\\cimv2"), options);

            oManagementScope.Connect();

            ManagementClass oPrinterClass = new ManagementClass (new ManagementPath("Win32_Printer"), null);
            ManagementBaseObject oInputParameters = oPrinterClass.GetMethodParameters("AddPrinterConnection");

            oInputParameters.SetPropertyValue("Name", sPrinterName);

           ManagementBaseObject x = oPrinterClass.InvokeMethod("AddPrinterConnection", oInputParameters, null);

            foreach(PropertyData p in x.Properties)
            {
                switch ((UInt32)p.Value)
                {
                    case 0:
                        return "has been added successfuly";//success
                    case 5:
                        return "access denied";//access denied
                    case 1801:
                        return "invalid printer name";//invalid printer name
                    case 1930:
                        return "incompatible printer driver";//incompatible printer driver
                    default:
                        return "unknown error";
                }
            }

            return "unknown error";
        }
        catch (Exception ex)
        {
            return "exception caught";
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.