Boost logo

Boost Users :

Subject: [Boost-users] [function] dynamic function factory
From: Alexander Lamaison (awl03_at_[hidden])
Date: 2010-03-10 14:46:20


I'm trying to write a little wrapper round Windows GetProcAddress (dlsym
equivalent?) that takes a function name by string, loads it dynamically and
returns it as a boost::function. I'm having trouble getting my code to
accept the same type of signatures as boost::function.

E.g.:

template<typename Signature>
boost::function<Signature> proc_address(
    hmodule hmod, const std::string& name)
{
    return boost::function<Signature>(GetProcAddress(hmod, name.c_str());
}

Where GetProcAddress returns a int (*)() which you cast to the function
signature you're expecting. The problem is that when I call:

boost::function<int (int)> func;
func = proc_address<int (int)>(hmod, "GetKeyboardInfo");

The compiler (MSVC 2005) complains about a ) after the int:

error C2144: syntax error : 'int' should be preceded by ')'
error C2563: mismatch in formal parameter list

Am I trying to reuse boost::function the wrong way?

I also tried making a functor instead but I have no idea what to put as the
parameter list for operator():

template<typename Signature>
class dynamic_function
{
public:
    dynamic_function(hmodule hmod, const std::string& name)
          : m_func(GetProcAddress(hmod, name.c_str())) {}

    operator()(...what goes here?...) { return m_func( ... what ... ); }

private:
    boost::function<Signature> m_func;
};

Any help is greatly appreciated.

Alex


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net