C# 调用 c++ dll

C# 调用 c++ dll

首先 c++ 库 dll 要定义

  • 代码中定义 CPP_EXPORTS

    #ifdef LASERSDK_EXPORTS
    #define CPP_EXPORTS __declspec(dllexport)
    #else
    #define CPP_EXPORTS __declspec(dllimport)
    #endif

  • 编译器定义

LASERSDK_EXPORTS

普通函数

  • c++

    extern "C" CPP_EXPORTS int __stdcall Add(int n1, int n2);

    int __stdcall Add(int n1, int n2)
    {
    return n1 + n2;
    }

  • c#

    [DllImport("SampleCppWrapper.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern int Add(int n1, int n2);

    // 使用
    int res = Add(1, 1);

结构体

  • c++

    struct Vector4
    {
    float A, B, C;
    const char* D = nullptr;
    };

    extern "C" CPP_EXPORTS Vector4 __stdcall Add2(int n1, int n2);
    extern "C" __declspec(dllexport) void __stdcall Add5(Vector4 vec);

    // 结构体指针
    // c++ 开启的内存,c++ 回收 Add7_Clear
    extern "C" __declspec(dllexport) Vector4* __stdcall Add7();
    extern "C" __declspec(dllexport) void __stdcall Add7_Clear(Vector4* vc);

    // cpp
    Vector4 __stdcall Add2(int n1, int n2)
    {
    Vector4 sd;
    sd.A = 12;
    sd.B = 13;
    sd.D = "asdfssssws";
    return sd;
    }

    void __stdcall Add5(Vector4 vec)
    {
    printf("\n Add5:: \n");

      printf("%f ,", vec.A);
      printf("%s ,", vec.D);
    
      printf("\n");
    

    }

    Vector4* __stdcall Add7()
    {
    Vector4* vec = new Vector4();
    vec->A = 1212;
    vec->D = new char[10000];
    return vec;
    }

    void __stdcall Add7_Clear(Vector4* vc)
    {
    if (vc)
    {
    if (vc->D)
    {
    delete vc->D;
    }
    delete vc;
    }
    }

  • c#

    [DllImport("SampleCppWrapper.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern Vector4 Add2(int n1, int n2);

    [DllImport("SampleCppWrapper.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern void Add5(Vector4 vec);

    [DllImport("SampleCppWrapper.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern IntPtr Add7();
    [DllImport("SampleCppWrapper.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern void Add7_Clear(IntPtr vc);

    // 使用
    Vector4 sd = Add2(1, 1);
    string s = Marshal.PtrToStringAnsi(sd.D);
    Console.WriteLine("asdfasdfasdf : " + s);

    Vector4 _vec = new Vector4();
    _vec.A = 123;
    _vec.D = Marshal.StringToHGlobalAnsi("dddddddddsdsdsdsdsbsbsbsbsbsbsb");

    Add5(_vec);
    Marshal.FreeHGlobal(_vec.D);

    IntPtr add7 = Add7();

    Vector4 _vec7 = (Vector4)Marshal.PtrToStructure(add7, typeof(Vector4));
    Console.WriteLine(_vec7.A);

    Add7_Clear(add7);

数组

  • c++

    extern "C" CPP_EXPORTS int __stdcall arraytest(float* vals, int len);

    int __stdcall arraytest(float* vals, int len)
    {

      for (int i = 0; i < len; i++)
      {
          vals[i] = i;
      }
      return 0;
    

    }

  • c#

    [DllImport("SampleCppWrapper.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern int arraytest( float[] datas,int len);

    // use

    float[] one_outlines = new float[1024];
    int res = arraytest(one_outlines, 1024);

回调函数

  • c++

    typedef void (callback_outline)(int, float, int);
    extern "C" CPP_EXPORTS void __stdcall laser_set_callback_outline_handle(callback_outline cb);

    callback_outline _callback_outline;
    float vals[1024];

    void __stdcall laser_set_callback_outline_handle(callback_outline cb)
    {
    _callback_outline = cb;

      printf("laser_set_callback_outline_handle suc \n");
    
      if (_callback_outline)
      {
      	_callback_outline(1, vals, 1024);
      }
    

    }

  • c#

    // 映射c++函数
    [DllImport("SampleCppWrapper.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern void laser_set_callback_outline_handle(CallbackOutlineDelegate call);

    // 创建委托
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void CallbackOutlineDelegate(int handle, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1024)] float[] ydata, int len);

    // use

    // 回调函数
    public static void CallbackOutlineFunc(int handle, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1024)] float[] ydata, int len)
    {

    }

    static LaserSdk.CallbackOutlineDelegate outlineDelegate;
    static void Main1(string[] args)
    {
    outlineDelegate = new LaserSdk.CallbackOutlineDelegate(CallbackOutlineFunc);
    laser_set_callback_outline_handle(outlineDelegate);

    }

相关推荐
童先生14 分钟前
Go 项目中实现类似 Java Shiro 的权限控制中间件?
开发语言·go
lulu_gh_yu15 分钟前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
Re.不晚39 分钟前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
老秦包你会41 分钟前
Qt第三课 ----------容器类控件
开发语言·qt
凤枭香44 分钟前
Python OpenCV 傅里叶变换
开发语言·图像处理·python·opencv
ULTRA??1 小时前
C加加中的结构化绑定(解包,折叠展开)
开发语言·c++
远望清一色1 小时前
基于MATLAB的实现垃圾分类Matlab源码
开发语言·matlab
confiself1 小时前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言
凌云行者1 小时前
OpenGL入门005——使用Shader类管理着色器
c++·cmake·opengl
XiaoLeisj1 小时前
【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
java·开发语言·java-ee