简体   繁体   中英

Calling unmanaged C++ Class DLL from C#

如何从C#调用非托管C ++类DLL?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM