Make C++ code communicate with Grasshopper


I have coded an algorithm using Grasshopper but now I need to launch it with different parameters from a C++ software “in the background” (typically the parameters would be a boolean to launch the algorithm and a string with the path to an .obj file).

I have been searching for a way to do that. But I ended with complicated IPC communication in C++ and C# like a shared memory using file mapping (and I’m not even sure I’ll have access to the right libs on the C# scripting in Grasshopper…).

Is there a way to achieve what i want in an easy way that i didn’t see ?

Thank you in advance.

Typically the way to communicate between C# and C++ is to write some exported C functions into your C++ library and then pInvoke into them from C#. This is how RhinoCommon communicates with the Rhino C++ SDK.

P/Invoke seems to be able to call the C++ functions but not the other way around if I’m not mistaking.

I wanted to “call” the Grasshopper (or the C#) from the C++.

You would need to have your C# code set call a pInvoke to set a C function callback that your C++ code could later call.

By “call” it often means just passing variables from C# to C++ and then getting output back.
PInvoke allows you to call functions from C++ .
Wrapping is a tedious process, but there are number of plugins already written that way.
I did one for wrapping pointcloud methods: Cockroach | Food4Rhino
There is also example from Dale, called Moose on Github.

What I really miss is how to pass nested arrays, since making flat arrays from nested ones are always a bit slow process.

For more complex things you can use CLI.

In all cases you are at the border of internet so allocate time implementing these workflows.