ManagementCreate attribute usage

U

urkec

I am trying to create an in-process WMI provider using
System.Management.Instrumentation namespace. For testing I use a simple class
as a wrapper for FileInfo class. I have been able to use all attributes
successfully (ManagementKey, ManagementProbe, ManagementBind...). The only
one attribute I am having problem with is ManagementCreate. As I understand
from the documentation, it is supposed to support creation of new instances
of a management class. (by using eg. SWbemObject.SpawnInstance_ from WMI
scripting library). My code is as follows:


using System;
using System.Collections;
using System.IO;
using System.Management.Instrumentation;


[assembly: WmiConfiguration(
@"root\test",
HostingModel = ManagementHostingModel.NetworkService)]

namespace WmiTest
{

[System.ComponentModel.RunInstaller(true)]
public class MyInstall : DefaultManagementInstaller
{
}


[ManagementEntity(Name = "My_File")]
public class MyDataFile
{

private FileInfo objFile;

public MyDataFile(FileInfo objFile)
{
this.objFile = objFile;
}

[ManagementKey]
public string FullName
{
get
{
return objFile.FullName;
}
}

[ManagementConfiguration]
public DateTime CreationDate
{
get
{
return objFile.CreationTime;
}
set
{
objFile.CreationTime = value;
}
}


[ManagementCreate]
static public MyDataFile Create(string FullName)
{
FileInfo objFile = new FileInfo (FullName);
objFile.Create();
return new MyDataFile(objFile);
}

[ManagementBind]
public MyDataFile(string FullName)
{
FileInfo objFile = new FileInfo(FullName);
this.objFile = new FileInfo(FullName);
}

[ManagementEnumerator]
static public IEnumerable EnumerateFiles()
{
FileInfo[] colFiles =
new DirectoryInfo(@"C:\test").GetFiles();

foreach (FileInfo objFile in colFiles)
{
yield return new MyDataFile(objFile);
}
}
}
}


Everything else works except ManagementCreate. Root\test namespace and
My_File class get created, I am able to enumerate and bind to instances,
enumerate and read properties etc. When I try to use
SWbemObject.SpawnInstance_ and SWbemObject.Put_ there is no error but the
file doesn't get created. The same thing happens when I use CIM studio. Am I
missing something or just don't understand what ManagementCreate attribute is
supposed to be used for? Are there any working examples of ManagementCreate
(and ManagementRemove) usage?
Thanks for any help.
 
S

Sofapilot

Hi Urkec

I have been trying to use the same attribute to get a de-coupled WMI
provider to work with no success..

However, I have (in the last couple of minutes) had some luck via the
alternative method of adding a static [ManagementTask] method, which calls
the WMI constructor. In my case, the code snippet looks like this:

[ManagementTask]
public static void addWMI(int id)
{
WMI inst = new WMI(id);
}

public WMI([ManagementName("ID")] int id)
{
_ID = id;
_Instances.Add(_ID, this);
}

Where WMI is the (uninspired class) name for my test WMI class which
maintains a static collection of WMI instances, which is returned by the
[ManagementEnumerator] as :


[ManagementEnumerator]
static public IEnumerable EnumerateInstances()
{
foreach (WMI curWMI in _Instances.Values)
{
yield return curWMI;
}

}

This version of my test class allows me to call the addWMI method from the
VS2008 Server Explorer / Management classes tree. I haven't tried it via
wbemtest.

This is my first foray into the world of WMI, so sadly I am unlikely to be
of much more immediate help. Good luck though, and I'd be interested to hear
how you get on.



urkec said:
I am trying to create an in-process WMI provider using
System.Management.Instrumentation namespace. For testing I use a simple class
as a wrapper for FileInfo class. I have been able to use all attributes
successfully (ManagementKey, ManagementProbe, ManagementBind...). The only
one attribute I am having problem with is ManagementCreate. As I understand
from the documentation, it is supposed to support creation of new instances
of a management class. (by using eg. SWbemObject.SpawnInstance_ from WMI
scripting library). My code is as follows:


using System;
using System.Collections;
using System.IO;
using System.Management.Instrumentation;


[assembly: WmiConfiguration(
@"root\test",
HostingModel = ManagementHostingModel.NetworkService)]

namespace WmiTest
{

[System.ComponentModel.RunInstaller(true)]
public class MyInstall : DefaultManagementInstaller
{
}


[ManagementEntity(Name = "My_File")]
public class MyDataFile
{

private FileInfo objFile;

public MyDataFile(FileInfo objFile)
{
this.objFile = objFile;
}

[ManagementKey]
public string FullName
{
get
{
return objFile.FullName;
}
}

[ManagementConfiguration]
public DateTime CreationDate
{
get
{
return objFile.CreationTime;
}
set
{
objFile.CreationTime = value;
}
}


[ManagementCreate]
static public MyDataFile Create(string FullName)
{
FileInfo objFile = new FileInfo (FullName);
objFile.Create();
return new MyDataFile(objFile);
}

[ManagementBind]
public MyDataFile(string FullName)
{
FileInfo objFile = new FileInfo(FullName);
this.objFile = new FileInfo(FullName);
}

[ManagementEnumerator]
static public IEnumerable EnumerateFiles()
{
FileInfo[] colFiles =
new DirectoryInfo(@"C:\test").GetFiles();

foreach (FileInfo objFile in colFiles)
{
yield return new MyDataFile(objFile);
}
}
}
}


