Win32_Printer方法是否从服务器对本地计算机起作用

本文关键字:计算机 起作用 服务器 Printer 方法 是否 Win32 | 更新日期: 2023-09-27 18:26:50

我最近制作了一个使用WCF服务的c#打印机管理工具,该服务包含WMI Win32_printer方法,如AddPrinterConnection和SetDefaultPrinter。

如果客户端和WCF服务都在同一台计算机上运行,一切都会正常工作。但是,如果我试图在另一台机器上移动WCF服务,方法会停止工作,但不会崩溃。

例如,AddPrinterConnection返回0,这意味着成功,但没有在本地计算机上真正添加打印机。默认打印机即使设置为默认也会返回false等。打印机列表是可以访问的。

WCF服务的行为是相同的,即使托管在具有应用程序池管理员标识的IIS中,或者托管在服务器上的ASP开发服务器中。

但在本地机器上有相同的身份,一切都很好。打印机名称和本地计算机地址作为参数发送到WCF方法。

这是权利、授权还是冒充问题?这是WMI的限制吗?有人遇到这个问题吗?我真的需要一个解决方案。提前谢谢。

EDIT:以下是一些代码,表示在添加打印机的WCF服务上调用的函数。

注意:在本地安装(客户端和服务本地)和远程安装(客户端本地,服务远程)上,函数都返回0,这意味着"成功"且没有错误,但在第二种情况下,实际上没有添加任何内容。

我也尝试了各种模拟,正如我所说,WCF服务运行的身份与使用客户端的用户相同。我认为这是一个与用户上下文有关的东西,因为它没有多大意义。我试图从微软那里找到一些关于这方面的信息,但没有成功。

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";
        }
    }

Win32_Printer方法是否从服务器对本地计算机起作用

经过一些研究,我发现在第三台机器上不可能做到这一点。必须使用本地登录完成。