   Function pointers and structs
  This is so much easier in asm, but I need to use C/C++ 'cause it's a medium sized project - any hints please ladies and gentlemen?The problem :I want to store a function pointer in a struct and then (obviously) call that function at a later time. struct example{   int  n;   int  (*f)();};s = new example;hModule = LoadLibrary ("myDLL.dll");s->f = GetProcAddress (hModule, "exportedFunction");s->n = s->f();using code something similar to the above causes the compiler to barf:quote:1) invalid conversion from `int (*)()' to `int (*)()'That just makes no sense...Any help much appreciated edit:  stupid err fixes ('.' to '->', and 'f()' to 's->f()' )[edited by - xf00f on February 13, 2004 2:15:33 PM]
  I've never used that crazy syntax before =-) Try s.f() or something, because as you have it right now, 'f' is a local function variable or a global variable.... but since it's not declared in the function or globally (it's a member of 'example') the compiler can't find it.Dunno about the first error though. I haven't dealt much with DLL's before.EDIT: Yeah. You got it.   [edited by - Tac-Tics on February 13, 2004 2:20:27 PM];  s = new example;you didnt tell the compiler what return type ''s'' hasexample s = new example;please excuse me if im wrong im still learning ;quote:Original post by mullink  s = new example;you didnt tell the compiler what return type ''s'' hasSince that isn''t his error, you can infer that s was declared earlier. ;  As to the actual problem, assuming the signature on the function pointer matches the function exported by the DLL, consider operator precedence. -> and () have the same precedence, so you might need to do:s->n = (s->f)();  I''m assuming the compiler error occurs on the last line. ;  Try making the function pointer its own type (typedef it).typedef (void* MYFUNCTYPE)(void);struct sExample {  MYFUNCTYPE f;};sExample t* = new sExample;HINSTANCE hDLL = LoadLibrary("mydll.dll");sExample->f = (MYFUNCTYPE)GetProcAddress(hDLL, "MyFunc");sExample->f();   Something along those lines.   [edited by - porthios on February 13, 2004 2:32:57 PM];  Here is a complete example: #include <stdio.h>typedef int (*FUNC0)();typedef int (*FUNC1)(int);typedef int (*FUNC2)(int,int);struct example{    intn;    FUNC0   f0;    FUNC1   f1;    FUNC2   f2;};int test0(){    return 0;}int test1( int i ){    return i;}int test2( int i, int j ){    return i+j;}int main( int argc, char *argv[] ){    example e;    e.f0 = test0;    e.f1 = test1;    e.f2 = test2;    printf( "test0()    = %d\n", e.f0() );    printf( "test1(1)   = %d\n", e.f1(1) );    printf( "test2(1,1) = %d\n", e.f2(1,1) );    return 0;} [edited by - Ranok on February 13, 2004 2:39:18 PM] [edited by - Ranok on February 13, 2004 2:39:35 PM];  Thank you very much ppl - seem to have solved the problem with your help (not actually tried it in the application yet, but the test code compiles, which can''t be a bad thing  )Never knew about the precedence issue, so thanks for the heads up, and a version of the typing did the trick -int (*f)() is now...int (__stdcall *f)()If anyone can explain why that works I''d be most grateful - always nice to learn why and not think "it just works ok!  Leave it at that" ;  My guess is that when the functions were exported into the DLL, they used the __stdcall convention, so they would also be required for the function pointers. ;  I didn''t even think about calling conventions not matching. There are a number of calling conventions available to C: __cdecl, __stdcall, etc. There are some that may be platform-dependent, like __fastcall. Then there''s __thiscall, which is specific to C++ (it indicates that the first parameter of the low-level code is the object on which the method is being called, ie, obj.method(p0, p1) gets translated into method(obj, p0, p1)).The function exported by __dllspec (Windows-specific), unless specified, may be __cdecl or __stdcall. I''d recommend specifying on both ends for robustness. 