ManagementObjectSearcher Win32_Printer - UnauthorizedAccessException

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I recently had to open a project because someone on our production floor did not know how to navigate the menus. When I tried to run it, I got an Unauthorized Access Exception for code that I had not modified.

This query worked before installing VS2008, but now it will not run in a test application under VS2005 either.

My code fails in the foreach loop when the printer is assigned at this line:
(ManagementObject printer in Printers)

Code:
public static void GetPrintersCollection() {
  try {
    string sql = "SELECT * FROM Win32_Printer";
    using (ManagementObjectSearcher query = new ManagementObjectSearcher(sql)) {
      ManagementObjectCollection Printers = query.Get();
      foreach (ManagementObject printer in Printers) {
        Console.WriteLine(printer.Properties["Name"].Value.ToString());
      }
    }
  } catch (UnauthorizedAccessException err) { // err.Message is empty
    Console.WriteLine("You do not have authorization to access this printer.");
  } catch (Exception err) {
    Console.WriteLine(err.Message);
  }
}

Any ideas why?

Help! Please, and Thank You!
 
I have traced the problem down as far as Event Viewer, where the error shows up under the local System tab, and I do not know how to resolve this.

Here is a screen shot of the error:

wmiprvse-Event.gif


The description of the error is as follows (if the image is ever deleted):
Source: DCOM
EventID: 10000
User: NT AUTHORITY\NETWORK SERVICE
Description:
Unable to start a DCOM Server: {1F87137D-0E7C-44D5-8C73-4EFFB68962F2}. The error:
"Access is denied. "
Happened while starting this command:
C:\WINDOWS\system32\wbem\wmiprvse.exe -secured -Embedding

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.
The link, however, is broken on my machine.

Any help in troubleshooting this problem is greatly appreciated.

Regards,
~Joe
 
Has anything else changed apart from installing 2008? One possibility is a change in permissions for your network account.

If you have a good backup of ytour system you could try resetting the machine permissions to the default by typing
Code:
secedit /configure /cfg %windir%\repair\secsetup.inf /db secsetup.sdb /verbose
at a command prompt. Have you tried the executable on any other machine to see if it generates the same error?
 
Last edited:
Back
Top