开发者

tr1::function WINAPI

开发者 https://www.devze.com 2022-12-19 10:32 出处:网络
How can I use tr1::function with WINAPI calling convention ? (at least开发者_StackOverflow中文版 in windows). I can use visual c++ 9 SP1 TR1 or BOOST\'s one...

How can I use tr1::function with WINAPI calling convention ? (at least开发者_StackOverflow中文版 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");


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号