Creating dynamic function?


Just say I want to make a scripting language that support functions,
and I want to implements WinAPI callback from user defined function in my script...

What should I do then?
Any suggestion?

Thanks in advance.
Have a look at LUA. This should already support what you are trying to do.
Was your answer here not good enough for you?
http://www.cplusplus.com/forum/general/2209/

If you need help understanding, or think you would like to go in a different direction, say so. Please don't start new threads asking the same question.
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int add(int a, int b)
{
   //printf("Something wrong??");
   return a + b;
}

void helper()
{
}

int main()
{
   int pSource = (int)add;

   int size = (int)helper - pSource;

   char *byteCode = (char*)malloc(size);

   int (*newAdd1)(int a, int b);
   int (*newAdd2)(int a, int b);

   memcpy(byteCode, (void*)pSource, size);

   newAdd1 = (int(*)(int a, int b))byteCode;
   newAdd2 = (int(*)(int a, int b))byteCode;

   printf("pSource      : %d\n", pSource);
   printf("size         : %d\n", size);

   printf("\nadd(original): %d\n", add(1, 2));
   printf("newAdd1      : %d\n", newAdd1(3, 4));
   printf("newAdd2      : %d\n", newAdd2(5, 6));

   free(byteCode);
}


I found this trick somewhere in www.planet-source-code.com.
It works great at first, but try to uncomment the "printf" function inside "add".

I tried to debug this file, but unfortunately I know nothing about assembly :(

Is there anyway to solve this problem?
Or
Is there another way to create dynamic function on the fly?

I think getting non-static method pointer will solve this...
But it seems impossible.

Btw, thanks in advance for your answers.
Topic archived. No new replies allowed.