www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/14/06:49:30

From: Lennart Steinke <steinke AT zeus DOT adv-boeblingen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Dynamic loading of functions
Date: Fri, 14 Feb 1997 09:51:08 -0600
Organization: InterNetNews at News.BelWue.DE (Stuttgart, Germany)
Lines: 60
Message-ID: <Pine.VUL.3.91.970214094133.8488I-100000@zeus.adv-boeblingen.de>
NNTP-Posting-Host: zeus.adv-boeblingen.de
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hi!

I'm currently playing with the idea of loading function dynamically
(for plug ins). It's working, but I've some questions:

1) Do I have to lock the memory of the funtion after loading or
   not?
2) Are there already libs for djgpp which allow dynamic binding?

Here's the simple and short demo source:

====Start
#include <stdio.h>
#include <stdlib.h>

static void __begin_func(void){};

int f1(int a)
{
    printf("\nTest %i\n",a);
    return a*2;
}

static void __end_func(void){};

int (*f2)(int a);

void main(void)
{
    void *p_func;
    FILE *f;
    long int size;
    int a=2;

    p_func= &__begin_func+ sizeof(int);
    size = (long int) (&__end_func- &__begin_func);

    f=fopen("test.fnc","wb");
    fwrite(p_func, size, 1, f);
    fclose(f);

    f=fopen("test.fnc","rb");
    (void *)f2 = malloc(size);
    fread(f2,size,1, f);
    fclose(f);

    a=f2(a);
    printf("%i\n",a);

}

====End


Lenny
-- 
Lennart Steinke (steinke AT adv-boeblingen DOT de)
http://mars.adv-boeblingen.de/~steinke


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019