Notifications
Clear all

[Closed] How can i create an object from a dotnet class?

Today i tried to create an instance of ManagementClass from dot net.
mcClass = dotNetObject “System.Management.ManagementClass”

but i saw this error:
Runtime error: Cannot resolve type: System.Management.ManagementClass

why can’t i create an object from ManagementClass?
Thanks for any help

5 Replies
 JHN

Because objects need to be instantiated with a value.

mcClass = dotNetObject “System.Management.ManagementClass” “Win32_Share”
(not sure on the value, saw this somewhere on the net)

Classes can be staticly called by
mcClass = dotNetClass “System.Management.ManagementClass”

showInterfaces mcClass

Haven’t tested any of this, not behind max right now.

-Johan

I tried mc = dotNetObject “System.Management.ManagementClass” “Win32_Process”
but i saw this error:
Cannot resolve type: System.Management.ManagementClass

I tried to create a struct from it by this code:
mcClass = dotNetClass “System.Management.ManagementClass”

But this is not successful too. because it return an undefined type! therefore i cant instantiate any object.
I think maxscript can’t access to this resource in .NET frame work. what do you think the reason is? I kept telling myself that the name apace ‘System.Management’ is not accessible from within a maxscript.

seems like the System.Management assembly is not loaded by default so you’ll need to load it at the start of you script as follows:

dotnet.loadAssembly @"System.Management.dll"

i did this and then dotNetClass “System.Management.ManagementObject” no longer returned undefined

1 Reply
(@atoraby)
Joined: 11 months ago

Posts: 0

Thanks for your reply. it is very helpful.
But now i have another problem. I bless you if you can guide me for second time
I have written this code to extract CPU ID.

CPUID=""
 dotnet.loadAssembly @"System.Management.dll"
 mc = dotNetObject "System.Management.ManagementClass" "Win32_Processor"
 moc = dotNetClass "System.Management.ManagementObjectCollection"
 moc = mc.GetInstances()
 for mo in moc do
 (
 	CPUID = CPUID + mo.Properties("ProcessorId").Value.ToString()
 )

But when i evaluate this code i receive this error:

No "map" function for dotNetObject:System.Management.ManagementObjectCollection

I am sure that ProcessorId is a property of ManagementObject in .NET. I also can pledge that ToString is a member function of ManagementObject. but how can i reference to a property of the object in maxscript? why maxscript says that there is no map function!?
Thanks for any help

it’s been my experience that you can’t itterate over dotnetobjects like you can standard max arrays so the loop has to be an index loop (eg for i = 1 to …) however in this case its slightly more complex because there is no get_item property or method to use in your index loop.

There is however a GetEnumerator method that will let you iterate over the collection in a slightly different way by first getting the enumerator into a variable and then calling <enumerator>.MoveNext(). this method will return true until you have reached the end of the collection so you can use it in a while loop as shown below. then you can get the current object from the <enumerator>.current property.

Your next problem is getting the value from the “ProcessorID” PropertyData object from the current ManagementObject’s properties. See below for the proper syntax. Note that you don’t need to call the ToString() method because maxscript has automatically cast it to a string.

dotnet.loadAssembly @"System.Management.dll"
 mc = dotNetObject "System.Management.ManagementClass" "Win32_Processor"
 moc = dotNetClass "System.Management.ManagementObjectCollection"
 moc = mc.GetInstances()
 enumerator = moc.GetEnumerator()
 while enumerator.MoveNext() do
 (
 mo = enumerator.current
 append CPUID ( mo.Properties.Item["ProcessorId"].value )
 )