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.
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.