Question

So I found this guide for doing it in C# and it worked flawlessly, very very easy to set up. Now I've been trying to do the same thing in C++/CLI as he only codes in C++/CLI and it's turning out to be a lot more complicated..

First of all there is no dynamic keyword in C++/CLI so translating the code example from that guide didn't really seem to work. I found another example specifically for C++/CLI but it doesn't actually work anymore, I'm guessing it is for an old version of IronPython. So I really need some help with just an easy way to run an IPY script from within C++/CLI and then maybe an example on how to expose objects/methods to the script.

Was it helpful?

Solution

It should be possible (I've never done it with C++/CLI), but you'll just have to do it the old fashioned, pre-.NET 4 way (in C#):

ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
engine.Execute(fooScript, scope);
object foo = scope.GetVariable("foo");
object result = engine.Operations.Invoke(foo, 1, "a");

Sprinkle ^ in there as appropriate to convert the C# to C++/CLI.

(fooScript, in this case, would contain something like:

def foo(a, b):
    return str(a) + ", " + str(b)

)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top