DllImport or LoadLibrary for best performance

后端 未结 4 1575
温柔的废话
温柔的废话 2020-12-30 12:09

I have external .DLL file with fast assembler code inside. What is the best way to call functions in this .DLL file to get best performance?

4条回答
  •  隐瞒了意图╮
    2020-12-30 12:41

    Assuming your target platform is the same as said native dll. You can use DLLImport to pinvoke LoadLibrary and use LoadLibrary to load the native dll into your process. Then use DllImport to pinvoke GetProcAddress.

    Then you can define delegates for all the methods exported in said dll that you want to call.

    Next you use the Marshal.GetDelegateForFunctionPointer to set your delegate from GetProcAddress.

    You create a static class that does this stuff once in the constructor. Then you can call your delegates to invoke the native exported functions in the dll, without having DllImport on everything. Much cleaner, and I'm pretty sure it's a lot faster and will probably completely bypass before mentioned parameter checks.

    SO you would have a slow initialization, but once loaded, would run fast imo. Haven't tested this.

    Here's a blog on it from my source.

    http://blogs.msdn.com/b/jonathanswift/archive/2006/10/03/dynamically-calling-an-unmanaged-dll-from-.net-_2800_c_23002900_.aspx

提交回复
热议问题