简体   繁体   English

在Windows XP 64位上使用WMI添加以太网打印机端口时,访问被拒绝

[英]Access denied when adding an Ethernet printer port with WMI on Windows XP 64 bit

I've been writing a printer driver installer. 我一直在写打印机驱动程序安装程序。 Among other things it can create an Ethernet printer, which means I need to create an Ethernet printer port. 它可以创建一个以太网打印机,这意味着我需要创建一个以太网打印机端口。

The code I've used to create the port (see below) works fine on Windows XP 32 bit, Windows Vista 32 & 64 bit and Windows 7 32 & 64 bit. 我用来创建端口的代码(请参见下文)在Windows XP 32位,Windows Vista 32和64位以及Windows 7 32和64位上工作正常。 However when the code is run on Windows XP 64 bit I get an access denied exception. 但是,当代码在Windows XP 64位上运行时,出现拒绝访问异常。

Here is the C# code, boiled down to a simple form which produces the error: 这是C#代码,归结为一个简单的形式,会产生错误:

static void Main(string[] args)
{
    ManagementClass portClass = new ManagementClass("Win32_TCPIPPrinterPort");
    ManagementObject portObject = portClass.CreateInstance();

    portObject["Name"] = "TestPort";
    portObject["HostAddress"] = "172.16.2.78";
    portObject["PortNumber"] = 9100;
    portObject["Protocol"] = 1;
    portObject["SNMPEnabled"] = false;

    PutOptions options = new PutOptions();
    options.Type = PutType.UpdateOrCreate;
    try
    {
        portObject.Put(options);
    }
    catch (ManagementException e)
    {
        Console.WriteLine("ManagementException: " + e.Message);
    }
}

When running the program I am logged in as administrator, and I've also tried right clicking and doing a "Run as" administrator, but it doesn't have an effect. 运行该程序时,我以管理员身份登录,并且我也尝试过右键单击并以“运行方式”管理员身份进行操作,但这没有任何效果。 I also have a manifest to force the application to run as admin in Windows Vista/7; 我也有一个清单,可以强制应用程序在Windows Vista / 7中以管理员身份运行; I am not sure how that affects Windows XP 64 bit. 我不确定这会如何影响Windows XP 64位。

Most of the information I've found online so far deals with using WMI on a remote machine, but all of this is for the local machine. 到目前为止,我在网上找到的大多数信息都涉及在远程计算机上使用WMI,但是所有这些都是针对本地计算机的。

When running the program I am logged in as administrator and I've also tried right clicking and doing a "Run as" administrator but it doesn't have an effect. 运行该程序时,我以管理员身份登录,并且我还尝试过右键单击并以“运行身份”管理员身份进行操作,但这没有任何效果。

This context menu item was not added until Windows Vista. 直到Windows Vista才添加此上下文菜单项。 How exactly are you doing this on Windows XP? 您在Windows XP上的操作情况如何? Have you tried to adjust the manfest file? 您是否尝试过调整清单文件?

I wish I could actually provide an answer, but your question is missing important details and/or makes a statement that makes no sense ( ie "run as administrator on Windows XP" ). 我希望我能提供一个答案,但是您的问题缺少重要的细节和/或做出了没有意义的声明(即“以Windows XP管理员身份运行”)。

From the Following Question: Addressing "Access Denied" Exception with WMI Calls 来自以下问题: 解决WMI调用的“访问被拒绝”异常

Add a manifest to your program so that it always runs with Admin privileges. 将清单添加到您的程序,以便它始终以管理员权限运行。 Here is another link which is more to the point. 是另一个更重要的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM