?
Solved

WMI Windows 7 64bit problem

Posted on 2013-06-17
12
Medium Priority
?
832 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

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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