Boost logo

Boost Users :

From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2003-12-01 17:23:59


I'm interfacing with several 'C' dynamic link libraries each with the same
interface. The appropriate libraries get loaded/unloaded at run time. The
following is an attempt create a wrapper that returns a function pointer of
the proper static type as declared for the 'C' functions.

//------------------------------
class DLL
{
   HMODULE mDLLHdl;

public:

   ~DLL(){ FreeLibrary( mDLLHdl ); }
    DLL( const std::string& aDLLName )
    :mDLLHdl( LoadLibrary( aDLLName.c_str() ) ){}

   operator bool()const{ return mDLLHdl; }
   operator HMODULE()const{ return mDLLHdl; }

   template< typename TFnc >
   TFnc Fnc( const std::string& aName, TFnc )const
   {
     return (TFnc)GetProcAddress( mDLLHdl, aName.c_str() );
   }
};
//------------------------------

With:

typedef int (FAR WINAPI *FARPROC)();

FARPROC GetProcAddress(
  HMODULE hModule, // handle to DLL module
  LPCSTR lpProcName // function name
);

For example, it's used as follows:
//------------------------------

// declared in an included header with extern "C"
int DoAttach( const char* aName, int attachMode );

DLL lSomeDll( "Some.dll" );

boost::function0<int> lFnc = boost::bind( lSomeDll.Fnc( "DoAttach",
DoAttach ), "Name", 123 );

//------------------------------

Which compiles fine, but results in unresolved external symbols errors
during linking for 'DoAttach' in this example. I can force-link and run the
resulting executable w/out problem. Is there a way to get the type of the
function without the compiler needing to use the function pointer itself?

Thanks, Jeff


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