Solved

WMI Windows 7 64bit problem

Posted on 2013-06-17
12
803 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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