开发者

Calling unmanaged C++ Class DLL from C#

开发者_JAVA百科

How can I call unmanaged C++ Class DLL from C#?


You may want to create a managed C++ wrapper for that class, compile it with /clr (common language runtime support) and then you can use it in C#. You may also want to look at PInvoke.


The CLR doesn't support directly using native C++ classes, it prefers static methods to call via PInvoke or COM interfaces to use via COM interop. So some kind of C++ wrapper is required.


You need to use P/Invoke & its marshalling


For example like this :

public unsafe class CppFunctionImport
{
    [DllImport("ImageProcessingCpp.dll", EntryPoint = "PerformMovingAverage", ExactSpelling = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]//!-!
    public static extern void PerformMovingAverage
    (
        ref byte *image,
        int width,
        int height,
        int stride,
        int kernelSize
    );
}

Create your small wrapper, import desired functions and call

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