How to implement callback functions from C++ DLL

Hi,
I created a DLL from a C++ project and wanted to use its functions in Unity, but I still have difficulties with callbacks. I keep on getting the error EntryPointNotFoundException: WacomMTRegisterAttachCallback.
Here the C++ file:

extern "C" {
#include <stdlib.h>

typedef enum _WacomMTError
{
    WMTErrorSuccess                = 0,
    WMTErrorDriverNotFound        = 1,
    WMTErrorBadVersion            = 2,
    WMTErrorAPIOutdated            = 3,
    WMTErrorInvalidParam            = 4,
    WMTErrorQuit                    = 5,
    WMTErrorBufferTooSmall        = 6
} WacomMTError;

typedef void (__stdcall * WMT_ATTACH_CALLBACK)(WacomMTCapability deviceInfo, void *userData);

__declspec(dllexport) WacomMTError __stdcall WacomMTInitialize(int libraryAPIVersion);

__declspec(dllexport) WacomMTError __stdcall WacomMTRegisterAttachCallback(WMT_ATTACH_CALLBACK attachCallback, void *userData);

}

The C# file in Unity:

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
using UnityEngine;


enum WacomMTError
{
    WMTErrorSuccess = 0,
    WMTErrorDriverNotFound = 1,
    WMTErrorBadVersion = 2,
    WMTErrorAPIOutdated = 3,
    WMTErrorInvalidParam = 4,
    WMTErrorQuit = 5,
    WMTErrorBufferTooSmall = 6
}


public class PluginTest : MonoBehaviour {

    [DllImport("WacomFeel")]
    private static extern WacomMTError WacomMTInitialize(int libraryAPIVersion);

    private delegate void AttachCallbackDel(WacomMTCapability deviceInfo, IntPtr userData);
    [DllImport("WacomFeel")]
    private static extern WacomMTError WacomMTRegisterAttachCallback(AttachCallbackDel attachCallback, IntPtr userData);

    private AttachCallbackDel attachCallbackDel;

    private static void AttachCallback(WacomMTCapability deviceInfo, IntPtr userData)
    {
        //do something
    }

    void Start () {
        WacomMTError test = WacomMTInitialize(4);
        Debug.Log(test); //returns success

        attachCallbackDel = new AttachCallbackDel(AttachCallback);

        test = WacomMTRegisterAttachCallback(attachCallbackDel, IntPtr.Zero);
        Debug.Log(test); //no return due to error
    }

}

The function WacomMTInitialize works fine, but calling WacomMTRegisterAttachCallback brings an error. Any suggestions?

My understanding is you cannot call back like that. You have to use the UnitySendMessage() method on the native side, and Unity will get the call to your C# function, but on the next frame.

See here:

This is specifically the iOS plugins page, but I believe it applies to ALL native-C# interop under Unity.

Quote:

Calling C# back from native code

Unity iOS supports limited native-to-managed callback functionality via UnitySendMessage:

UnitySendMessage(“GameObjectName1”, “MethodName1”, “Message to send”);
This function has three parameters: the name of the target GameObject
, the script method to call on that object and the message string to pass to the called method.

Known limitations:

Only script methods that correspond to the following signature can be called from native code: function MethodName(message:string)
Calls to UnitySendMessage are asynchronous and have a delay of one frame.

Are you sure?

In this thread someone seemed to make it work, and he used quite a similar approach: