Plugin Development in C-Sharp
From DeleDWiki
| Plug-in Development |
|
This is an example of how to write a plugin in C# and the .NET framework, which is a managed environment. In order to account for the proper interop between your C# plugin and DeleD, you must install the following Visual Studio template:
DLLExport is a MSBuild task created by Robert Giesecke that allows you to export static C# functions that can be consumed as an ordinary native DLL Export. So make sure you install it and have the proper requirements installed (it requires the .NET SDK, as it emits IL instructions).
Code Sample
The following code is a port of the C/C++ examples, and when executed it will query DeleD for the current scene geometry and display it in a message box pop-up. The plugin allocates unmanaged memory for DeleD to write to as response XML. Caution needs to be exercised when doing this since if you do not free this memory after you're done using it, then memory leaks can occur.
TestPlugin.cs
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using RGiesecke.DllExport;
namespace DeleDTest {
[StructLayout(LayoutKind.Sequential)]
public struct TCallBackRecord {
public int RequestID;
public String RequestXML;
public IntPtr ResponseXML;
public int ResponseSize;
}
public delegate void TCallBackProc(ref TCallBackRecord record);
static class TestPlugin {
static TCallBackProc _callback;
const int PR_GETMEM = 0;
const int PR_GETDATA = 1;
const int PR_SETDATA = 2;
[DllExport("PluginName", CallingConvention.StdCall)]
static String PluginName() {
return "Example C# Plugin";
}
[DllExport("PluginDescription", CallingConvention.StdCall)]
static String PluginDescription() {
return "Does a lot of bar, in addition to the foo.";
}
[DllExport("PluginDeleDVersion", CallingConvention.StdCall)]
static String PluginDeleDVersion() {
return "1.8";
}
[DllExport("PluginVersion", CallingConvention.StdCall)]
static String PluginVersion() {
return "1.0";
}
[DllExport("PluginAuthor", CallingConvention.StdCall)]
static String PluginAuthor() {
return "Starnick";
}
[DllExport("PluginEmail", CallingConvention.StdCall)]
static String PluginEmail() {
return "plug@me.in";
}
[DllExport("PluginSetCallback", CallingConvention.StdCall)]
static void PluginSetCallback(TCallBackProc callbackproc) {
_callback = callbackproc;
}
[DllExport("PluginExecute", CallingConvention.StdCall)]
static void PluginExecute() {
String data = GetData("<request><primitives subset=\"all\" /></request>");
MessageBox.Show(data, "Hello from C#!");
}
static String GetData(String requestXML) {
TCallBackRecord rec = new TCallBackRecord();
rec.RequestID = PR_GETMEM;
rec.RequestXML = requestXML;
_callback(ref rec);
rec.RequestID = PR_GETDATA;
rec.ResponseXML = Marshal.AllocHGlobal(rec.ResponseSize);
_callback(ref rec);
String data = Marshal.PtrToStringAnsi(rec.ResponseXML);
Marshal.FreeHGlobal(rec.ResponseXML);
return data;
}
static void SetData(String data) {
TCallBackRecord rec = new TCallBackRecord();
rec.RequestID = PR_SETDATA;
rec.RequestXML = data;
_callback(ref rec);
}
}
}
