views:

161

answers:

1

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");
+2  A: 

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.

Éric Malenfant
Thank you for the solution and for the explanation!!
QbProg