|
Boost : |
From: Steven Ketcham (sketcham_at_[hidden])
Date: 2003-04-22 15:00:56
Experimenting with boost::bind and boost::function using Window's
GetProcAddress(...) function. I don't really see an advantage of boost over
the regualar function pointer in this case. Is there a better way to do the
boost version?
Thanks!
/// DllFunction: int __stdcall DllFunction(int param);
//sample 1
#define BOOST_FUNCTION_NO_DEPRECATED
#define BOOST_BIND_ENABLE_STDCALL
#include "./boost/function.hpp"
#include "./boost/bind.hpp"
void MyClass::DoTest1()
{
MyLoadLibrary myLoadLibrary( "MyDll.dll" );
if ( myLoadLibrary.IsOpen() == true )
{
//sample function - bFunction is the typedef name
typedef boost::function1<int, int> bFunction;
bFunction f = boost::bind(reinterpret_cast< int
(*)(int)>(myLoadLibrary.GetFunction("DllFunction")), _1);
//overloaded() is called
if ( f )
{
int param = 0;
int result = f(param);
}
}
}
//sample2
void MyClass::DoTest2()
{
MyLoadLibrary myLoadLibrary( "MyDll.dll" );
if ( myLoadLibrary.IsOpen() == true )
{
//sample function - pFunction is the typedef name
typedef int (__stdcall *pFunction)(int);
pDsiGrab f = NULL;
f =
reinterpret_cast<pDsiGrab>(myLoadLibrary.GetFunction("DllFunction"));
if ( f != NULL )
{
int param = 0;
//dereference pointer
int result = (*f)(param);
}
}
}
class MyLoadLibrary
{
private:
HINSTANCE f_hDLL; // Handle to DLL
public:
MyLoadLibrary()
{
}
~MyLoadLibrary()
{
this->Close();
}
void Close()
{
if ( f_hDLL != NULL )
{
FreeLibrary(f_hDLL);
}
}
bool Open(const std::string & dllName)
{
this->Close();
f_hDLL = LoadLibrary(dllName.c_str());
return (f_hDLL != NULL);
}
bool IsOpen() const
{
return (f_hDLL != NULL);
}
FARPROC GetFunction(const std::string & functionName)
{
FARPROC r = NULL;
if ( f_hDLL != NULL )
{
r = GetProcAddress(f_hDLL, functionName.c_str());
}
return r;
}
};
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk