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

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
KISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Bob
0
KISAuthor 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
0
Bob LearnedCommented:
I still don't understand what you are trying to do?  What type of class instance are you working with here?

Bob
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

KISAuthor 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.
   
0
KISAuthor 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.
0
Bob LearnedCommented:
Yes, but what type of classes have you created, and how have you created them?

Bob
0
grayeCommented:
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
0
KISAuthor 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.
0
Bob LearnedCommented:
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
0
KISAuthor 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.


0
Bob LearnedCommented:
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
0
JoeDWCommented:
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
0
KISAuthor 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.
0
GranModCommented:
Closed, 250 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.