Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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 DLL中的回调?_C_Dll_Callback - Fatal编程技术网

C DLL中的回调?

C DLL中的回调?,c,dll,callback,C,Dll,Callback,我在C DLL(静态void\uu stdcall)中有一个回调。我希望另一个程序注册它(通过将func ptr传递给它),然后在DLL中调用calback。我到目前为止运气不好。但是,如果它在一个常规C++程序中,则同样的回调工作。我现在想知道在DLL中调用是否可能。任何帮助都将不胜感激 谢谢 添加一些代码: C#应用程序: C动态链接库: //链接到externalLibrary.lib #include "externalLibrary.h" typedef void (_

我在C DLL(静态void\uu stdcall)中有一个回调。我希望另一个程序注册它(通过将func ptr传递给它),然后在DLL中调用calback。我到目前为止运气不好。但是,如果它在一个常规C++程序中,则同样的回调工作。我现在想知道在DLL中调用是否可能。任何帮助都将不胜感激

谢谢

添加一些代码:

C#应用程序:

C动态链接库: //链接到externalLibrary.lib

    #include "externalLibrary.h"
    typedef void (__stdcall CallbackFunc)(void);
    CallbackFunc* func;    //global in DLL

    //Exported

    extern "C" __declspec(dllexport) void DLL_SetCallback(CallbackFunc* funcptr)
    {

      //setting the function pointer

        func = funcptr;
        return;
    }

//Exported

extern "C" __declspec(dllexport) void RegisterEventHandler(Target, Stream,&ProcessEvent , NULL)
{
     //ProcessEvent is func to be caled by 3rd party callback

     //Call third-party function to register &ProcessEvent func-ptr (succeeds)

     ...
     return;
}        

//This is the function which never gets called from the 3rd party callback
//But gets called when all the code in the DLL is moved to a standard C program.

void  __stdcall ProcessEvent (params..)
{
    //Do some work..

        func();         //Call the C# callback now


    return;
}

你的问题有点让人困惑。你是说在DLL中有一个未导出的函数,你希望获取它的地址并传递给一些外部代码,这些代码将调用它?这样做完全合理。如果它不起作用,这里有一些东西要看

1) 确保函数定义、DLL中函数指针的类型以及外部代码中声明的函数指针的类型的调用约定是正确的

2) 在DLL中,尝试通过传递给外部代码的相同函数指针调用回调


3) 如果(1)正确,而(2)有效,则启动调试器,在试图从外部代码调用回调的行上放置一个断点,然后进入反汇编。请仔细查看通话结果。

请告诉我们发生了什么。它不能链接吗?编译?崩溃?添加更多信息:函数未导出,它被传递给来自头的函数(dll链接到lib文件)。该函数注册该回调,该回调应该在该函数有任何输出时被调用。它编译并运行,但不会触发回调。然而,我在一个C程序中尝试了相同的机制,它成功了(回调被触发)。我使用的调用约定是_stdcall。另外,我对回调使用static(在独立的C程序中工作),但在dll内部不工作。我正在使用一个C程序来测试DLL。(一些导出的函数从C#调用DLL)此图也可能有帮助:lib file(1)->DLL(2)->C#app。箭头表示回调。(2) 工作(所以我之前没有提到,但为了完整起见,我在这里这么做),但是(1)没有。如果我的原始查询不起作用,是否可以将委托函数(ptr)的func ptr从C#app传递到DLL中的外部函数?作为一个示例,一点代码可能会有所帮助。(1)是正确的,(2)起作用,不幸的是,我没有访问调用回调的第三方代码的权限。。
    #include "externalLibrary.h"
    typedef void (__stdcall CallbackFunc)(void);
    CallbackFunc* func;    //global in DLL

    //Exported

    extern "C" __declspec(dllexport) void DLL_SetCallback(CallbackFunc* funcptr)
    {

      //setting the function pointer

        func = funcptr;
        return;
    }

//Exported

extern "C" __declspec(dllexport) void RegisterEventHandler(Target, Stream,&ProcessEvent , NULL)
{
     //ProcessEvent is func to be caled by 3rd party callback

     //Call third-party function to register &ProcessEvent func-ptr (succeeds)

     ...
     return;
}        

//This is the function which never gets called from the 3rd party callback
//But gets called when all the code in the DLL is moved to a standard C program.

void  __stdcall ProcessEvent (params..)
{
    //Do some work..

        func();         //Call the C# callback now


    return;
}