Runtime modular programming
Hi all,
Can anybody advice me if its possible to make a program modular and have the components (modules) be added in runtime
The language of preference is C/C++
Thanks for help
This sounds like an OS kind of question to me. On Win32 use the LoadLibrary/FreeLibrary and GetProcAddress API''s.
Yes, check the man pages on dlopen() or LoadLibrary() depending on your OS.
Both basically return a library handle that you can then use with dlsymb() or GetProcAddress() to get a pointer on a named function in the library.
ex: (windows)
{
double (myfunction*)( char* c );
HINSTANCE hlib = LoadLibrary( "MyLib.dll" );
if ( !hlib ) exit( EXIT_FAILURE );
myfunction = GetProcAddress( hlib, "MyFunctinInMyLib" );
if ( !myfunction ) exit( EXIT_FAILURE );
double x = myfunction( "Hail Eris! All Hail Discordia!" );
}
Now YOU and YOU only are responsible for having the right type of function pointer (type checking... what''s that?) and ensuring the existence of the name (beware of C++ name mangling, extern "C" is your friend)
Both basically return a library handle that you can then use with dlsymb() or GetProcAddress() to get a pointer on a named function in the library.
ex: (windows)
{
double (myfunction*)( char* c );
HINSTANCE hlib = LoadLibrary( "MyLib.dll" );
if ( !hlib ) exit( EXIT_FAILURE );
myfunction = GetProcAddress( hlib, "MyFunctinInMyLib" );
if ( !myfunction ) exit( EXIT_FAILURE );
double x = myfunction( "Hail Eris! All Hail Discordia!" );
}
Now YOU and YOU only are responsible for having the right type of function pointer (type checking... what''s that?) and ensuring the existence of the name (beware of C++ name mangling, extern "C" is your friend)
Thanks for the help :-)
You said, check the manpages, does anybody have an aproach for Linux too?
Thanks for sharing the knowledge I apreciate it :-)
Kindest regards,
Barry
You said, check the manpages, does anybody have an aproach for Linux too?
Thanks for sharing the knowledge I apreciate it :-)
Kindest regards,
Barry
Sure *creaking sound* as I open my notebook of things coders-should-be-required-to-know.
Everything you want to know is in "Program Library Howto" by David A. Wheeler at http://www.linuxdoc.org/HOWTO/Program-Library-HOWTO/
Everything you want to know is in "Program Library Howto" by David A. Wheeler at http://www.linuxdoc.org/HOWTO/Program-Library-HOWTO/
Thanks!
You have been of great help to me
And yes, these are things programers should know, but not always have to deal with.
This is my first time dealing with it
Thanks for all your help, I apreciate it
Kindest regards,
Barry
You have been of great help to me
And yes, these are things programers should know, but not always have to deal with.
This is my first time dealing with it
Thanks for all your help, I apreciate it
Kindest regards,
Barry
That''s why I put them in a notebook (a binder actually...) when I encountered them : ANSI codes, scancodes, the man page for make, ld, the standard filesystem, gcc-specific C extensions ...
If there is a single advice I can give you (from programmer to programmer) is to keep one too.
If there is a single advice I can give you (from programmer to programmer) is to keep one too.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement