C#调用C++带指针参数函数

C++ DLL代码

typedef void (__stdcall *fnSavedCB)(int savedLen);

__declspec(dllexport)
void __stdcall testSaveData(fnSavedCB fn)
{
    for (int i = 0; i < 10; ++i) {
        fn((i+1)*1024);
    }
}

注意在.def文件中声明testSaveData导出函数

C#调用代码

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void fnSaveCB(int savedLen);

[DllImport("testDLL", CallingConvention = CallingConvention.StdCall, EntryPoint = "testSaveData")]
public static extern void testSaveData(fnSaveCB);

//调用测试
public void SaveCB(int saveLen)
{
    System.Diagnostics.Trace.WriteLine("Saved:" + savedLen.ToString());
}

public void TestDll()
{
    testSaveData(SaveCB);
}

参考文章

https://stackoverflow.com/questions/5155180/changing-a-c-sharp-delegates-calling-convention-to-cdecl
https://www.codeproject.com/Articles/12512/Using-the-CDECL-calling-convention-in-C-changing

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页