www.digitalmars.com         C & C++   DMDScript  

D - function pointers and calling conventions

reply "Pavel Minayev" <evilone omen.ru> writes:
It seems that D function pointers always have the D calling
convention. Try:

    import c.stdio;

    extern(Windows) void (*func)(int, int, int);

    extern(Windows) void foo(int a, int b, int c)
    {
     printf("%d %d %d\n", a, b, c);
    }

    int main()
    {
     func = &foo;
     (*func)(1, 2, 3);
     return 0;
    }

Pointers to stdcall functions are needed, however, for LoadLibrary/
GetProcAddress way to use DLLs. They're also used in TinyPTC.

BTW it took me about 5 hours (!) to figure this out. I just couldn't
get why the same code works in C but fails in D. Stack dumps,
printfs all around the code... till I finally disassembled both OBJs
and looked at the code there =)
Feb 06 2002
parent "Walter" <walter digitalmars.com> writes:
You're right. I'll have that fixed in the next update. -Walter

"Pavel Minayev" <evilone omen.ru> wrote in message
news:a3r52s$29gs$1 digitaldaemon.com...
 It seems that D function pointers always have the D calling
 convention. Try:

     import c.stdio;

     extern(Windows) void (*func)(int, int, int);

     extern(Windows) void foo(int a, int b, int c)
     {
      printf("%d %d %d\n", a, b, c);
     }

     int main()
     {
      func = &foo;
      (*func)(1, 2, 3);
      return 0;
     }

 Pointers to stdcall functions are needed, however, for LoadLibrary/
 GetProcAddress way to use DLLs. They're also used in TinyPTC.

 BTW it took me about 5 hours (!) to figure this out. I just couldn't
 get why the same code works in C but fails in D. Stack dumps,
 printfs all around the code... till I finally disassembled both OBJs
 and looked at the code there =)
Feb 06 2002