小编Dav*_*ide的帖子

从 C++ DLL 调用时,带有参数的 C# 委托回调会导致 AccessViolation

我有一个非托管 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)

.net c# c++ pinvoke unmanaged

0
推荐指数
1
解决办法
1104
查看次数

标签 统计

.net ×1

c# ×1

c++ ×1

pinvoke ×1

unmanaged ×1