Solution to creating a managed MWI provider

I am attempting to create a Windows Service in C# that exposes management interfaces through WMI. I'm having trouble creating new objects from the WMI command line. See the problem description.

In working through the referenced article, I learned a bit more about managed MWI providers. At the half-way point, you have a working coupled WMI provider written in C#. You should be able to enumerate service objects with the following command:

C:\>wmic /NAMESPACE:\\root\test PATH win32_servicehost get /value

I tried that, and sure enough I get a list of service hosts and the services that they are running. These objects are created in code by the EnumerateServiceHosts method.

My problem requires that I create a new object. I have to allow operations to specify its properties and issue it commands. So I tried do create an instance of a Win32_ServiceHost with an ID not yet in the list:

wmic:root\cli>PATH win32_servicehost create id=821
Create instance of 'win32_servicehost' class (Y/N)?y
ERROR:
Code = 0x80041024
Description = Provider is not capable of the attempted operation
Facility = WMI

Not surprising that this would fail, since the code returns null if the process ID is not found. But if I try to create an object using a process ID already in the list, this is what happens:

wmic:root\cli>PATH win32_servicehost create ID=820
Create instance of 'win32_servicehost' class (Y/N)?y
ERROR:
Code = 0x80041019
Description = Object or property already exists
Facility = WMI

So I changed the example code to allow me to create process hosts with IDs that are not in use. Now every ID I use gives me "Object or property already exists". But if I query for an ID that does not exist, it tells me "No Instance(s) Available".

wmic:root\cli>PATH win32_servicehost create id=820
Create instance of 'win32_servicehost' class (Y/N)?y
ERROR:
Code = 0x80041019
Description = Object or property already exists
Facility = WMI

wmic:root\cli>PATH win32_servicehost create id=821
Create instance of 'win32_servicehost' class (Y/N)?y
ERROR:
Code = 0x80041019
Description = Object or property already exists
Facility = WMI

wmic:root\cli>PATH win32_servicehost where ID=820
ID   Services
820  {"SCardSvr"}

wmic:root\cli>PATH win32_servicehost where ID=821
No Instance(s) Available.

Here's my solution

I think I'm approaching the problem from the wrong angle. Create doesn't mean what I think it means. WMI is set up to access objects that already exist.

Instead of creating instances of objects from WMI, I'm going to change my service so that I can return objects created in code. The operations team can issue commands to these existing objects to cause new objects to be created -- in code. Based on what works in the example, this should solve the problem. I'll post when it's working.

Leave a Reply

You must be logged in to post a comment.