We help IT Professionals succeed at work.

Simple WMI Problem in C# but it doesnt work for me.

KIS
KIS asked
on
Medium Priority
318 Views
Last Modified: 2010-04-16
Hi,

I created a function which returns a ManagementObject, base on the className and InstanceName. InstanceName is a field of the WMI class provider which I created:

  private static void GetWMIObject(string className, string instanceName, out ManagementObject result)
  {
    result = null;
   
    // Search the parameter with the given instance name.
    string wqlStatement = "SELECT * FROM " +  className + " WHERE InstanceName = '" + instanceName + "'";
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\PDN", wqlStatement);

   foreach (queryObj in searcher.Get())
   {
       // i am interested in the first instance found
       result = queryObj;
       
       // for testing only. it works OK here
       object x = result["InstanceName"];
       break;
   }

   searcher.Dispose();
 }                  


Now, in my code I call this function to get the out WMI object:

ManagementObject objWMI;
GetWMIObject("MyClass", "Instance1", objWMI);

if(objWMI == null)
{
  return;  // it does NOT get here
}

// this throws exception: "Object reference not set to an instance of an object"
object x = objWMI["InstanceName"];


So why the objWMI seems like not linked to the object ?
Within the function,  result["InstanceName"] works OK but for the returned objWMI the call objWMI["InstanceName"] does not work.

thanks
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
This doesn't sound a simple problem to me.  Can you describe what you are trying to accomplish here?

Bob
KIS

Author

Commented:
Well, on my machine it seems that it does not work and I dont have any idea why the object within the function is working and why the out object 2nd doesnt.

any ideas ?

thanks
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
I still don't understand what you are trying to do?  What type of class instance are you working with here?

Bob
KIS

Author

Commented:

    I have a WMI provider written in C# by me in which I have like 4 classes which I want to expose by WMI.  
    I create objects of my WMI Classes and I publish their instances to WMI.

    Now, i have also a 2nd application which I want to use, to read values from this instances.
    In this client app, in order to get the instances, i wrote that function which accepts a class name and a instance name (My WMI Class has a string field called "InstanceName") and returns the WMI ManagementObject instance.

   Please tell me if I am clear enough.

  thanks.
   
KIS

Author

Commented:
 
   More:
   accessing InstanceName is one field my class. I have other fields which are more important and I want to use.
  InstanceName is just for differentiate between instances of the class.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Yes, but what type of classes have you created, and how have you created them?

Bob

Commented:
It could be a problem with our Provider code (and not the code you're using here).  I'd recommend that you download/install the WMI Admin Tools (if you haven't already).   The tools will allow you to view the WMI namespaces and see if your provider is working correctly.

http://www.microsoft.com/downloads/details.aspx?FamilyID=6430f853-1120-48db-8cc5-f2abdc3ed314&displaylang=en
KIS

Author

Commented:

  thanks for your comment. i am using whole suite of WMI tools from MS.

  the same code works OK on other machine. on mine it doesnt.
  reinstalling Windows is an alternative but its very important to see what is going on.

  thanks.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Information, we need more information?  What type of classes are you creating?  How are you creating them?  What kind of operating systems are involved?

More, we need more ;)

Bob
KIS

Author

Commented:
OK, :) sorry

OS: Windows XP SP2
.NET Framework v1.1.
Dev Environment: MS Visual Studio 2003.
Im working in C#.

I followed the MSDN and created my classes in order to expose thourgh WMI:

public interface IInterface1
{

}
public interface IInterface2
{

}

public interface IInterface3
{

}


[InstrumentationClass(InstrumentationType.Abstract)]
public abstract class MyBaseClass : MarshalByRefObject, IInterface1, IInterface2, IInterface3
{
       protected string instanceName = null;
}


[InstrumentationClass(InstrumentationType.Instance)]
public class MyDerivedClass1 : MyBaseClass
{
    public MyDerivedClass (string instanceName)
    {
         this.instanceName = instanceName;
    }
}

the other classes MyDerivedClass2, MyDerivedClass3, MyDerivedClass4 look similar

now, as I said, it works OK on the other machine.
whats freaky is that it works within the that function and not using out the param.


CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Do you have a reference to the MSDN article that describes the steps that you took to create a class?  What kind of information are these classes collecting?

Bob

Commented:
KIS...

I am not sure that this will even help you at all but i use WMI in a similiar way you do but to retrieve network information... The code is as follows:

public void GetWMI()
        {
           
            string query = "SELECT * FROM Win32_NetworkAdapterConfiguration"
                         + " WHERE DNSDomain = ''domain.com";

            ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
            ManagementObjectCollection moCollection = moSearch.Get();

            try
            {
                foreach (ManagementObject mo in moCollection)
                {
                     lblMachineName.Text = mo["DNSHostName"].ToString();
                     lblMAC.Text = mo["MACAddress"].ToString();
                     lblDomain.Text = mo["DNSDomain"].ToString();
                     

                    string[] addresses = (string[])mo["IPAddress"];
                    foreach (string ipaddress in addresses)
                    {
                        lblIPAddress.Text = ipaddress;
                    }

                    string[] subnets = (string[])mo["IPSubnet"];
                    foreach (string ipsubnet in subnets)
                    {
                        lblSubnet.Text = ipsubnet;
                    }

                    string[] defaultgateways = (string[])mo["DefaultIPGateway"];
                    foreach (string defaultipgateway in defaultgateways)
                    {
                        lblGateway.Text = defaultipgateway;
                    }
                }
            }
            catch (Exception exc)
            {
                                MessageBox.Show(exc.Message);
            }
}

maybe it will give you a little more information than you have now..

Cheers
Joe
KIS

Author

Commented:
OK,

The problem is solved.
For anyone who will find this, the problem comes from the .NET Framework 1.1 WITHOUT the SP installed.

If you look in the C:\Windows\system32\wbem\repository you will see that the Framework 1.1 generates bad cs files. I dont know exactly what are these cs for, but It seems like they are used by .NET Management INstrumentation at run time (compiled at runtime maybe).
But these cs files are bad generated by the framework when the WMI Class is registered in the repository. I figure out by comparing them with the same cs files for the same WMI Class on another PC which doesnt have my problem.

Hope this will be useful for somebody.
Commented:
Closed, 250 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.