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# 调用 c++ dll_第1张图片

普通函数

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


}


你可能感兴趣的:(c#,c++,开发语言)