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);

    }

相关推荐
CYBEREXP200813 分钟前
MacOS M3源代码编译Qt6.8.1
c++·qt·macos
ZSYP-S32 分钟前
Day 15:Spring 框架基础
java·开发语言·数据结构·后端·spring
yuanbenshidiaos35 分钟前
c++------------------函数
开发语言·c++
yuanbenshidiaos39 分钟前
C++----------函数的调用机制
java·c++·算法
程序员_三木1 小时前
Three.js入门-Raycaster鼠标拾取详解与应用
开发语言·javascript·计算机外设·webgl·three.js
是小崔啊1 小时前
开源轮子 - EasyExcel01(核心api)
java·开发语言·开源·excel·阿里巴巴
tianmu_sama1 小时前
[Effective C++]条款38-39 复合和private继承
开发语言·c++
黄公子学安全1 小时前
Java的基础概念(一)
java·开发语言·python
liwulin05061 小时前
【JAVA】Tesseract-OCR截图屏幕指定区域识别0.4.2
java·开发语言·ocr
jackiendsc1 小时前
Java的垃圾回收机制介绍、工作原理、算法及分析调优
java·开发语言·算法