C++函数定义
extern "C" __declspec(dllexport) void ThundbirdPhaseLocalFolder(const string* localFolderPaths,const string* zipFolders, bool (*isProcess)()) { const char* localFolderPath = localFolderPaths->c_str(); const char* zipFolder = zipFolders->c_str(); }
C#函数定义
namespace TestThunderbirdZipperCSharp { class CPPDLL { [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate bool IsProcess(); [DllImport("ThunderbirdLocalFolderZipper.dll")] public static extern void ThundbirdPhaseLocalFolder([MarshalAs(UnmanagedType.LPWStr)]string localFolderPath, [MarshalAs(UnmanagedType.LPWStr)]string zipFolder, [MarshalAs(UnmanagedType.FunctionPtr)] IsProcess isProcess); } }
C#调用
namespace TestThunderbirdZipperCSharp { class Program { static bool flag = true; static bool callback() { return flag; } static void Main(string[] args) { string localFolderPath = ""; string zipFolder = ""; CPPDLL.ThundbirdPhaseLocalFolder(localFolderPath, zipFolder, callback); } } }
本文介绍了一个具体的跨平台编程案例,展示了如何使用C++定义一个函数,并在C#中通过PInvoke机制调用该函数,涉及到字符串参数传递及委托回调的处理。
2173

被折叠的 条评论
为什么被折叠?



