Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要使用C#/C互操作解决AccessViolationException问题的帮助吗_C#_Interop_Libmosquitto - Fatal编程技术网

需要使用C#/C互操作解决AccessViolationException问题的帮助吗

需要使用C#/C互操作解决AccessViolationException问题的帮助吗,c#,interop,libmosquitto,C#,Interop,Libmosquitto,我需要一些帮助来修复持久的AccessViolationException 给一个像这样的c签名 struct message { char *topic; void *payload; int payloadlen; }; __declspec(dllexport) void callback_set(struct data *dat, void (*on_publish)(struct data *, void *, const struct message *))

我需要一些帮助来修复持久的AccessViolationException

给一个像这样的c签名

struct message {
    char *topic;
    void *payload;
    int payloadlen;
};

__declspec(dllexport) void callback_set(struct data *dat, void (*on_publish)(struct data *, void *, const struct message *));
我有这个C#

公共结构消息
{
公共字符串主题;
公共IntPtr有效载荷;
公共int payloadlen;
};
/*互操作访问*/
发布时公共委托无效(IntPtr dat、IntPtr usrData、IntPtr messageData);
[DllImport(dllPath,CallingConvention=CallingConvention.Cdecl)]
公共外部静态无效回调集合(IntPtr dat、IntPtr回调);
/*我们的在线发布的实现*/
公共静态void MessageHandler(IntPtr dat、IntPtr usrData、IntPtr messageData)
{
var instance=(message)Marshal.PtrToStructure(messageData,typeof(message));
string test=instance.topic;//我最终创建了一个项目,将C项目包装到.NET

<>我发现使用C++管理非托管代码要容易得多,我最终得到了一个很好的互操作到C类,因为我的类成为.NET Access。 我会推荐这条路径,并且会自己做——下次我需要将C#与C库集成时

    public struct message 
    {
        public string topic;
        public IntPtr payload;
        public int payloadlen;
    };


    /* INTEROP ACCESS */
    public delegate void on_publish(IntPtr dat, IntPtr usrData, IntPtr messageData);

    [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)]
    public extern static void callback_set(IntPtr dat, IntPtr callback);

    /* IMPLEMENTATION OF OUR on_publish*/
    public static void MessageHandler(IntPtr dat, IntPtr usrData, IntPtr messageData)
    {
        var instance = (message)Marshal.PtrToStructure(messageData, typeof(message));
        string test = instance.topic; // <-- this is successfully received
        Console.WriteLine("message rec " + test);
    } //<-- as soon as I exit, the dll blows up with access violation


    /* REGISTERING MESSAGEHANDLER AS ON_PUBLISH */
    public static void RegisterMessageHandler(IntPtr dat) //<-- I promise, the pointer I send here is valid and not modified
    {
        messageHandler = new on_publish(MessageHandler);
        messageHandlerPtr = Marshal.GetFunctionPointerForDelegate(messageHandler);
        callback_set(dat, messageHandlerPtr); //<-- if I do not call this, everything works, no ADE
        Console.WriteLine("message handler registered");
    }
    //just tried to move to scope in order to retain their managed memory loc
    private static IntPtr messageHandlerPtr;  
    private static on_publish messageHandler;