Problems with OpenGL bindings and D2

Sergey Gromov snake.scaly at gmail.com
Wed Sep 17 17:20:15 PDT 2008


Bill Baxter <wbaxter at gmail.com> wrote:
> On Wed, Sep 17, 2008 at 11:37 PM, Sergey Gromov <snake.scaly at gmail.com> wrote:
> > Maybe it's the same problem I've had hit recently.
> >
> > The names in DLL exports are not mangled according to the calling
> > convention rules.  I.e. kernel32.dll exports CreateFileA although it's
> > _CreateFileA at 28 in Microsoft's kernel32.lib.  The latter is a correctly
> > mangled name, the former is merely a symbolic reference.
> >
> > There are two options for generating the correct import library, safe
> > one, and flexible one.
> >
> > The safe method is available if you have a COFF (MS-compatible) import
> > library for your DLL.  Then you use
> > http://www.digitalmars.com/ctg/coffimplib.html
> > to convert it into an OMF import library usable with DMD.  Then simply
> > use whatever export() generates the right names for your lib.
> >
> > The flexible method is to use
> > http://www.dprogramming.com/linkdef.php
> > to create an import library based upon the linker errors produced by
> > DMD.  Beware though that it's your responsibility to choose the correct
> > extern() calling convention.  DLL does not have any calling convention
> > meta-data, so linkdef will link any undefined symbols to the similarly
> > named symbols in a DLL without any compatibility guarantee.
> >
> 
>  If you have the DLL, I've always had success creating the DMD COFF
> import library for it using "implib" in the basic utils package.
> Usually you'll need to create it using the /system flag.  If that
> doesn't work try it without the /system flag.

I'll tell you what happens next.  You create an import library:

implib kernel32.lib c:\windows\system32\kernel32.dll /system

Then you declare a function:

extern (System) HANDLE CreateFileA(...);

and linker fails because it expects "_CreateFileA at 28" which is not in 
your library, there's only "_CreateFileA".
Then you change your declaration to

extern (C) HANDLE CreateFileA(...);

Everything compiles but you get your stack corrupted because CreateFileA 
is stdcall and unwinds stack, but extern(C) is cdecl so your code 
contains stack unwinding, too.  So you clean stack twice.


More information about the Digitalmars-d-learn mailing list