High performance DllImport mechanism to invoke unmanaged dll from C# managed code.
"DllImportAttribute", also known as PInvoke mechanism in .net framework, is used to call functions exported from native dll. However, PInvoke procedure executes a lot of additional tasks from .net managed code to unmanged Dll code, when calling Dll with high frequency, PInvoke mechanism causes low performance.
"FastDllImportManager" is used to solve the problem, it can provide high efficiency function-call from .net to Dll. The performance is almost approaching to call C# built-in functions.
We provide 2 options to use "FastDllImportManager
":
- Add "
FastDllImportManager
" class to the project - Add an
[FastDllImportRedirected]
attribute on the[DllImport]
function, such as:
public partial class Form1 : Form
{
...
[FastDllImportRedirected]
[DllImport("MyDll.dll")]
public static extern int MyAdd1(int a);
...
}
- Add a static construction function to call FastDllImportManager.Instance.Register(typeof(XXX)) for initialization, such as:
(XXX is the class which use declare to import the DLL)
static Form1()
{
FastDllImportManager.Instance.Register(typeof(Form1));
}
- Same as Option1 above
- Use the following declaration:
[FastDllImport("MyDll.dll")]
public delegate int F1(int arg1); static F1 MyAdd1;
instead of PInvoke declaration:
[DllImport("MyDll.dll")]
public static extern int MyAdd1(int a);
- Same as Option1 above.
- Do not automatically support array parameter, if want to use array parameter, please use "
fixed
" key word to fix the array, like this:
fixed(byte* p = (byte*)&arr[0])
{
DllFucn(p)
}
- For Option2, only support 0/1/2/3 parameters in Dll functions, more parameters will be supported in the future.