views:

298

answers:

1

First, this code worked in VS2005.

The code in VS2008 crashes with an UnauthorizedAccessException whenever I try stepping over the foreach loop and assign a ManagementObject.

public static List<string> GetPrintersCollection() {
  if (printers == null) {
    printers = new List<string>();
    string searchQuery = "SELECT * FROM Win32_Printer";
    try {
      using (ManagementObjectSearcher searchPrinters = new ManagementObjectSearcher(searchQuery)) {
        ManagementObjectCollection Printers = searchPrinters.Get(); // <= Printers data below
        foreach (ManagementObject printer in Printers) { // <= Error Here
          printers.Add(printer.Properties["Name"].Value.ToString());
        }
      }
    } catch (UnauthorizedAccessException err) {
      Console.WriteLine(err.Message); // the message is an empty string
      throw new Exception("PrinterSpool - GetPrintersCollection: You do not have authorization to access this printer.");
    } catch (Exception err) {
      throw new Exception(string.Format("PrinterSpool - GetPrintersCollection: {0}", err.Message));
    }
  }
  return printers;
}

StackTrace =
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
at AcpClasses.PrinterSpool.GetPrintersCollection()...

When I try to view the data in the Locals window, these fields look immediately suspect:
1. Count - Value: "Function evaluation timed out."
2. IsSynchronized - Value: Function evaluation disabled because a previous function evaluation timed out.
3. SyncRoot - Value: Function evaluation disabled because a previous function evaluation timed out.

How can I try to debug this further to find out what is going on?

A: 

To debug further you need to inspect Printers before entering the foreach loop.

  1. Put a breakpoint on the searchPrinters.Get() line.
  2. Press F10
  3. Now inspect your item.

Another thing that should help is changing err.Message to err.ToString().

For what it's worth, I debugged this with no problems running both VS 2005 & 2008 on XP.

EDIT: Posting code for you to try.

public static List<string> GetPrintersCollection()
{
    if (printers == null)
    {
        printers = new List<string>();
        string searchQuery = "SELECT * FROM Win32_Printer";
        try
        {
            using (ManagementObjectSearcher searchPrinters = new ManagementObjectSearcher(searchQuery))
            {
                ManagementObjectCollection Printers = searchPrinters.Get(); // <= Printers data below
                foreach (ManagementObject printer in Printers)
                {
                    printers.Add(printer.Properties["Name"].Value.ToString());
                }
            }
        }
        catch (UnauthorizedAccessException err)
        {
            //Log & re-throw
            Console.WriteLine("Caught UnauthorizedAccessException:  " + err.ToString()); 
            throw;  //re-throw existing exception, not a new one
        }
        //there's no reason to catch the plain-old Exception 
    }

    return printers;
}

EDIT: The only other thing I can think of is to manually go through your Printers and Faxes to see if any are causing problems for you.

Austin Salonen
- Printers {System.Management.ManagementObjectCollection} System.Management.ManagementObjectCollection'+' Count 'Printers.Count' threw an exception of type 'System.UnauthorizedAccessException'
jp2code
Meanwhile, the err.ToString() gave this data: "PrinterSpool - GetPrintersCollection: You do not have authorization to access this printer.\r\nSystem.UnauthorizedAccessException\r\n at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)\r\n at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()\r\n at win32_printer.Program.GetPrintersCollection()
jp2code
Does the information above help any? I don't know what to do with it. Obviously, I am an admin on my own machine, but I also want this application to work for others (i.e. non-admins) on other machines.*** (NOTE: Sorry for posting this reply in multiple comments, but SO seems to impose a limit on the comment length, and I don't know how to otherwise respond to a message.) ***
jp2code
I've posted some code for you to try. From your second comment, you were posting the content of *your* exception handling which I believe is partially preventing you from solving your problem.
Austin Salonen
Same error Mr. Salonen: ***"Caught UnauthorizedAccessException: System.UnauthorizedAccessException\r\n at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)\r\n at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()\r\n at win32_printer.Program.GetPrintersCollection2() in C:\\Shared\\jp2Testing\\win32_printer\\Program.cs:line 57"***
jp2code
In WinXP, under "Printers and Faxes", I can open all printers listed and print a test page without a problem. Under Users, I am listed as an Administrator.
jp2code
I'm out of ideas for today @jp2code... Sorry.
Austin Salonen