Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. WMI Access

WMI Access

Scheduled Pinned Locked Moved Visual Basic
csharpcomquestionlounge
13 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W wakkerjack

    using threading with C# and ManagementObjectSearcher Object , i get a "random" COM Exception scanning XP and NT.. Any one have a clue??

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #2

    Not without seeing your code. BTW: This is the VB.NET Forum, not C#... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

    W 1 Reply Last reply
    0
    • D Dave Kreskowiak

      Not without seeing your code. BTW: This is the VB.NET Forum, not C#... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      W Offline
      W Offline
      wakkerjack
      wrote on last edited by
      #3

      oh soz didn't realize.. but maybe u can still help me? Ive been battling with this prob for a LONG time now.. Basically- i use: private ManagementObjectSearcher m_MOSearcher; private ManagementScope m_ManScope; private ConnectionOptions m_ConnectOptions; m_ConnectOptions = new ConnectionOptions (); m_ConnectOptions.Username = "user"; m_ConnectOptions.Password = "pass"; m_ConnectOptions.Authentication = AuthenticationLevel.Call; m_ConnectOptions.Impersonation = ImpersonationLevel.Impersonate; m_ConnectOptions.EnablePrivileges = true; m_ConnectOptions.Locale = "MS_409"; m_ConnectOptions.Timeout = new TimeSpan (0,0,0,0,5); string strConnection = "\\\\"+ "192.168.0.88" + "\\root\\cimv2"; m_ManScope = new ManagementScope(strConnection, m_ConnectOptions); ObjectQuery Temp = new ObjectQuery("SELECT " + "Size" + " FROM Win32_" + "DiskDrive"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("Size: " +mo["Size"].ToString ()); } Temp = new ObjectQuery("SELECT " + "Model" + " FROM Win32_" + "DiskDrive"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("Model: " +mo["Model"].ToString ()); } And thread it with: for (int i=0; i< 250; i++) { Thread a = new Thread(new ThreadStart(Function)); a.Start(); } i get a rpc error or COm exception sometimes? maybe deadlock?

      D 1 Reply Last reply
      0
      • W wakkerjack

        oh soz didn't realize.. but maybe u can still help me? Ive been battling with this prob for a LONG time now.. Basically- i use: private ManagementObjectSearcher m_MOSearcher; private ManagementScope m_ManScope; private ConnectionOptions m_ConnectOptions; m_ConnectOptions = new ConnectionOptions (); m_ConnectOptions.Username = "user"; m_ConnectOptions.Password = "pass"; m_ConnectOptions.Authentication = AuthenticationLevel.Call; m_ConnectOptions.Impersonation = ImpersonationLevel.Impersonate; m_ConnectOptions.EnablePrivileges = true; m_ConnectOptions.Locale = "MS_409"; m_ConnectOptions.Timeout = new TimeSpan (0,0,0,0,5); string strConnection = "\\\\"+ "192.168.0.88" + "\\root\\cimv2"; m_ManScope = new ManagementScope(strConnection, m_ConnectOptions); ObjectQuery Temp = new ObjectQuery("SELECT " + "Size" + " FROM Win32_" + "DiskDrive"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("Size: " +mo["Size"].ToString ()); } Temp = new ObjectQuery("SELECT " + "Model" + " FROM Win32_" + "DiskDrive"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("Model: " +mo["Model"].ToString ()); } And thread it with: for (int i=0; i< 250; i++) { Thread a = new Thread(new ThreadStart(Function)); a.Start(); } i get a rpc error or COm exception sometimes? maybe deadlock?

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #4

        Your code never connects to the management source on the target machine. You need

        m_ConnectOptions.Locale = "MS_409";
        m_ConnectOptions.Timeout = new TimeSpan (0,0,0,0,5);
         
        string strConnection = "\\\\"+ "192.168.0.88" + "\\root\\cimv2";
         
        m_ManScope = new ManagementScope(strConnection, m_ConnectOptions);
         
        m_ManScope.Connect();
         
        ObjectQuery Temp = new ObjectQuery("SELECT " + "Size" + " FROM Win32_" + "DiskDrive");
        m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp);

        Also, you don't have to perform two queries to get the result you want. One will suffice:

        ObjectQuery Temp = new ObjectQuery("SELECT Size, Model FROM Win32_DiskDrive");
        m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp);

        foreach(ManagementObject mo in m_MOSearcher.Get())
        {
        Console.WriteLine("Size: " +mo["Size"].ToString ());
        Console.WriteLine("Model: " +mo["Model"].ToString ());
        }

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        W 1 Reply Last reply
        0
        • D Dave Kreskowiak

          Your code never connects to the management source on the target machine. You need

          m_ConnectOptions.Locale = "MS_409";
          m_ConnectOptions.Timeout = new TimeSpan (0,0,0,0,5);
           
          string strConnection = "\\\\"+ "192.168.0.88" + "\\root\\cimv2";
           
          m_ManScope = new ManagementScope(strConnection, m_ConnectOptions);
           
          m_ManScope.Connect();
           
          ObjectQuery Temp = new ObjectQuery("SELECT " + "Size" + " FROM Win32_" + "DiskDrive");
          m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp);

          Also, you don't have to perform two queries to get the result you want. One will suffice:

          ObjectQuery Temp = new ObjectQuery("SELECT Size, Model FROM Win32_DiskDrive");
          m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp);

          foreach(ManagementObject mo in m_MOSearcher.Get())
          {
          Console.WriteLine("Size: " +mo["Size"].ToString ());
          Console.WriteLine("Model: " +mo["Model"].ToString ());
          }

          RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          W Offline
          W Offline
          wakkerjack
          wrote on last edited by
          #5

          Hey thanks for the timely response!! ah soz i forgot to paste that in!!(manscope.connect) sorry! plz if u could try-> just request a whole lot of values from different tables.. and use threads (a lot).. and if u dont get an error maybe i can send u my program:) 2) Is there any windows setting(using XP) that can affect the WMI? I am unable to connect to some computers?? Thanks so much!

          D 1 Reply Last reply
          0
          • W wakkerjack

            Hey thanks for the timely response!! ah soz i forgot to paste that in!!(manscope.connect) sorry! plz if u could try-> just request a whole lot of values from different tables.. and use threads (a lot).. and if u dont get an error maybe i can send u my program:) 2) Is there any windows setting(using XP) that can affect the WMI? I am unable to connect to some computers?? Thanks so much!

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #6

            There are no settings that can affect WMI. I can't get the ManagementObjectSearcher class to work properly at all, either quering the local machine or a remote WMI namespace. The .Get() method works and returns, but the results are unreadable. I get a "COM object that has been separated from its underlying RCW can not be used." error in a Unhandled exception of type 'System.Management.ManagementException' occurred in system.windows.forms.dll when I try to get an enumerator for the returned object collection (ManagementObjectCollection.) But the entire WMI search code is wrapped in a Try/Catch block. This error is getting back to the message pump unhandled for some reason. It looks like a bug in the .NET Framework, but I can't find any information on it. I also can't get a complete list of bug fixes in the upcomming Service Pack 1 for the .NET Framework 1.1 and SP3 for the .NET Framework 1.0. There are a LOT of bug fixes that don't have Knowledge Base articles for them yet... Until the service pack comes out, I don't have a way around this problem. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            W 1 Reply Last reply
            0
            • D Dave Kreskowiak

              There are no settings that can affect WMI. I can't get the ManagementObjectSearcher class to work properly at all, either quering the local machine or a remote WMI namespace. The .Get() method works and returns, but the results are unreadable. I get a "COM object that has been separated from its underlying RCW can not be used." error in a Unhandled exception of type 'System.Management.ManagementException' occurred in system.windows.forms.dll when I try to get an enumerator for the returned object collection (ManagementObjectCollection.) But the entire WMI search code is wrapped in a Try/Catch block. This error is getting back to the message pump unhandled for some reason. It looks like a bug in the .NET Framework, but I can't find any information on it. I also can't get a complete list of bug fixes in the upcomming Service Pack 1 for the .NET Framework 1.1 and SP3 for the .NET Framework 1.0. There are a LOT of bug fixes that don't have Knowledge Base articles for them yet... Until the service pack comes out, I don't have a way around this problem. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              W Offline
              W Offline
              wakkerjack
              wrote on last edited by
              #7

              Here's my code.. works for me.. Excuse the repertition, i just do this for complexities sake:) some RPC should occur if u use the threads... using System; using System.Threading; using System.Management; namespace SimpleTRY { public class Class1 { private ManagementObjectSearcher m_MOSearcher; private ManagementScope m_ManScope; private ConnectionOptions m_ConnectOptions; public Class1() { for (int i=0; i< 250; i++) { /*Thread a = new Thread(new ThreadStart(this.connect)); a.Start();*/ this.connect(); } } public void connect() { string ipAddress = "192.168.0.88"; m_ConnectOptions = new ConnectionOptions (); m_ConnectOptions.Username = "Gorilla"; m_ConnectOptions.Password = "rock"; m_ConnectOptions.Authentication = AuthenticationLevel.Call; m_ConnectOptions.Impersonation = ImpersonationLevel.Impersonate; m_ConnectOptions.EnablePrivileges = true; m_ConnectOptions.Locale = "MS_409"; m_ConnectOptions.Timeout = new TimeSpan (0,0,0,30); string strConnection = "\\\\"+ ipAddress + "\\root\\cimv2"; m_ManScope = new ManagementScope(strConnection, m_ConnectOptions); m_ManScope.Connect(); { ObjectQuery Temp = new ObjectQuery("SELECT " + "Size" + " FROM Win32_" + "DiskDrive"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("Size: " +mo["Size"].ToString ()); } Temp = new ObjectQuery("SELECT " + "Model" + " FROM Win32_" + "DiskDrive"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("Model: " +mo["Model"].ToString ()); } Temp = new ObjectQuery("SELECT " + "SystemName" + " FROM Win32_" + "DiskDrive"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("SystemName: " +mo["SystemName"].ToString ()); } Temp = new ObjectQuery("SELECT " + "AdapterRAM" + " FROM Win32_" + "VideoController"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("AdapterRAM: " +mo["AdapterRAM"].ToString ()); } Temp = new ObjectQuery("SELECT " + "CurrentRefreshRate" + " FROM Win32_" + "VideoController"); m_MOSearcher = new Man

              D 1 Reply Last reply
              0
              • W wakkerjack

                Here's my code.. works for me.. Excuse the repertition, i just do this for complexities sake:) some RPC should occur if u use the threads... using System; using System.Threading; using System.Management; namespace SimpleTRY { public class Class1 { private ManagementObjectSearcher m_MOSearcher; private ManagementScope m_ManScope; private ConnectionOptions m_ConnectOptions; public Class1() { for (int i=0; i< 250; i++) { /*Thread a = new Thread(new ThreadStart(this.connect)); a.Start();*/ this.connect(); } } public void connect() { string ipAddress = "192.168.0.88"; m_ConnectOptions = new ConnectionOptions (); m_ConnectOptions.Username = "Gorilla"; m_ConnectOptions.Password = "rock"; m_ConnectOptions.Authentication = AuthenticationLevel.Call; m_ConnectOptions.Impersonation = ImpersonationLevel.Impersonate; m_ConnectOptions.EnablePrivileges = true; m_ConnectOptions.Locale = "MS_409"; m_ConnectOptions.Timeout = new TimeSpan (0,0,0,30); string strConnection = "\\\\"+ ipAddress + "\\root\\cimv2"; m_ManScope = new ManagementScope(strConnection, m_ConnectOptions); m_ManScope.Connect(); { ObjectQuery Temp = new ObjectQuery("SELECT " + "Size" + " FROM Win32_" + "DiskDrive"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("Size: " +mo["Size"].ToString ()); } Temp = new ObjectQuery("SELECT " + "Model" + " FROM Win32_" + "DiskDrive"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("Model: " +mo["Model"].ToString ()); } Temp = new ObjectQuery("SELECT " + "SystemName" + " FROM Win32_" + "DiskDrive"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("SystemName: " +mo["SystemName"].ToString ()); } Temp = new ObjectQuery("SELECT " + "AdapterRAM" + " FROM Win32_" + "VideoController"); m_MOSearcher = new ManagementObjectSearcher (m_ManScope, Temp); foreach(ManagementObject mo in m_MOSearcher.Get()) { Console.WriteLine("AdapterRAM: " +mo["AdapterRAM"].ToString ()); } Temp = new ObjectQuery("SELECT " + "CurrentRefreshRate" + " FROM Win32_" + "VideoController"); m_MOSearcher = new Man

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #8

                Now I get your problem. The issue is your swamping the target machine with requests. You have to pace your request by dropping in a Thread.Sleep() right after your Thread.Start(). What's happening is by launching 250 requests, at nearly the same time, the WMI service on the target machine can't keep up. Your pegging the target machines CPU Utilization at 100% the entire time your making these requests. In order to get it to work reliably, I had to pace the thread launches at every 150 milliseconds. This was by no means an extensive test, so for reliability, you'd probably have to increase that number to 250 milliseconds. THe only way to guarantee that it won't fail, is to launch them one at a time and wait for each request to complete before you launch the next one. BTW: Why on earth would you want to do this? The only reason I can see is that your launching 250 requests at 250 seperate machines, not the same machine. This requirement would invalidate any testing your doing because you ar no longer stressing a single target machine. A better way to do this would be to queue the work using the .NET Frameworks' built in ThreadPool.

                public Class1()
                {
                for (int i=0; i< 250; i++)
                {
                Thread a = new Thread(new ThreadStart(this.connect));
                a.Start();
                Thread.Sleep(150);
                //this.connect();
                }
                }

                RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                W 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Now I get your problem. The issue is your swamping the target machine with requests. You have to pace your request by dropping in a Thread.Sleep() right after your Thread.Start(). What's happening is by launching 250 requests, at nearly the same time, the WMI service on the target machine can't keep up. Your pegging the target machines CPU Utilization at 100% the entire time your making these requests. In order to get it to work reliably, I had to pace the thread launches at every 150 milliseconds. This was by no means an extensive test, so for reliability, you'd probably have to increase that number to 250 milliseconds. THe only way to guarantee that it won't fail, is to launch them one at a time and wait for each request to complete before you launch the next one. BTW: Why on earth would you want to do this? The only reason I can see is that your launching 250 requests at 250 seperate machines, not the same machine. This requirement would invalidate any testing your doing because you ar no longer stressing a single target machine. A better way to do this would be to queue the work using the .NET Frameworks' built in ThreadPool.

                  public Class1()
                  {
                  for (int i=0; i< 250; i++)
                  {
                  Thread a = new Thread(new ThreadStart(this.connect));
                  a.Start();
                  Thread.Sleep(150);
                  //this.connect();
                  }
                  }

                  RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  W Offline
                  W Offline
                  wakkerjack
                  wrote on last edited by
                  #9

                  Why i am doing this incredibly daft task is to test the ManagementObjectSearcher Class.. because: i have another program that does retrieve info from many different comps.. the prob is while im scanning them, sometimes all of a sudden i get an RPC fail or COMException.. SO i want to make sure it's WMI setting or possibly a bug. To Me, Something seems fishy with remotly retrieving the info.. Everything works fine locally. please try again with my code, using no threads (just "connect()") and see if u get the error? after it has gone thru the loop about 95 times the Exception pops up? Thanks:)

                  D 2 Replies Last reply
                  0
                  • W wakkerjack

                    Why i am doing this incredibly daft task is to test the ManagementObjectSearcher Class.. because: i have another program that does retrieve info from many different comps.. the prob is while im scanning them, sometimes all of a sudden i get an RPC fail or COMException.. SO i want to make sure it's WMI setting or possibly a bug. To Me, Something seems fishy with remotly retrieving the info.. Everything works fine locally. please try again with my code, using no threads (just "connect()") and see if u get the error? after it has gone thru the loop about 95 times the Exception pops up? Thanks:)

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #10

                    Well, I ran the code like this:

                    public Class2()
                    {
                    for (int i=0; i< 25000; i++)
                    {
                    //Thread a = new Thread(new ThreadStart(this.connect));
                    //a.Start();
                    //Thread.Sleep(150);
                    this.connect();
                    }
                    }

                    and didn't have a single problem... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                    1 Reply Last reply
                    0
                    • W wakkerjack

                      Why i am doing this incredibly daft task is to test the ManagementObjectSearcher Class.. because: i have another program that does retrieve info from many different comps.. the prob is while im scanning them, sometimes all of a sudden i get an RPC fail or COMException.. SO i want to make sure it's WMI setting or possibly a bug. To Me, Something seems fishy with remotly retrieving the info.. Everything works fine locally. please try again with my code, using no threads (just "connect()") and see if u get the error? after it has gone thru the loop about 95 times the Exception pops up? Thanks:)

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #11

                      Then I tried it like this:

                      public Class2()
                      {
                      for (int i=0; i< 2500; i++)
                      {
                      Thread a = new Thread(new ThreadStart(this.connect));
                      a.Start();
                      Thread.Sleep(500);
                      //this.connect();
                      }
                      }

                      and couldn't get it to work reliably until I increased the Sleep delay to half a second. Took forever to run, but... My suggestion still stands, queue up the work in the .NET Thread Pool. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                      W 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        Then I tried it like this:

                        public Class2()
                        {
                        for (int i=0; i< 2500; i++)
                        {
                        Thread a = new Thread(new ThreadStart(this.connect));
                        a.Start();
                        Thread.Sleep(500);
                        //this.connect();
                        }
                        }

                        and couldn't get it to work reliably until I increased the Sleep delay to half a second. Took forever to run, but... My suggestion still stands, queue up the work in the .NET Thread Pool. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                        W Offline
                        W Offline
                        wakkerjack
                        wrote on last edited by
                        #12

                        ja with the first 1: my problems are rare... most of the times it works.. but it's like the WMI on the remote computer gets clogged up from the previous attempts (when i ran the program before) it's funny - i changed the password on the remote comp and now with threads i get an UnauthorizedAccessException sometimes.. NE Ways il let u know wat happens:)

                        W 1 Reply Last reply
                        0
                        • W wakkerjack

                          ja with the first 1: my problems are rare... most of the times it works.. but it's like the WMI on the remote computer gets clogged up from the previous attempts (when i ran the program before) it's funny - i changed the password on the remote comp and now with threads i get an UnauthorizedAccessException sometimes.. NE Ways il let u know wat happens:)

                          W Offline
                          W Offline
                          wakkerjack
                          wrote on last edited by
                          #13

                          hi - thread pool still gives me problems.. um i dont wanna worry about threads atm.. rather, plz could u just use the connect (no threads) and run the exe in the debug folder at the same time.. i would like to know if u also get and RPC error? if u dont get one at first, just keep running the program and exe :) thanks.. Another thing-do u know how to pass Credentials to be able to access the registry? Im using RegistryKey and OpenRemoteBaseKey... thanks again :)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups