Plugin modules for application

Plugin modules for application

Post by Mats » Sun, 04 Aug 2002 20:58:12



Hi!

I'm writing an app using C/C++ and i would like the app to be able to load
a compiled module and execute it in runtime.

Forexample: The user running my app choose a moudule from a listbox, the
module should be loaded into memory an run it's thing and then exit back
to the main app.

The modules should also ofcourse be written in C/C++.

Is there any examples on this or pointers?

TIA

Mats

----
Remove all before 'mog' to reply by e-mail.

 
 
 

Plugin modules for application

Post by Jens.Toerr.. » Sun, 04 Aug 2002 22:05:40



> I'm writing an app using C/C++ and i would like the app to be able to load
> a compiled module and execute it in runtime.
> Forexample: The user running my app choose a moudule from a listbox, the
> module should be loaded into memory an run it's thing and then exit back
> to the main app.
> The modules should also ofcourse be written in C/C++.
> Is there any examples on this or pointers?

It's actually quite simple: make your module a shared library and load
it on demand with dlopen(3). Then get the function (or, actually a pointer
to the function) from the library you want to run using dlsym(3), call
the function and, when you're done, call dlclose(3). If there's only one
task for the library that should be executed whenever you open it, you
can even get arond the dlsym(3) stuff by having a function in the library
called _init(), which is automatically run when the library is opened.

                                        Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046

 
 
 

Plugin modules for application

Post by Chronos Tachyo » Sun, 04 Aug 2002 23:58:31


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


  [Snip]

Quote:> If there's only one task for the library that should be executed whenever
> you open it, you can even get arond the dlsym(3) stuff by having a
> function in the library called _init(), which is automatically run when
> the library is opened.

Actually, _init() and _fini() are both reserved for the C runtime to use, so
you will get a "multiple definitions of _init" error when linking unless
you forego all use of the C library.  You're better off using
"attribute((constructor))" instead, as multiple functions can be defined
with that attribute.

- --
Chronos Tachyon
http://chronos.dyndns.org/ -- WWED?
Guardian of Eristic Paraphernalia
Gatekeeper of the Region of Thud
  9:50am  up 9 days, 12:11,  1 user,  load average: 0.05, 0.11, 0.07

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9S++XPGpxVErWvSoRAtHEAKDaxpDNkGmanzsMLvXxfpJdlBcIpQCfS4R9
kuw7R8ScenfMfZhVE8xVgPo=
=J2uG
-----END PGP SIGNATURE-----

 
 
 

Plugin modules for application

Post by Jens.Toerr.. » Mon, 05 Aug 2002 00:52:22




>   [Snip]
>> If there's only one task for the library that should be executed whenever
>> you open it, you can even get arond the dlsym(3) stuff by having a
>> function in the library called _init(), which is automatically run when
>> the library is opened.
> Actually, _init() and _fini() are both reserved for the C runtime to use, so
> you will get a "multiple definitions of _init" error when linking unless
> you forego all use of the C library.  You're better off using
> "attribute((constructor))" instead, as multiple functions can be defined
> with that attribute.

Thanks for the clarification, I was just quoting from the man page and
some vague memories that I once played around with an _init() function
in a library loaded with dlopen() without getting any ill effects.

                                              Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046

 
 
 

Plugin modules for application

Post by mlw » Mon, 05 Aug 2002 05:03:18



> Hi!

> I'm writing an app using C/C++ and i would like the app to be able to load
> a compiled module and execute it in runtime.

> Forexample: The user running my app choose a moudule from a listbox, the
> module should be loaded into memory an run it's thing and then exit back
> to the main app.

> The modules should also ofcourse be written in C/C++.

> Is there any examples on this or pointers?

Here are some snippits from one of my class libraries.

<< Class info >>

#ifdef GCC
void *MPlugin::getpfn(char *name)
{
        void * pfn = dlsym(m_dll, name);
        if(!pfn)
                mferr.mfprintf("MPlugin dlsym Warning: %s\n", dlerror());
        return pfn;

Quote:}

Boolean MPlugin::openlib(char *name)
{
        m_dll = dlopen(name, RTLD_NOW);

        if(!m_dll)
        {
                char buffer[PATH_MAX];
                mferr.mfprintf("MPlugin dlopen Error: %s %s\n", name,
dlerror());
                getcwd(buffer, sizeof(buffer));
                sprintf(&buffer[strlen(buffer)], "/%s", name);

                m_dll = dlopen(buffer, RTLD_NOW);
        }

        if(!m_dll)
                mferr.mfprintf("MPlugin dlopen Error: %s %s\n", name,
dlerror());

        return m_dll ? TRUE : FALSE;

Quote:}

void MPlugin::closelib()
{
        dlclose(m_dll);
Quote:}

#endif
#ifdef WIN32
void *MPlugin::getpfn(char *name)
{
        void * pfn = GetProcAddress(m_dll, name);
        if(!pfn)
                mferr.mfprintf("MPlugin Warning: %d\n", GetLastError());
        return pfn;

Quote:}

Boolean MPlugin::openlib(char *name)
{
        m_dll = LoadLibrary(name);

        if(!m_dll)
        {
                mferr.mfprintf("MPlugin Error: %s\n", name, GetLastError());
        }
        return m_dll ? TRUE : FALSE;

Quote:}

