Reputation: 9015
The main issue is this: I get a ManagementBaseObject
as returned value. But I cannot call its methods, as it doesn't have the InvokeMethod()
member, like ManagementObject
has. So how do I call its member-methods?
I open a BcdStore
object:
var bcdCls = new ManagementClass(@"root\WMI", "BcdStore", null); // OpenStore is a static method
var methodParams = bcdCls.GetMethodParameters("OpenStore");
methodParams["file"] = ""; // the default store
var results = bcdCls.InvokeMethod("OpenStore", methodParams, null);
Assert.IsNotNull(results);
var store = results["store"] as ManagementBaseObject;
Assert.IsNotNull(store);
But this object is useless:
// unfortunately, it is not a ManagementObject, so no InvokeMethod() is possible :-(
Assert.IsNull(store as ManagementObject);
store.InvokeMethod("EnumerateObjects", new object[] { 0 }); // Compilation error!
// ManagementBaseObject doesn't have an InvokeMethod member method!
Upvotes: 2
Views: 5588
Reputation: 2781
Probably it's a bug, because PropertyData
always returns ManagementBaseObject
if Type
's value is CimType.Object
(source). The return value of InvokeMethod
is also ManagementBaseObject
(source), except when ManagementOperationObserver
is used.
Example without an observer:
// result is ManagementBaseObject
// result["__GENUS"] == 2
var result = bcdCls.InvokeMethod("OpenStore", methodParams, null);
Example with an observer:
var observer = new ManagementOperationObserver();
observer.ObjectReady += ObjectReady;
bcd.InvokeMethod(observer, "OpenStore", methodParams, null);
void ObjectReady(object sender, ObjectReadyEventArgs e)
{
// e.NewObject is ManagementObject
// e.NewObject["__GENUS"] == 2
}
In the second snippet we have a ManagementObject
, because the ManagementBaseObject.GetBaseObject
method is used internally (source). So you can report to Microsoft.
And finally, workaround:
var bcd = new ManagementClass(@"root\WMI", "BcdStore", null);
var openStoreParams = bcd.GetMethodParameters("OpenStore");
openStoreParams["File"] = "";
var openStoreResult = bcd.InvokeMethod("OpenStore", openStoreParams, null);
var openedStore = (ManagementObject)typeof(ManagementBaseObject)
.GetMethod("GetBaseObject", BindingFlags.Static | BindingFlags.NonPublic)
.Invoke(
null,
new object[]
{
typeof(ManagementBaseObject)
.GetField("_wbemObject", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(openStoreResult["Store"]),
bcd.Scope
}
);
var enumObjectsParams = openedStore.GetMethodParameters("EnumerateObjects");
enumObjectsParams["Type"] = 0;
var enumObjectsResult = openedStore.InvokeMethod("EnumerateObjects", enumObjectsParams, null);
var enumObjects = (ManagementBaseObject[])enumObjectsResult["Objects"];
foreach (var enumObject in enumObjects)
{
// Do something
}
Upvotes: 2