Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

WMI Windows 7 64bit problem

Posted on 2013-06-17
12
Medium Priority
?
850 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:Meir Rivkin
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:Meir Rivkin
ID: 39256247
is searchPrinters.Get(); returns null?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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:Meir Rivkin
ID: 39258506
put try/catch around the code and post the exception and inner exception if avbailable.
0
 
LVL 42

Expert Comment

by:Meir Rivkin
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:Meir Rivkin
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

Technology Partners: 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!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

618 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