htod problems

Gilles G. schaouette at free.fr
Thu Oct 4 04:50:08 PDT 2007


working with DLLs using D is simple:
I will make a simple example for this function:
BOOL WINAPI cdtInit (int *width, int *height)

if you want to use that function in a D program, you have to declare its prototype as extern(Windows) (because it use the WINAPI). For example:
extern(Windows) alias int function(int*,int*) CDTINIT;
(note: the BOOL type was replaced by int because I guess this BOOL type is not equivalent to D's bool)

Then, in the main programm you must load the DLL before using it:
import std.c.windows.windows;
/* some more imports and external defines */
int main()
{
        HMODULE h;     // the DLL handle
        h = LoadLibraryA("StepArray.dll");
        /*  do something */
        FreeLibrary(h);
        return 0;
}

Now, in order to access the function, you have to know how the names exported by the DLL are "mangled" by the compiler. A simple way to know this is to use the program "Dependency walker" that you will find easily on the internet. for example, the mangled name for cdtInit could be: _cdtInit at 8 or just cdtInit.
Once you know the name for the function you can get an instance of the function by using:
auto cdtInit = cast(CDTINIT) GetProcAddress(h,"_cdtInit at 8");

And you can use cdtInit just as a function, for example:
int width, height;
auto returnCode = cdtInit(&width,&height);

Good luck!

--
Gilles

Scott Wrote:

> I'm currently trying to access some functions in the cards.dll library from a D library.  I'm an ok programmer but not an expert by any means.  
> 
> A explanation of the C interfaces are at this URL.  
> http://www.catch22.net/tuts/cardtut.asp  
> 
> Essentially they provide a cards.h example and I tried to run htod on that file.  But I was getting a few errors due to some fancy function pointer.  
> 
> I also have used implib to convert cards.dll to cards.lib.  Essentially how can I properly make the function calls from a D file?  
> 
> #ifndef _CARDS_INCLUDED
> #define _CARDS_INCLUDED
> 
> typedef BOOL (WINAPI *pfcdtInit)(int *, int *);
> typedef BOOL (WINAPI *pfcdtDraw)(HDC, int x, int y, int card, int type, DWORD color);
> typedef BOOL (WINAPI *pfcdtDrawEx)(HDC, int x, int y, int dx, int dy, int card, int type, DWORD color);
> typedef BOOL (WINAPI *pfcdtAnimate)(HDC hdc, int cardback, int x, int y, int frame);
> typedef void (WINAPI *pfcdtTerm) (void);
> 
> #define ecbCROSSHATCH  53
> #define ecbWEAVE1      54
> #define ecbWEAVE2      55
> #define ecbROBOT       56
> #define ecbFLOWERS     57
> #define ecbVINE1       58
> #define ecbVINE2       59
> #define ecbFISH1       60
> #define ecbFISH2       61
> #define ecbSHELLS      62
> #define ecbCASTLE      63
> #define ecbISLAND      64
> #define ecbCARDHAND    65
> #define ecbUNUSED      66
> #define ecbTHE_X       67
> #define ecbTHE_O       68
> 
> #define ecsCLUBS    0
> #define ecsDIAMONDS 1
> #define ecsHEARTS   2
> #define ecsSPADES   3
> 
> #define ectFACES		0
> #define ectBACKS		1
> #define ectINVERTED		2
> 
> #define MAKE_CARD_VALUE(face, suit) (face + suit*4)
> 
#endif


More information about the Digitalmars-d-learn mailing list