发送回调函数从c#到C结构中的dll

本文关键字:结构 dll 回调 函数 | 更新日期: 2023-09-27 18:17:49

我试图发送一个结构体,其中包括从c#回调函数到c++本地DLL的引用,我正在做什么有什么问题>?,结构体通过!,所有的回调引用都不是空的,但当调用它时,我没有从DLL到达c#函数:(

class Native_Wrraper
{
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate int LogMessageCallback(string mMyStruct);
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate int ActionCompleteCallback(int actionId, int actionResult);
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate string GetFilePathCallback(string filter);

其结构:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct InitCallBacksStruct
    {
        [MarshalAs(UnmanagedType.FunctionPtr)]
        public LogMessageCallback logMessageCallback;
        [MarshalAs(UnmanagedType.FunctionPtr)]
        public ActionCompleteCallback actionCompleteCallback;
        [MarshalAs(UnmanagedType.FunctionPtr)]
        public GetFilePathCallback getPath;
        //UpdaterCallback updaterCallback;
    };
    [DllImport("_Wrraper.dll", CharSet = CharSet.Unicode)]
    public static extern int InitDll(InitCallBacksStruct mMyStruct);
    [DllImport("_Wrraper.dll", CharSet = CharSet.Unicode)]
    public static extern int StartTestAPIDll(string TestHeader, int someNumber);

这里是DLL应该调用的回调函数(enter)

    public static int FuncLogMessageCallback(string mMyStruct)
    {
        return 1;
    }
    public static int FuncActionCompleteCallback(int actionId, int actionResult)
    {
        return 1;
    }
    public static string FuncGetFilePathCallback(string filter)
    {
        return "The Call back worked!";
    }
}

测试按钮:

    private void btnTest_Click(object sender, EventArgs e)
    {
        Native_Wrraper.InitCallBacksStruct str =  new Native_Wrraper.InitCallBacksStruct();
        str.actionCompleteCallback = new Native_Wrraper.ActionCompleteCallback(Native_Wrraper.FuncActionCompleteCallback);
        str.getPath = new Native_Wrraper.GetFilePathCallback(Native_Wrraper.FuncGetFilePathCallback);
        str.logMessageCallback = new Native_Wrraper.LogMessageCallback(Native_Wrraper.FuncLogMessageCallback);
        Native_Wrraper.InitDll(str);
        Native_Wrraper.StartTestAPIDll(DateTime.Now.ToShortTimeString(), 0xEE);
    }

:

结构:

#define DLL __declspec(dllexport)
typedef void (__stdcall * ActionCompleteCallback)(int actionId, int actionResult);
typedef char* (__stdcall * GetFilePathCallback)(char* filter);
typedef void (__stdcall * LogMessageCallback)(char* message);

//This structure include all the references to a call backs functions 
struct InitCallBacksStruct
{
    LogMessageCallback logMessageCallback;
    ActionCompleteCallback actionCompleteCallback;
    GetFilePathCallback getPath;
};
#endif
API:

#ifdef __cplusplus
extern "C"
{
#endif
#define DLL __declspec(dllexport)
DLL int InitDll(InitCallBacksStruct mMyStruct);
DLL int StartTestAPIDll( __in LPWSTR TestHeader, int someNumber);
#ifdef __cplusplus
}
#endif

从DLL调用的例子:

if(this->mInitCallBacksStruct.actionCompleteCallback)
{
    this->mInitCallBacksStruct.actionCompleteCallback(someNumber,0xCC);
}

发送回调函数从c#到C结构中的dll

对不起,伙计们,代码工作完美!

问题是c#中的断点!!:)

添加对话框。

所以这将是一个模板和/或答案的人谁正在寻找一个解决方案:

如何从c#发送回调函数引用列表,使用结构内部的一个函数调用到本机DLL。

ONLY,请注意,你应该在c#中全局地保持回调,最好是静态的,而不是在btnTest_Click函数中!为了确保引用不会消失,意味着对回调函数的引用应该是全局变量或静态变量,例如:

static Native_Wrraper.ActionCompleteCallback refFunction = new Native_Wrraper.ActionCompleteCallback(Native_Wrraper.FuncGetFilePathCallback);

然后发送到DLL

str.actionCompleteCallback = refFunction

祝你好运!约瑟夫。