Windows DLLs with D

Rainer Schuetze r.sagitario at gmx.de
Tue Sep 17 11:31:38 PDT 2013



On 14.09.2013 18:46, Buk wrote:
> Hi all,
>
> I've read http://dlang.org/dll.html, and frankly there seems to be a lot
> of boilerplate & rote process to build a DLL.
>
> I realise that many of the do nothing functions /can/ be used to do a
> lot more; and these may be required for some purposes. But, for a simple
> DLL of functions, that can be built in C as simply as:
>
> C:\test\demo>type mydll.c
> int __declspec(dllexport) add( int a, int b ) {
>      return a + b;
> }
>
> C:\test\demo>cl /MT /LD mydll.c
> mydll.c
> /out:mydll.dll
> /dll
> /implib:mydll.lib
> mydll.obj
>     Creating library mydll.lib and object mydll.exp
>
> C:\test\demo>dumpbin /exports mydll.dll
> Dump of file mydll.dll
> File Type: DLL
>    Section contains the following exports for mydll.dll
>             1 ordinal base
>             1 number of functions
>             1 number of names
>
>      ordinal hint RVA      name
>
>            1    0 00001000 add
>
> Is there any similar mechanism for these 'simple' cases for D?
>
> If not, shouldn't it be possible to create an ?interface? file that
> takes care of the boilerplate? (If so, does anyone have one they can
> share?)
>
> Thanks, Buk.

Actually the code snippet for DllMain on that page is the needed 
boilerplate code if you don't need any tweaking of the defaults. If you 
add code in mydll.d like:

export extern(C) add(int a, int b) {
	return a + b;
}

and build this with: dmd -ofmydll.dll -d mydll.d dllmain.d

you get a dll with the export:

 >dumpbin /exports mydll.dll
Dump of file mydll.dll

File Type: DLL

   Section contains the following exports for mydll.dll

     00000000 characteristics
            0 time date stamp Thu Jan 01 01:00:00 1970
         0.00 version
            1 ordinal base
            1 number of functions
            1 number of names

     ordinal hint RVA      name

           1    0 00003010 _add

[The -d in the command line has nothing to do with building DLLs, but is 
needed because the DllMain snippet causes a deprecation message.]

If you are looking for something as described in the "D code calling D 
code in DLLs" section, I'd have to issue a warning: this does not really 
work sensibly yet, but it is being worked on.


More information about the Digitalmars-d-learn mailing list