Solved

WMI Windows 7 64bit problem

Posted on 2013-06-17
12
795 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 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