Writing external in c#
Is it possible to write max/msp externals in C#?
You can write externals in any language that supports C calling conventions.
I've spent a lot of time trying to figure out ways to get this to work. You can't directly write max externals in C#, because C# compiles into CIL byte code which is then run through the .NET runtime. Max does not have anything to do with the .NET runtime, so all externals are written in native code (C/C++/Objective-C).
These are the options that I've come up with for getting C# code to interact with Max:
1. Host an instance of the runtime in an external. You'd need to be really careful about performance considerations with this method. If you had separate instances of the runtime for each instance of the external, it would create A LOT of overhead.
See: Loading the Common Language Runtime into a Process
2. Use COM Interop to communicate between an application/component written in C# that exposes itself to COM and a proxy external in max written in C/C++ that connects to the COM application/component. This will involve writing COM code in C/C++, which can be unpleasant. The benefit of this approach is that exposing classes to COM in C# is really easy.
See: Exposing .NET Framework Components to COM
3. Use some sort of inter-process communication to set up a bridge between an application written in C# and a proxy external in max. This could be via named pipes, shared memory, or another method (there are a few others). The downside of this is that writing inter-process communication code in C# can be complicated (at least to me). The C/C++ code for the proxy external should be at least slightly better than with COM, but still not simple.Interprocess Communications
4. Write a web service in C# and access it using max networking objects. This will not be a viable option when high performance is required.
5. Write a C# application that communicates via TCP/UDP with max TCP/UDP send/receive objects. This can involve some slightly complicated C# code. The max won't have to involve writing an external in C/C++. Coming up with a protocol for the communication may take some work.Network Programming
None of these techniques is easy. I've attempted a few of them and haven't gotten especially far. If you figure out a better way of doing this, please let me know.
It may be possible to adapt some of the code from the project "VST.NET" (http://www.codeplex.com/vstnet). It provides a mechanism for hosting .NET VST code in an environment that hosts native DLLs. Since Max extenals are essentially the same thing, it might be possible to take this code and rework it to host .NET external code inside a native external DLL wrapper.
I’ve just published a working demo to show how C# / .Net assemblies can be called from Max and the other way around n MaxForLive.com. It’s only for Windows 10; sorry, Apple is not supported. The Console is accessible and Windows Forms can be used. You can experiment with AI. This is the link: https://maxforlive.com/library/device.php?id=7618 .