Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Create New WMI Custom class

416 views
Skip to first unread message

Jacques Pretorius

unread,
Jun 15, 2004, 7:51:01 AM6/15/04
to
Hi
I am trying to through VB .net create my own wmi calss to store demographic and problem ticket data.
All works fine untill i try to create a new instance.
The error i get is
Attempt to put and instance with no defined keys.

The documentation in .NET and MSDN indicat that before committing the changes the keys must be defined on the property but nowhere that i can find do they tell you how to add or define the key on the property.
Other than building a MOF file and running it through the MOFcompiler or Through wbemtest or such manually assigning the Key

I would appreciatesome assistance


Ivan Brugiolo [MSFT]

unread,
Jun 15, 2004, 8:45:14 PM6/15/04
to
When you create and stored the class that's the prototype of your instances,
did you use the [key] qualifier ?

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of any included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Jacques Pretorius" <JacquesP...@discussions.microsoft.com> wrote in
message news:DA4B5652-10C0-4011...@microsoft.com...

Jacques Pretorius

unread,
Jun 17, 2004, 2:16:01 AM6/17/04
to
This is my code maybe you can point me right

Dim sgetvalue As Integer
Dim firstval(3) As String
Dim secondval(3) As String
Dim c As New ManagementClass("root\cimv2", String.Empty, Nothing)
Dim options As New PutOptions
'Set the wmi update options
options.Type = PutType.CreateOnly

'Setup testing value array
firstval(0) = "test1"
secondval(0) = "val1"

firstval(1) = "test2"
secondval(1) = "val2"

firstval(2) = "test3"
secondval(2) = "val3"

c("__Class") = "demo"

Try
Dim i As Integer
Dim secondoval As New ManagementObject

Do Until i = firstval.Length - 1
c.Properties.Add(firstval(i), CimType.String, False)
i = i + 1
Loop

c.Put(options)

Catch e As ManagementException
MsgBox("Couldn't update class: " & e.Message)
End Try

Dim newInstance As New ManagementObject
Try
newInstance = c.CreateInstance()
newInstance("test1") = "mynewinstval1"
newInstance.Put() 'to commit the new instance.

Catch e As ManagementException
MsgBox("Couldn't update class: " & e.Message)
'MsgBox("Couldn't update class: " & e.ErrorInformation)
End Try

Jacques Pretorius

unread,
Jun 17, 2004, 2:17:01 AM6/17/04
to
This is my code maybe you can point me right

Dim sgetvalue As Integer

Dim firstval(3) As String

Dim secondval(3) As String

Dim c As New ManagementClass("root\cimv2", String.Empty, Nothing)

'Setup testing value array

firstval(0) = "test1"

secondval(0) = "val1"

firstval(1) = "test2"

secondval(1) = "val2"

firstval(2) = "test3"

secondval(2) = "val3"

c("__Class") = "demo"

Dim options As New PutOptions

'Set the wmi update options

options.Type = PutType.CreateOnly

Try

Dim i As Integer

Dim secondoval As New ManagementObject

Do Until i = firstval.Length - 1

c.Properties.Add(firstval(i), CimType.String, False)

i = i + 1

Loop

c.Put(options)

'Write the values to the WMI

Catch e As ManagementException

MsgBox("Couldn't update class: " & e.Message)

End Try

Dim newInstance As New ManagementObject

Try

newInstance = c.CreateInstance()

newInstance("test1") = "mynewinstval1"

newInstance.Put() 'to commit the new instance.

Catch e As ManagementException

MsgBox("Couldn't update class: " & e.Message)

'MsgBox("Couldn't update class: " & e.ErrorInformation)

End Try

Ivan Brugiolo [MSFT]

unread,
Jun 17, 2004, 2:31:25 AM6/17/04
to
Let's not confuse language details with the fundamentals.
Open Wbemtest.exe.
Connect to the root\cimv2 namespace
Click "Open Class"
Type in the name of the class. ("demo" if I read correctly the VB.NET code)
The click "Show MOF".
Copy the MOF representation in notepad,
and look for a `[key]` string among the quyalifiers for the properties

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of any included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Jacques Pretorius" <JacquesP...@discussions.microsoft.com> wrote in

message news:1CAC2C6D-9BBE-4AD8...@microsoft.com...

Jacques Pretorius

unread,
Jun 17, 2004, 2:51:01 AM6/17/04
to
This is the MOF after my code has run
class demo
{
string test1;
string test2;
string test3;
};
If i then go and select the property Test1 and select the "key" qualifier the mof then looks like this
class demo
{
[key] string test1;
string test2;
string test3;
};
This i understand and works should i want to manually go and assign this property a [key] qualifier
However i want to achieve the same through my code

Thanks

0 new messages