tr1::函数WINAPI

发布于 2024-08-20 09:44:15 字数 514 浏览 6 评论 0原文

如何将 tr1::function 与 WINAPI 调用约定一起使用? (至少在 Windows 中)。我可以使用 Visual C++ 9 SP1 TR1 或 BOOST 的...

typedef void (WINAPI *GetNativeSystemInfoPtr)(LPSYSTEM_INFO);
HMODULE h = LoadLibrary (_T("Kernel32.dll"));
GetNativeSystemInfoPtr fp = (GetNativeSystemInfoPtr) GetProcAddress (h,"GetNativeSystemInfo");
SYSTEM_INFO info;
fp(&info); //works!

// This doesn't compile 
function< void WINAPI (LPSYSTEM_INFO) > fb = (GetNativeSystemInfoPtr) GetProcAddress (h,"GetNativeSystemInfo");

How can I use tr1::function with WINAPI calling convention ? (at least in windows). I can use visual c++ 9 SP1 TR1 or BOOST's one...

typedef void (WINAPI *GetNativeSystemInfoPtr)(LPSYSTEM_INFO);
HMODULE h = LoadLibrary (_T("Kernel32.dll"));
GetNativeSystemInfoPtr fp = (GetNativeSystemInfoPtr) GetProcAddress (h,"GetNativeSystemInfo");
SYSTEM_INFO info;
fp(&info); //works!

// This doesn't compile 
function< void WINAPI (LPSYSTEM_INFO) > fb = (GetNativeSystemInfoPtr) GetProcAddress (h,"GetNativeSystemInfo");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2024-08-27 09:44:15

编译:

#include <boost/function.hpp>
#include <windows.h>


int main(void)
{
    typedef void (WINAPI *GetNativeSystemInfoPtr)(LPSYSTEM_INFO);
    HMODULE h = LoadLibrary (("Kernel32.dll"));
    GetNativeSystemInfoPtr fp = (GetNativeSystemInfoPtr) GetProcAddress (h,"GetNativeSystemInfo");
    SYSTEM_INFO info;
    fp(&info); //works!

    boost::function< void (LPSYSTEM_INFO) > fb = (GetNativeSystemInfoPtr) GetProcAddress (h,"GetNativeSystemInfo");
    SYSTEM_INFO info2;
    fb(&info2);


    return 0;
}

“info”上的内容与“info2”上的内容相同,所以它似乎有效。

我的理解是,用于实例化 boost::function 的参数是其operator()的签名。它与它包装的函数对象的函数签名没有严格关系。否则,它的好处就会消失,因为 boost::function 的实用性正是能够将任何可调用的东西包装在统一的接口后面,而不管最终目标类型的细节如何。

This compiles:

#include <boost/function.hpp>
#include <windows.h>


int main(void)
{
    typedef void (WINAPI *GetNativeSystemInfoPtr)(LPSYSTEM_INFO);
    HMODULE h = LoadLibrary (("Kernel32.dll"));
    GetNativeSystemInfoPtr fp = (GetNativeSystemInfoPtr) GetProcAddress (h,"GetNativeSystemInfo");
    SYSTEM_INFO info;
    fp(&info); //works!

    boost::function< void (LPSYSTEM_INFO) > fb = (GetNativeSystemInfoPtr) GetProcAddress (h,"GetNativeSystemInfo");
    SYSTEM_INFO info2;
    fb(&info2);


    return 0;
}

and the contents on "info" is the same that the one of "info2", so it seems to work.

My understanding is that the parameter used to instantiate a boost::function is the signature of its operator(). It is not strictly related to the signature of the function of function object that it wraps. Otherwise, its benefits would be lost, since boost::function's utility is precisely to be able to wrap anything that is callable behind a uniform interface, regardless of the details of the final target's type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文