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.