Warm tip: This article is reproduced from serverfault.com, please click

AddPrinterConnection with Win32_Printer fails

发布于 2020-03-16 09:43:57

I have a simple programm that adds a specific printer connection to the server and prints on that printer a pdf document. After printing the document, the printer connection will be deleted.

Now everything works fine when I launch the app as my own admin-account. But when I launch the app with a service user-account, I can't add the printer connection. It fails with an error code 87: INVALID_PARAMETER

Note: If the printer connection on the machine (server) is available, then I can print with the service user-account. Only adding a printer connection, that does not exists, does not work.

Here is the code snippet:

    private static ManagementScope oManagementScope = null;

    public static bool AddPrinter(string printerName)
    {
        var result = false;
        try
        {
            oManagementScope = new ManagementScope(ManagementPath.DefaultPath);
            oManagementScope.Connect();

            using (ManagementClass win32Printer = new ManagementClass("Win32_Printer"))
            {
                using (ManagementBaseObject inputParam = win32Printer.GetMethodParameters("AddPrinterConnection"))
                {
                    inputParam.SetPropertyValue("Name", printerName);

                    using (ManagementBaseObject methodResult = win32Printer.InvokeMethod("AddPrinterConnection", inputParam, null))
                    {
                        uint errorCode = (uint)methodResult.Properties["returnValue"].Value;

                        switch (errorCode)
                        {
                            case 0:
                                Logger.LogInfo("Successfully connected printer.");
                                result = true;
                                break;
                            case 5:
                                Logger.LogError("Access Denied.");
                                break;
                            case 123:
                                Logger.LogError("The filename, directory name, or volume label syntax is incorrect.");
                                break;
                            case 1801:
                                Logger.LogError("Invalid Printer Name: " + printerName);
                                break;
                            case 1930:
                                Logger.LogError("Incompatible Printer Driver.");
                                break;
                            case 3019:
                                Logger.LogError("The specified printer driver was not found on the system and needs to be downloaded.");
                                break;
                            default:
                                Logger.LogError("Could not add the specific printer, Error Code: " + errorCode);
                                break;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Logger.LogError(ex.Message + ", " + ex.InnerException?.Message);
        }

        return result;
    }

We already tried to add the same permissions on the service user, as I have on my admin-account. I still get the error code 87 while adding a printer connection.

As I have researched a bit about the error code 87, I found out that it means something like INVALID PARAMETER. But I can't figure out which parameter should be invalid, and why does it work on the server when I launch it with my admin-account?

Any help is welcome

Thanks

Questioner
Leon
Viewed
0
Leon 2020-12-01 15:53:53

We have finally found a solution for this problem even if it's not the best practice it works fine now.

As I mentioned above we had a service account that should install the printers dynamically but somehow this does not work. So insted of creating a service user, we just created a normal user and it suddently worked.

The disatvantage of this solution is, that this pseudo user must always be logged in on the server. Otherwise adding printers won't work with our solution.