Thread overview
Windows DLLs with D
Sep 14, 2013
Buk
Sep 17, 2013
Buk
Sep 17, 2013
Dicebot
Sep 17, 2013
Rainer Schuetze
September 14, 2013
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.
September 17, 2013
On Saturday, 14 September 2013 at 16:46:06 UTC, 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.

Thanks for the help guys.
September 17, 2013
On Tuesday, 17 September 2013 at 16:05:13 UTC, Buk wrote:
> Thanks for the help guys.

Sorry for the silence. It has just happened that your question touches relatively under-explored area of the language and thus finding someone with actual experience on topic is hard. I don't even know if we have any official DLL support on Windows (other than workarounds presented on page you have linked), to be honest.

Hope this topic will attract some Windows users eventually :)
September 17, 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.