How to print a test page and capture the return value for a non default printer on a remote server using WMI's PrintTestPage method?

故事扮演 提交于 2019-12-12 05:36:12

问题


I'm using the System.Management namespace along with WMI to install printers/ports on a remote server. Once the printer/port is installed, I'd like to print a test page, capture whether or not printing the test page was (theoretically) successful, and display the result to the user. I've got everything working up until the point of printing the test page.

I've seen this article, as well as this article, on the subject, but neither one quite seems to be doing the trick because I'm not seeing how to apply them to the current printer I've just installed.

Anyway, here's the code I'm working with to install the printer (the port code is similar and probably not necessary...there's also additional code I'm using to log in to the remote server, but as long as you know that I'm creating a ManagementScope object with a ConnectionOptions object attached, there's no reason to show that code either):

foreach (Printers p in lstPrinters)
{
    search = printerQuery + "'" + p.PrinterName + "'";

    query = new SelectQuery(search);

    try
    {
        using (var searcher = new ManagementObjectSearcher(scope, query))
        {
            ManagementObjectCollection printers = searcher.Get();

            ObjectGetOptions oPrinterOptions =
                new ObjectGetOptions(null, TimeSpan.MaxValue, true);
            ManagementPath mPathPrinter =
                new ManagementPath("Win32_Printer");
            ManagementClass mClassPrinter =
                new ManagementClass(scope, mPathPrinter, oPrinterOptions);
            ManagementObject mNewPrinter = mClassPrinter.CreateInstance();

            mNewPrinter["Comment"] = p.Comment;
            mNewPrinter["DeviceID"] = p.DeviceID;
            mNewPrinter["DriverName"] = p.DriverName;
            mNewPrinter["Location"] = p.Location;
            mNewPrinter["Network"] = p.Network;
            mNewPrinter["PortName"] = p.PortName;
            mNewPrinter["Shared"] = p.Shared;
            mNewPrinter["ShareName"] = p.ShareName;

            // If no ports are returned, add the port.
            // Otherwise, update the current port object.
            PutOptions poPutPrinter = new PutOptions();
            poPutPrinter.Type = PutType.UpdateOrCreate;
            poPutPrinter.UseAmendedQualifiers = true;
            mNewPrinter.Put(poPutPrinter);
        }
    }
}

I thought I would be able to do something like this...which is similar to an example in second article link I listed above:

ManagementBaseObject retValue = mNewPrinter.InvokeMethod("PrintTestPage", null);

if (Convert.ToInt32(retValue["ReturnValue"]) == 0)
{
    MessageBox.Show("Test page printed successfully.");
}

In theory, at least in my head, the above would ensure that, by assigning my mNewPrinter ManagementObject variable to the InvokeMethod, the PrintTestMethod is linked to that printer on that specific server.

Thanks...


回答1:


I think I solved my own question using the following:

ManagementBaseObject outParams =
    mNewPrinter.InvokeMethod("PrintTestPage", null, null);

if (Convert.ToInt32(outParams["returnValue"]) == 0)
{
    MessageBox.Show("Test page printed successfully.");
}

For some reason, I had to add the extra null to the mNewPrinter.InvokeMethod("PrintTestPage, null). Otherwise, it was returning an error.



来源:https://stackoverflow.com/questions/16677034/how-to-print-a-test-page-and-capture-the-return-value-for-a-non-default-printer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!