Everything else works except ManagementCreate. Root\test namespace and
My_File class get created, I am able to enumerate and bind to instances,
enumerate and read properties etc. When I try to use
SWbemObject.SpawnInstance_ and SWbemObject.Put_ there is no error but the
file doesn't get created. The same thing happens when I use CIM studio. Am I
missing something or just don't understand what ManagementCreate attribute is
supposed to be used for? Are there any working examples of ManagementCreate
(and ManagementRemove) usage?
Thanks for any help.
 
U

urkec

Thank you for the response. This is also my first attempt with
System.Management.Instrumentation. I started with this article:

http://msdn2.microsoft.com/en-us/library/cc268228.aspx

It was not too difficult to figure out how to use ManagementKey,
ManagementProbe and ManagementConfiguration attributes, but I am still stuck
with ManagementCreate. I started with the code snippet from MSDN:

(http://msdn.microsoft.com/en-us/lib...nstrumentation.managementcreateattribute.aspx)

[ManagementEntity]
public class ProcessInstance
{
[ManagementKey]
public int Id;

[ManagementCreate]
public ProcessInstance StartProcess(string cmdLine)
{
ProcessInstance newProcess = new ProcessInstance(cmdLine);
newProcess.Start();
return newProcess;
}
}


but Installutil reported this:


An exception occurred during the Install phase.
System.Management.Instrumentation.WmiProviderInstallationException: Incorrect
usage of [ManagementCreate] attribute on a method. ... It should not be used
by Singleton classes and should be on a static method and all parameters'
managed name should match managed properties' managed name.

The class is not Singleton and parameter names match, so I then turned
Create() into a static method. Installutil completed without errors, but from
CIM Studio, which I use for testing I wasn't able to create a new instance.
(No errors, the instance just doesn't get created). Later, i tried to create
an instance via VBScript SpawnInstance_ and Put_, but an exception was thrown
on the Put_ line. I was not able to start the debugger with 'acces denied'
error. The exception was actually recorderd in the Application event log as
an IO exception (I also found other exceptions recorded there, this could be
useful for debugging). So I tried various code changes with no success. Just
as you, I was able to use ManagementTask to create and delete files, and it
seems to work without problems (now I think it is possible that I completely
misunderstand how ManagementCreate is supposed to be used).

--
urkec


Sofapilot said:
Hi Urkec

I have been trying to use the same attribute to get a de-coupled WMI
provider to work with no success..

However, I have (in the last couple of minutes) had some luck via the
alternative method of adding a static [ManagementTask] method, which calls
the WMI constructor. In my case, the code snippet looks like this:

[ManagementTask]
public static void addWMI(int id)
{
WMI inst = new WMI(id);
}

public WMI([ManagementName("ID")] int id)
{
_ID = id;
_Instances.Add(_ID, this);
}

Where WMI is the (uninspired class) name for my test WMI class which
maintains a static collection of WMI instances, which is returned by the
[ManagementEnumerator] as :


[ManagementEnumerator]
static public IEnumerable EnumerateInstances()
{
foreach (WMI curWMI in _Instances.Values)
{
yield return curWMI;
}

}

This version of my test class allows me to call the addWMI method from the
VS2008 Server Explorer / Management classes tree. I haven't tried it via
wbemtest.

This is my first foray into the world of WMI, so sadly I am unlikely to be
of much more immediate help. Good luck though, and I'd be interested to hear
how you get on.



urkec said:
I am trying to create an in-process WMI provider using
System.Management.Instrumentation namespace. For testing I use a simple class
as a wrapper for FileInfo class. I have been able to use all attributes
successfully (ManagementKey, ManagementProbe, ManagementBind...). The only
one attribute I am having problem with is ManagementCreate. As I understand
from the documentation, it is supposed to support creation of new instances
of a management class. (by using eg. SWbemObject.SpawnInstance_ from WMI
scripting library). My code is as follows:


using System;
using System.Collections;
using System.IO;
using System.Management.Instrumentation;


[assembly: WmiConfiguration(
@"root\test",
HostingModel = ManagementHostingModel.NetworkService)]

namespace WmiTest
{

[System.ComponentModel.RunInstaller(true)]
public class MyInstall : DefaultManagementInstaller
{
}


[ManagementEntity(Name = "My_File")]
public class MyDataFile
{

private FileInfo objFile;

public MyDataFile(FileInfo objFile)
{
this.objFile = objFile;
}

[ManagementKey]
public string FullName
{
get
{
return objFile.FullName;
}
}

[ManagementConfiguration]
public DateTime CreationDate
{
get
{
return objFile.CreationTime;
}
set
{
objFile.CreationTime = value;
}
}


[ManagementCreate]
static public MyDataFile Create(string FullName)
{
FileInfo objFile = new FileInfo (FullName);
objFile.Create();
return new MyDataFile(objFile);
}

[ManagementBind]
public MyDataFile(string FullName)
{
FileInfo objFile = new FileInfo(FullName);
this.objFile = new FileInfo(FullName);
}

[ManagementEnumerator]
static public IEnumerable EnumerateFiles()
{
FileInfo[] colFiles =
new DirectoryInfo(@"C:\test").GetFiles();

foreach (FileInfo objFile in colFiles)
{
yield return new MyDataFile(objFile);
}
}
}
}


Everything else works except ManagementCreate. Root\test namespace and
My_File class get created, I am able to enumerate and bind to instances,
enumerate and read properties etc. When I try to use
SWbemObject.SpawnInstance_ and SWbemObject.Put_ there is no error but the
file doesn't get created. The same thing happens when I use CIM studio. Am I
missing something or just don't understand what ManagementCreate attribute is
supposed to be used for? Are there any working examples of ManagementCreate
(and ManagementRemove) usage?
Thanks for any help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top