zhaoguohao / FastDllImport

High performance DllImport mechanism to invoke unmanaged dll from C# managed code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FastDllImport

High performance DllImport mechanism to invoke unmanaged dll from C# managed code.

Introduction

"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.

How to use

We provide 2 options to use "FastDllImportManager":

[Option1]

  1. Add "FastDllImportManager" class to the project
  2. 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);
...
}
  1. 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));
}

[Option2]

  1. Same as Option1 above
  2. 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);
  1. Same as Option1 above.

Now, the Dll call will be accelerated.

Test result

Increase 2*100,000,000 times:

To be improved

  1. 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)
}
  1. For Option2, only support 0/1/2/3 parameters in Dll functions, more parameters will be supported in the future.

About

High performance DllImport mechanism to invoke unmanaged dll from C# managed code.

License:Apache License 2.0