Search code examples
window.net-2.0wmi.net-1.1

Get the Readable Message from ManagementObject.InvokeMethod


Is there any way to get the Message Text from the return value of WMI InvokeMethod e.g. for the following code

ManagementBaseObject outParam = nicMO.InvokeMethod("SetGateways", newGateway, null);
int result = outParam["ReturnValue"];

Microsoft has published return values

Is there anyway to get the readable message from the return values instead creating hardcoded return value and message dictionary.

note: Please give me something which can work in .net 2.0 (and/or .net 1.1)


Solution

  • You can get the description of the value returned by the SetGateWays method using the ValueMap and Value Qualifiers .

    enter image description here

    Try this sample

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Management;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<String> LValues = new List<String>();
                List<String> LValuesDesc = new List<String>();
                Dictionary<String, String> LValuesDict = new Dictionary<String, String>();
    
                ManagementClass WClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
                WClass.Options.UseAmendedQualifiers = true;
                foreach (MethodData md in WClass.Methods)
                {
                    if (md.Name.Equals("SetGateways"))
                    {
    
                        //fill a list with the ValuesMap
                        foreach (QualifierData q in md.Qualifiers)
                        {
                            if (q.Name.Equals("ValueMap"))
                            {
                                foreach (object Value in ((Array)q.Value))
                                {
                                    LValues.Add(Convert.ToString(Value));
                                }
                            }
                        }
    
                        //fill a list with the Values
                        foreach (QualifierData q in md.Qualifiers)
                        {
                            if (q.Name.Equals("Values"))
                            {
                                foreach (object Value in ((Array)q.Value))
                                {
                                    LValuesDesc.Add(Convert.ToString(Value));
                                }
                            }
                        }
    
                    //Merge both lists in a dictionary
                    for (int i = 0; i <= LValues.Count - 1; i++)                
                        LValuesDict.Add(LValues[i], LValuesDesc[i]);                
    
                    }
    
                }
    
                    //Get the description of some return values
                    Console.WriteLine(LValuesDict["1"]);
                    Console.WriteLine(LValuesDict["64"]);
                    Console.WriteLine(LValuesDict["77"]);
                    Console.WriteLine(LValuesDict["91"]);
    
                Console.ReadKey();
            }
        }
    }