tags:

views:

560

answers:

2

I'm running into an odd issue retrieving printer port addresses. When I get all the entries in Win32_TCPIPPrinterPort, the HostAddress field (which should have the IP address) is usually blank/null, only the port name has a value. To make it a bit stranger, if a particular port is not in use by any printer, THEN the HostAddress will have the the proper value.

The C# code is simple, and results in something like this; IP_192.168.1.100, printerportxyz,

richTextBox1.Clear();
ManagementObjectSearcher portSearcher = new ManagementObjectSearcher("root\\CIMV2",
    "SELECT * FROM Win32_TCPIPPrinterPort");
foreach (ManagementObject port in portSearcher.Get())
{
    richTextBox1.AppendText(
        String.Format("Name: {0} HostAddress: {1}",
            port["Name"],
            port["HostAddress"])
        );
}

I also tried the same thing in WSH/VBS, and saw the same behavior.

+1  A: 

I ended up having to re-visit this, and making another attempt. I found that the built-in prnport.vbs managment script had no issues - looking into it I saw that while establishing its WMI connection it had oService.Security_.Priveleges.AddAsString "SeLoadDriverPrivilege"

the solution in C# ended up specifying the WMI ConnectionOptions and setting EnablePrivileges to true. Then the HostAdress was no longer null for unused or in-use ports.

ConnectionOptions connOptions = new ConnectionOptions();
connOptions.EnablePrivileges = true;

ManagementScope mgmtScope = new ManagementScope("root\\CIMV2", connOptions);
mgmtScope.Connect();

ObjectQuery objQuery = new ObjectQuery("SELECT * FROM Win32_TCPIPPrinterPort");
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(mgmtScope, objQuery);

foreach (ManagementObject mo in moSearcher.Get())
{
    Console.WriteLine(String.Format("PortName={0} HostAddress={1}", mo["Name"], mo["HostAddress"]));
}
Anthony
A: 

thanks for solution

This solution is working for Win32_TCPIPPrinterPort wmi object protperty data is null value.

Fatih
@Faith: Welcome to SO. Please read the [FAQ](http://stackoverflow.com/faq). You'll see that this is not a discussion forum, and we don't "reply to the thread".
John Saunders