Oct 21, 2009 at 6:10 PM
Edited Oct 22, 2009 at 8:54 AM
You don't have to wait for .Net 4.0 to create a single DLL C# VST plugin. Using a technique I found on the internet (called reverse P/Invoke), I was able to write a little utility program that runs as a post-build step and does the following:
- Decompiles the C# DLL into IL code
- Parses throught the IL text file and looks for a special DllExport attribute that tags a method that needs to be exported as a C function
- Modifies the IL text file to export the method as a C function (reverse P/Invoke)
- Recompiles the IL text file back into a C# DLL.
This happens very fast after each build and isn't even noticeable (takes less than a second). The recompile even creates a new pdb file so that you can debug the DLL just like any other C# DLL. The only thing that doesn't work is Edit & Continue, and
even though I usually use it a lot in other projects, it isn't a huge loss.
If you think about it for a while, you will realize why this works: All .Net languages are compiled down to IL code, including managed C++. This implies that IL itself supports reverse P/Invoke, since managed C++ does. Unfortunately this functionality is
not exposed all the way through to C# (there is no DllExport attribute). But by doing all the steps above, it is possible to add the neccesary attributes to mark methods to be exported.
To find out more about this reverse P/Invoke mechanism, start here:
A couple of points:
- A lot of the code from the above links are buggy. I had to rewritea lot of it to make it work. I ended up writing a completely new utility that works the way I want it to work.
- Some of the examples above do not deal with calling convention. One of the examples do, and you need to use that since a VST plugin needs to use the cdecl calling convention, which is
not the default.
- None of the examples use the /pdb command line switch when recompiling back into a DLL. You need to specify that if you want to debug the resulting DLL in Visual Studio.
If enough people show interest, I might post a working VS project that contains all the required code to create a functional single file C# VST plugin DLL.
EDIT: BTW, using the above technique, I created a single plugin base class that you derive from and override methods like OnGetVendorString, OnCanDo, OnPluginOpen, OnEditorOpen, OnProcess32, OnProcess64, etc. I think this is a much cleaner
implementation than what the current VST.Net framework provides. Simple and it works well. I was also able to add code that makes your plugin UI resizable in most hosts by simply resizing the plugin window itself.