473,503 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ManagementCreate attribute usage


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.
--
urkec
Jun 27 '08 #1
2 3239
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" wrote:
>
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.
--
urkec
Jun 27 '08 #2
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/libr...attribute.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.WmiProviderInsta llationException: 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" wrote:
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" wrote:

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.
--
urkec
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3638
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
14
4578
by: Del Ferguson | last post by:
Group, New to this list. I just found out that FireFox does not display the img alt attribute the same way IE does. I use both browsers to verify that my pages are readable. Dumb me for not...
9
5547
by: M.N.A.Smadi | last post by:
HI; I am having the following error. I am using someone else's code and all they are doing is pass an argv to a function then def execute_action(manager, argv): method_name =...
1
1524
by: tolisss | last post by:
Hi i have to custom attributes for using with properties. What i want to do is instruct the compiler to throw a warning or ever better an error if both attributes are used in the same property....
1
1886
by: prem | last post by:
Hi , Any one please give me the Usage with example for AutoEventWireUp PageDirective. Regards, Prem.
2
3513
by: Harry F. Harrison | last post by:
I get 2 compile errors on assembly attributes after creating a custom attribute. If I comment out the attribute, the errors go away. I don't get it because my attribute specifies class usage,...
1
2018
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
5
2860
by: Soledad Vel | last post by:
Hi All, i write this code: var sliderwidth=100; var sliderheight = 100; var div1 = document.createElement('div'); div1.setAttribute('id','d5'); div1.setAttribute('style',...
2
1711
by: gary.goodwin | last post by:
HI I am trying to understand Attribute usage. For example the class SerializableAttribute is a class correct? So why when it is actually u sed the "Attribute" portion of the name is dropped. The...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.