Boost logo

Boost :

From: Vesa Karvonen (vesa_karvonen_at_[hidden])
Date: 2002-03-19 11:14:55

>Ok, I'll explain a little more. I have written a utility header which
>allows to load DLLs dynamically without much typing for the user. It
>basically looks like this (of the top of my head...):
>class my_dll : public dynamic_dll_loader
> DECLARE_FUNCTION_2(__stdcall, int, foo, int, float);
> DECLARE_FUNCTION_1(__cdecl, int, bar, double);
> my_dll() : dynamic_dll_loader("path_to_my_dll")
> {
> }
>The base class takes care of loading and unloading the DLL. The
>DECLARE_FUNCTION_N makros insert all the necessary declarations and
>variables and the LOAD_FUNCTION() makros retrieve the function
>pointers needed.

Perhaps I should just tell you how to write the macro, but it seems to me
that you wouldn't need the preprocessor library and could get by with less
code if you did it like this:

  #define DECLARE_FUNCTION(specifiers, return_type, name, params)\
    return_type (specifiers *name) params

The above macro expands to a variable declaration, whose type is a pointer
to a function. It can be written to, and called using the syntax that you

  DECLARE_FUNCTION(__stdcall, int, foo, (int,float));
//int (__stdcall *foo)(int,float);

  DECLARE_FUNCTION(__cdecl, int, bar, (double));
//int (__cdecl *bar)(double);

(As you can see, the macro does not make the code shorter.)

Of course, you might actually want to do something a bit more complicated,
such as an inline wrapper for each function:

  public: int foo(int p0, float p1) {return foo_ptr(p0,p1);}
  private: int (__stdcall *foo_ptr)(int,float);

So that the user can not write to the member. For this task, you might
actually want to use the preprocessor library.

Chat with friends online, try MSN Messenger:
