Help needed getting "dl"-functions to work in Win32

P

Pelle Beckman

Hi,

I read a tutorial on function pointers in a swedish computer magazine.
The author used functions named dlopen() and dlsym()
(found in "dlfcn.h" under linux) that gave him the ability to get
function pointers from strings, ie :

typedef int (*fp) (int, int);
(...)
void *handle = dlopen (argv[1], RTLD_LAZY);
fp m = (fp) dlsym (handle, "the_magic_string_method");
(...)
cout << "Result from the_magic_etc... " << (*m)(x, y) << endl;
(...)
dlclose (handle);

The questions:
* What is this?
* How does it work

And perhaps more importantly.
* can it be used with gcc (mingw32) under win32 (winxp)?

Links, tutorials, anyone?
Thanks.

-- Pelle
 
V

Victor Bazarov

Pelle said:
I read a tutorial on function pointers in a swedish computer magazine.
The author used functions named dlopen() and dlsym()
(found in "dlfcn.h" under linux) that gave him the ability to get
function pointers from strings, ie :

typedef int (*fp) (int, int);
(...)
void *handle = dlopen (argv[1], RTLD_LAZY);
fp m = (fp) dlsym (handle, "the_magic_string_method");
(...)
cout << "Result from the_magic_etc... " << (*m)(x, y) << endl;
(...)
dlclose (handle);

The questions:
* What is this?

You should ask in a UNIX/Linux newsgroup, it's not a feature of C++
language, it's a feature of those operating systems. Apparently, the
function 'dlopen' creates a value (of type 'pointer to void') that can
be used later in a call to 'dlsym', which, in turn, returns something
that when converted to a pointer to function can be called like any
other ordinary function.
* How does it work
Somehow.

And perhaps more importantly.
* can it be used with gcc (mingw32) under win32 (winxp)?

Should be OK. Have you tried? If you have, did you get error messages
or did it work? If you haven't tried, why not?
Links, tutorials, anyone?

or any other OS-specific newsgroup
dealing with Linux/UNIX. 'dlopen' and 'dlsym' are not standard C or C++
functions.

V
 
R

red floyd

Pelle said:
Hi,

I read a tutorial on function pointers in a swedish computer magazine.
The author used functions named dlopen() and dlsym()
(found in "dlfcn.h" under linux) that gave him the ability to get
function pointers from strings, ie :

typedef int (*fp) (int, int);
(...)
void *handle = dlopen (argv[1], RTLD_LAZY);
fp m = (fp) dlsym (handle, "the_magic_string_method");
(...)
cout << "Result from the_magic_etc... " << (*m)(x, y) << endl;
(...)
dlclose (handle);

The questions:
* What is this?
* How does it work

And perhaps more importantly.
* can it be used with gcc (mingw32) under win32 (winxp)?

Links, tutorials, anyone?
Thanks.

-- Pelle

Definitely look in comp.unix.programmer. dxXXX() functions are dynamic
library functions. dlopen is (roughly) equivalent to LoadLibrary, and
dlsym is the rough equivalent of GetProcAddress().

Dynamic libraries are OT in comp.lang.c++, so the unix guys should be
able to help you a lot more!
 
R

Ron Natalie

Pelle said:
Hi,

I read a tutorial on function pointers in a swedish computer magazine.
The author used functions named dlopen() and dlsym()
(found in "dlfcn.h" under linux) that gave him the ability to get
function pointers from strings, ie :

The DL fucntions are fatally flawed in that they assume void* is
convertable to pointer-to-function. There is NO PORTABLE way
to make this work, so your stuck figuring out what hacks you might
use on your particular implementation (usually casting to a large
integral type as an itermediary is required, or some equally ugly
kludge).
 
M

Malte Starostik

Ron said:
The DL fucntions are fatally flawed in that they assume void* is
convertable to pointer-to-function. There is NO PORTABLE way
to make this work, so your stuck figuring out what hacks you might
use on your particular implementation (usually casting to a large
integral type as an itermediary is required, or some equally ugly
kludge).
Indeed, that's been annoying me since I first used them...reason is that
the pointer returned by dlsym() can just as well point to any kind of
object, but then the usual case is a function, so two alternatives could
have done the job more cleanly for the user.
This is about the only situation in which I would actually recommend a
C-style cast as it does the Right Thing and isn't as ugly as two
consecutive reinterpret_casts.
OTOH I never really understood why reinterpret_cast between
pointer-to-object and pointer-to-function is not valid.

Cheers,
Malte
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top