Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-03-31
16
Medium Priority
?
293 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
0
Comment
Question by:KIS
14 Comments
 
LVL 96

Expert Comment

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

Bob
0
 

Author Comment

by:KIS
ID: 16343302
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16343328
I still don't understand what you are trying to do?  What type of class instance are you working with here?

Bob
0
Industry Leaders: 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!

 

Author Comment

by:KIS
ID: 16343395

    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
 

Author Comment

by:KIS
ID: 16343442
 
   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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16343502
Yes, but what type of classes have you created, and how have you created them?

Bob
0
 
LVL 41

Expert Comment

by:graye
ID: 16343507
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
 

Author Comment

by:KIS
ID: 16343526

  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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16343549
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
 

Author Comment

by:KIS
ID: 16343759
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16343855
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
 
LVL 1

Expert Comment

by:JoeDW
ID: 16394877
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
 

Author Comment

by:KIS
ID: 16620857
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
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16646848
Closed, 250 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

571 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