0

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. However when the code is run on Windows XP 64 bit I get an access denied exception.

Here is the C# code, boiled down to a simple form which produces the error:

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; I am not sure how that affects Windows XP 64 bit.

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cdn
  • 11
  • 1

1 Answers1

0

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. How exactly are you doing this on 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 ( i.e. "run as administrator on Windows XP" ).

From the Following Question: Addressing "Access Denied" Exception with WMI Calls

Add a manifest to your program so that it always runs with Admin privileges. Here is another link which is more to the point.

Community
  • 1
  • 1
Security Hound
  • 2,577
  • 3
  • 25
  • 42
  • 1
    Windows XP 64 bit has a context menu item "Run as..." which brings [this window](http://imgur.com/yCMh7). Unchecking the "Run this program with restricted access" or running as the user "Administrator" have no effect. I also have a manifest to run as admin on Vista/7. – cdn Nov 18 '11 at 18:38
  • What you describe is not same feature as "run as administrator" by a long shot. If you run the application as a user with administrator powers does the program complain about the permissions? – Security Hound Nov 18 '11 at 19:00
  • Yes, no matter how I run the application on XP 64 it gives the Access Denied error. – cdn Nov 18 '11 at 19:05
  • This is not an answer. Should be a comment. – David Heffernan Nov 18 '11 at 19:06
  • @DavidHeffernan - I would have loved to make it a comment, had to much to say, I had no other choice. Its also the reason I reported the question, I don't believe in its current state, it can even be answered. – Security Hound Nov 18 '11 at 19:09