By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Pelle Beckman wrote:
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?


news:comp.os.linux.development.apps or any other OS-specific newsgroup
dealing with Linux/UNIX. 'dlopen' and 'dlsym' are not standard C or C++
functions.

V
Jul 23 '05 #2

P: n/a
Pelle Beckman wrote:
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!
Jul 23 '05 #3

P: n/a
Pelle Beckman wrote:
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).
Jul 23 '05 #4

P: n/a
Ron Natalie schrieb:
Pelle Beckman wrote:
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).

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
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.