Solved

WMI Windows 7 64bit problem

Posted on 2013-06-17
12
785 Views
Last Modified: 2013-06-28
string searchQuery = "SELECT * FROM Win32_Printer";

                ManagementObjectSearcher searchPrinters = new ManagementObjectSearcher("root\\CIMV2", searchQuery);

                ManagementObjectCollection printerCollection = searchPrinters.Get();

                foreach (ManagementObject printer in printerCollection)
                {
                    if (printer.Properties["Name"].Value.ToString() == pPrinterName)
                    {
                        switch (pMode)
                        {
                            case PrinterStatusMode.Pause:
                                printer.InvokeMethod("Pause", null);
                                break;
                            default:        //PrinterStatusMode.Resume
                                printer.InvokeMethod("Resume", null);
                                break;
                        }
                        retVal = true;
                        break;
                    }

                }

above could works fine on Windows XP, bit generates COMError on Windows 7 64bit.  How can I resolve this error?
0
Comment
Question by:raycclayton
  • 7
  • 5
12 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39255790
which line throws the error?
0
 

Author Comment

by:raycclayton
ID: 39256226
foreach (ManagementObject printer in printerCollection)

throws the error because I seem to have a bad count when I do this line:

ManagementObjectCollection printerCollection = searchPrinters.Get();
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39256247
is searchPrinters.Get(); returns null?
0
 

Author Comment

by:raycclayton
ID: 39257379
printerCollection is not null.

In debug the printerCollection has an error in the count

-            Count      'printerCollection.Count' threw an exception of type 'System.Management.ManagementException'      int {System.Management.ManagementException}
0
 

Author Comment

by:raycclayton
ID: 39258376
I do have Microsoft.Office.Interops if that matters
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39258506
put try/catch around the code and post the exception and inner exception if avbailable.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 42

Expert Comment

by:sedgwick
ID: 39259069
try this code:
ManagementScope scope = new ManagementScope(@"\\" + Environment.MachineName + @"\root\cimv2");
            scope.Connect();

            SelectQuery searchQuery = new SelectQuery("SELECT * FROM Win32_Printer");
            ManagementObjectSearcher searchPrinters = new ManagementObjectSearcher(scope, searchQuery);

            ManagementObjectCollection printerCollection = searchPrinters.Get();

            foreach (ManagementObject printer in printerCollection)
            {
                 if (printer.Properties["Name"].Value.ToString() == pPrinterName)
                    {
                        switch (pMode)
                        {
                            case PrinterStatusMode.Pause:
                                printer.InvokeMethod("Pause", null);
                                break;
                            default:        //PrinterStatusMode.Resume
                                printer.InvokeMethod("Resume", null);
                                break;
                        }
                        retVal = true;
                        break;
                    }

            }

Open in new window

0
 

Author Comment

by:raycclayton
ID: 39259499
the above code will product this error:
'printerCollection.Count' threw an exception of type 'System.Runtime.InteropServices.COMException'

I have test the above code on another Windows 7 64bit PC and it works.  How can I repair the System.Runtime.InteropServices.  I think mine my be corrupt in some way.
0
 

Author Comment

by:raycclayton
ID: 39260430
When I get the exception the errorcode is -2147023170
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39261827
check what is the version of System.Runtime.InteropServices dll?
0
 

Accepted Solution

by:
raycclayton earned 0 total points
ID: 39270224
I went into the WMI management and after that the errors went away.
0
 

Author Closing Comment

by:raycclayton
ID: 39283801
it worked
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now