473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
4 1500
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: marco | last post by:
Hello, I'm putting together a php webpage which is parsing my (.html) bookmarks list. I want to give them a new lay-out with php and CSS. My question is: How can I make a function that counts...
2
by: Greg Smethells | last post by:
What exactly does "*values" mean in the following code? Is this a pointer to a PyList? Why can I not find good documentation on this any where? I must be blind: >>> from struct import * >>>...
3
by: Pro Grammer | last post by:
Hello, all, I am not sure if this is the right place to ask, but could you kindly tell me how to "load" a shared object (like libx.so) into python, so that the methods in the .so can be used? That...
2
by: A Hess | last post by:
Today, my wife was chatting with a friend and her friend was telling her about an irc script that she just "needed" and then directed her to this download page:...
4
by: Cheng Mo | last post by:
I just swtiched my working language from java to C++. In java, there is one mechanism called reflect which enable us to have knowledge of internals of a given class at run-time. That's one basic...
3
by: eiselekd | last post by:
Does anyone know about a code snippet that can be used to output a backtrace just like gdb's "backtrace" command does. One that can be called on a segmentation fault. (without resolving symbol...
3
by: Maxwell2006 | last post by:
Hi, I am using Visual Studio 2005. I have added a DLL into GAC. I can confirm that by going to C:\WINDOWS\assembly and see the assembly name.
3
by: longhair | last post by:
I have a page that's causing me some problems in FireFox 1.5 but not IE6. I have a <dl> with the <dt> and <dd> both within defined with "float: left" to have a table-like look. See it at...
8
MMcCarthy
by: MMcCarthy | last post by:
Hi everyone I had an interesting problem today with a client. I have set up security and privilege on a database depending on NT User Login. However, I've only just realised that the code...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.