[Closed] How to transfer data from C# to maxscript
Hello everyone,
I recently have a project need to use C# data,what I want to do is just get a string from C#,then transfer to maxscript,for my goal,C# output a string “Hello World”,then in maxscript,get that string like:
--my goal
get_string_from_c="Hello, World!"
The origin C# code like this:
using System;
public class Hello2
{
public static void Main()
{
Console.WriteLine("Hello, World!");
Console.ReadLine();
}
}
To embed into maxscript:
source = "using System;
"
source += "public class Hello2
"
source += "{
"
source += "public static void Main()
"
source += "{
"
source += "Console.WriteLine("+"\""+"Hello, World!"+"\""+");
"
source += "Console.ReadLine();
"
source += "}
"
source += "}
"
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.GenerateInMemory = on
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
global assembly = compilerResults.CompiledAssembly.createInstance "assembly"
--how to get "Hello, World!" now?
Thanks for any help!
Hi @momo2012.
What you are trying is surely easy and direct… but I can’t unserstand what you need.
Any C# method that returns a string will give you directly a string in Maxscript.
In the other direction, any Maxscript string can be used directly by any C# method that requires a string input parameter.
Hi aaandres,thanks for reply,I need to get some hardware information through C#,I just don’t know how to do that ,is there an example of get data from C# to maxscript ?
public static string sayHelloWord (string str)
{
string hell= str;
return hell;
// if you want just say string
// returned ex:str= "hello" in maxscript listener "hello" just fine
}
public static string[] sayHelloStringArray (int total)
{
//you could just parallel but now its standard version
string[] hello50times = new string[total];
for (int i=0 ; i <total ;i++)
{
string st = String.Format ("hello : {0}",i);
hello50times[i] = st;
}
return hello50times
// if you exe it , it'll return #("Hello : 1","Hello : 2",,,,,,etc) in mxs listener
}
Hope it help , coz im new too in c# world
as for how to get HW info , you could just read it here
http://www.codescratcher.com/windows-forms/get-computer-hardware-information-using-c/
but I believe maxscript has it too. But I forgot it where, maybe some masta here will help you out.
Hi,too much thanks,is that possible to transfer data from C# to maxscript without compile to exe?Just import C# codes into maxscript itself and get the data(strings) from source C# ?
About the hardware infomation, maxscript can do it ,right,but not for all old machines or old OS,so I am trying to do that
(PS,I am C# language newer too)
some can some not …I believe if you use “lambda” in c#, it’ll hard to use it directly using maxscript but if its some native method that max c# version support it can…
//in case of this
//from http://www.codescratcher.com/windows-forms/get-computer-hardware-information-using-c/
//in c#
public static String GetProcessorId()
{
ManagementClass mc = new ManagementClass("win32_processor");
//ManagementObjectCollection moc = mc.GetInstances();
String Id = String.Empty;
....blla..bla
}
in maxscript you just
aa= dotnetObject "System.Management.ManagementClass"
--showProperties aa
--showMethods aa
bb = aa.GetInstances()
--bla bla bla.....
the thing that I still not figuring it out is something like this
Parallel.For (0,total, i => {do something}); how to write it in maxscript?????
altho its almost the same as for{int=i;i<xx;i++} , it just different in performance, because one is multithreaded the later is not , in this case you must compile it as dll or compile it on the fly…
or c# code source in max lower than targeted lang like c# 4,5 where my max still 4
also dont use Console.WriteLine in dll , it wont show anything [but it work, it just had to hack it little] , comparable to print in maxscript listener just use Debug.WritteLine , its in case you work in VS tho.
try to read “windowsdir-sysinfo” in mxs help , altho not many you can get there tho.