Loadable modules

This sounds, to me, like something that would be extremely useful to know how to do. How would you write a program that can dynamically load different modules at runtime (probably according to a config file)?

I have thought about it; and I guess you'd need three functions to start with: one to load modules, one to start a module and one to end a module. So I came up with a header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef _MODULE_H_
#define _MODULE_H_

#include <types.h>

/* Globals */
typedef struct module {
    int(* entry_point)(void); /* Entry point: similar to C[++]'s main() function. Must return int. */
	boolean is_running; /* Should be set by module_run or module_end only. */
}   module_t;

/* Function declarations */
/* module_load: loads and connects a module to be run */
module_t* module_load(const char*);

/* module_connect: connects and initializes the entry point of a pre-loaded module */
module_t* module_connect(int(* entry_point)(void));

/* module_run: run a pre-connected and pre-loaded module */
void module_run(module_t* const);

/* module_end: kill the thread containing a running module */
void module_end(module_t* const);

#endif 

but I don't know if this is the right thing to do, or if I'm completely lost.

I'm also interested in writing plugin frameworks (it's always annoyed me that in Firefox you have to restart the whole browser when you update a plugin: surely if plugins were ran in a separate thread to the main browser (all of them in one thread, that is) you'd only have to restart that thread? Maybe every now and again you could create a new plugin thread (if there are too many).)

Oh and another reason is to make money. Later on when I'm writing usable programs, I can make the program itself free, and sell plugins.
Last edited on
What exactly is the definition of "module" you are referring to?
More specifically you'd have to use some kind of shell command in windows or linux api to execute an external program and otherwise I have absolutely no idea.
Oh, sorry. I guess I should be more specific.

What I mean by a "module" is a dynamically loadable "piece" of a program: the rest of the program doesn't depend on it; but you get extra functionality by having it. Sort of like add-ons for Firefox or kernel modules for Linux. You don't need it, but if it's there, you can make the program use it.

DLLs are out of the question; I need a cross-platform method.

What I was thinking of doing is having a dedicated file format (.mod, maybe) and placing a header at the top. Then module_load would parse the header, find the entry function and run it from there. But short of that I really don't know what to do.
DLLs are out of the question; I need a cross-platform method.
If you want native modules, that's impossible. You have to use dynamic libraries. It's perfectly possible to use DLLs in a cross-platform application, though. You just have to compile the code to different kinds of libraries depending on the target system (i.e. DLLs for Windows, shared objects for UNIX), and load them using different functions (LoadLibrary() and friends vs. dlopen() and friends).

I posted a thread a couple months ago about this very subject. I found that making the plugins fully portable by writing them in Python and embedding Python in C++ had a performance penalty of over 1000x relative to native code. That was unacceptable for my application. MAybe that won't be the case for you, and that's a possible plugin implementation.
Hmm... I think having plugins written in Python would be OK. The issue would be (for me) the larger executable size caused by embedding the Python interpreter, more than the speed impact.

Another issue would be whether to embed Python or Perl or Ruby, or write a javascript interpreter... I would say more people know Perl than Python... but Python is probably easier to learn. Edit: or PHP, or LISP? There are too many damn interpreted languages.

Maybe I could benchmark the two interpreters doing the same thing (opening 100 files or something) and see which is faster... I know both languages at a basic level; and I think I prefer Perl... but whichever is faster will be the one to choose I suppose. [ Perl seems to be faster according to the internet ]

MAybe that won't be the case for you, and that's a possible plugin implementation.

I'm just trying to learn how to actually use plugins right now. I'm not bothered about performance (for now).




I'm using this program (I don't know what it is yet) as an opportunity to tackle POSIX threads, GUI programming, socket programming, and modules and plugins at once. Ideally the program will be able to self-update (optionally, I hate programs that force updates on you). I can't see that that's too difficult though; the hardest part will be checking for a new version and downloading the files.
Last edited on
The issue would be (for me) the larger executable size caused by embedding the Python interpreter, more than the speed impact.
The interpreter is dynamically linked. At least it is for Windows.
K, thanks.
Topic archived. No new replies allowed.