我有一个非托管 C++ DLL 和一个使用 P/Invoke 进行通信的 .net 应用程序。我需要从 C++ DLL 更新 .net 应用程序中的字典。
我尝试通过以下方式做到这一点:
public delegate void MyDelegate(string address, string username);
[DllImport("MyDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void SaveMyDelegate(MyDelegate callback);
private static SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
public void save_delegate() {
SaveMyDelegate(delegate(string address, string username) {
if (username != "") {
dict.Add(address, username);
}
});
}
Run Code Online (Sandbox Code Playgroud)
在 C++ 方面我有:
typedef void (*MANAGED_CALLBACK)(string user_address, string username);
extern "C" __declspec(dllexport) void SaveMyDelegate(MANAGED_CALLBACK callback);
MANAGED_CALLBACK my_callback;
void SaveMyDelegate(MANAGED_CALLBACK callback) { …Run Code Online (Sandbox Code Playgroud)