void MPlugin::closelib()
{
        FreeLibrary(m_dll);
Quote:}

#endif

<< Header info >>

#ifdef GCC
#define PDECL   extern "C"
#endif

#ifdef WIN32
#define PDECL   extern "C" __declspec(dllexport)
#endif

#ifdef GCC
typedef void *HDLL;
#define __HDLL_DEFINED
#endif

#ifdef WIN32
typedef HMODULE HDLL;
#define __HDLL_DEFINED
#endif

<< Darwin code >>
#ifdef DARWIN

#define RTLD_NOW        0
char dyn_buf[1024];
char noerror[]="noerror";
static char * dyn_error = noerror;
void * dlopen(char *filename, int mode)
{
        NSObjectFileImage image;
        dyn_error = noerror;
        if (NSCreateObjectFileImageFromFile(filename, &image) !=
NSObjectFileImageSuccess)
        {
                dyn_error = "Could not open file";
                return NULL;
        }
        return NSLinkModule(image, filename, NSLINKMODULE_OPTION_PRIVATE);

Quote:}

void dlclose(void *handle)
{
        dyn_error = noerror;
        NSUnLinkModule(handle, FALSE);
        return;

Quote:}

void *dlsym(void *handle, char *funcname)
{
        NSSymbol symbol;
        int cb = strlen(funcname)+2;
        char *symname = (char *)ALLOCA(cb);
        sprintf(symname, "_%s", funcname);

        dyn_error = noerror;

        symbol = NSLookupSymbolInModule(handle, symname);
        if (symbol)
        {
                return (void *) NSAddressOfSymbol(symbol);
        }
        else
        {
                sprintf(dyn_buf, "Symbol [%s] not found", symname);
                dyn_error = dyn_buf;
                return (void *) NULL;
        }

Quote:}

char *dlerror(void)
{
        return dyn_error;
Quote:}

#endif
 
 
 

Plugin modules for application

Post by Eric P. McC » Mon, 05 Aug 2002 05:33:43



> I'm writing an app using C/C++ and i would like the app to be able to load
> a compiled module and execute it in runtime.

Which language are you using, C or C++?  The differences are actually
fairly important here.

The general idea is to write your module like this:

  /* module.h */
  struct module_handle {
    char *mydata1, *mydata2;
    int (*dosomething)(char *);
    int (*dosomethingelse)(char *, int);
  };

  extern "C" struct module_handle *module_init(void);

  /* mymodule.c */

  #include "module.h"

  int my_dosomething(char *arg) {
    return 1;
  }

  int my_dosomethingelse(char *arg1, int arg2) {
    return arg2;
  }

  struct module_handle *module_init(void) {
    struct module_handle *mh;

    mh = malloc(sizeof(*mh));
    mh->mydata1 = mh->mydata2 = NULL;
    mh->dosomething = my_dosomething;
    mh->dosomethingelse = my_dosomethingelse;

    return mh;
  }

Note that module.h would be a common include file, not something
specific to mymodule.c.  Then in your program (which loads the
modules), do:

  #include "module.h"

  int run_module(const char *name) {
    void *dh;
    struct module_handle *mh;
    struct module_handle *(*func)(void);

    if((dh = dlopen(name, RTLD_LAZY)) == NULL) {
      /* error */
    }

    if((func = dlsym(dh, "module_init")) == NULL) {{
      /* error */
    }

    mh = func();

    mh->dosomething("Hello, world!");
    mh->dosomethingelse("Nice to meet you, world!", 3);

    free(mh);
    dlclose(dh);

    return 0;
  }

For C++, the idea is the same, except you would return a pointer to a
regular class (the extern "C" only defines the linkage for the
function, not the type of code you can use _in_ it).

--

"Last I checked, it wasn't the power cord for the Clue Generator that
was sticking up your ass." - John Novak, rasfwrj

 
 
 

1. Using Mozilla/Netscape plugin in embedded application

I posted a question on this a couple of days ago to c.o.l.d.a but
perhaps the title wasn't catchy enough :)

I want to be able to play Flash movies fullscreen in an embedded
application. The Swift tools aren't compatible enough for me, though
they work well. The Macromedia standalone player has a serious bug -
even when run with -root, it still opens a blank window on top of the
content. I don't want the user to see anything onscreen except the
movie.

The only other option I see is to somehow use the Netscape Flash
plugin to play my content. Are there any references for using a
Netscape Linux plugin outside of Netscape? When I google on this
topic, all I find is people asking how to write plugins, and people
whining that Netscape's dev support isn't good enough for plugin
developers. :/

TIA for any suggestions.

2. Building apache 2.0.43 on DEC/Alpha OSF 4.0F

3. How to auto detect the plugin of USB device in my application.

4. telnet/sendmail problem on RH7.1, why is everybody so quiet?

5. Apache module and Netscape Plugin

6. replacing NIS

7. Cant load plugin /usr/local/lib/netscape/plugins/java_30

8. cgi script

9. modules, add-ons or plugins programming

10. Plugins/modules

11. Need some help, how to write small application to access Linux loadable module

12. Interrupt notification from kernel module to user application

13. Start application on module insert