Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Creating a DLL? Using SharedLib?

Moderators: larsivi kris

Posted: 05/28/09 14:37:18

I have the following three files:

default.d --------------------------------------------- module Default;

import tango.io.Stdout; import tango.core.runtime; import tango.sys.win32.Types;

HINSTANCE g_hInst;

extern (Windows) BOOL DllMain?(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved) {

switch (ulReason) {

case DLL_PROCESS_ATTACH:

Runtime.initialize(); break;

case DLL_PROCESS_DETACH:

Runtime.terminate(); break;

case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH:

// Multiple threads not supported yet return false;

} g_hInst=hInstance; return true;

}

export void SG_init() {

Stdout.format("Hello World!\n");

}


default.def ------------------------------------------- LIBRARY "default.dll" EXETYPE NT SUBSYSTEM WINDOWS CODE SHARED EXECUTE DATA WRITE


test.d ------------------------------------------------ import tango.sys.SharedLib?; import tango.io.Stdout;

void function() do_stuff;

int main() {

if (auto lib = SharedLib?.load("default.dll")) {

Stdout.formatln("Library successfully loaded");

void* ptr = lib.getSymbol("SG_init"); if (ptr) {

Stdout.formatln("Symbol SG_init found. Address = 0x{:x}", ptr);

do_stuff = cast(void function())ptr; do_stuff();

} else {

Stdout.formatln("Symbol SG_init not found");

}

lib.unload();

} else {

Stdout.formatln("Could not load the library");

}

return 0;

}


I'm building my DLL like this:

dmd -ofdefault.dll default.d default.def

That gives this error:

OPTLINK (R) for Win32 Release 8.00.1 Copyright (C) Digital Mars 1989-2004 All rights reserved. OPTLINK : Warning 183: Extension not .RES : tango-user-dmd.lib OPTLINK : Warning 183: Extension not .RES : tangobos.lib default.obj(default)

Error 42: Symbol Undefined _D5tango2io5Print12T5PrintTaZ5Print6formatMFAaYC5t

ango2io5Print12T5PrintTaZ5Print default.obj(default)

Error 42: Symbol Undefined _D5tango2io6Stdout6StdoutC5tango2io5Print12T5Print

TaZ5Print T:\tango\bin\..\lib\tango-user-dmd.lib

Error 30: Unexpected End of File

--- errorlevel 3

It seems to be having a problem linking to Tango. I can build exe's just fine, so I'm not sure what about DLLs is different.

But so once I get my DLL working, should I be able to build test.d like this:

dmd test.d

Just that? No further def files or anything?

Author Message

Posted: 05/28/09 16:33:45

I'm not of much help for your specific issue since I'm not a windows user - but did you look at the DLL tutorial? (note that D and DLL is somewhat of a bad match from the onset, so some issues you'll have to expect in any case - DDL may be a better alternative)

Posted: 05/29/09 06:23:08

Ahah, a tutorial! *doh*

However, copying and pasting the tutorial's sc.ini, .d and .def files and building them, I'm still getting an error on build:

OPTLINK (R) for Win32 Release 8.00.1 Copyright (C) Digital Mars 1989-2004 All rights reserved. default.obj(default)

Error 42: Symbol Undefined _rt_init

default.obj(default)

Error 42: Symbol Undefined _rt_term

default.obj(default)

Error 42: Symbol Undefined _D5tango2io5Print12T5PrintTaZ5Print8formatlnMFAaYC

5tango2io5Print12T5PrintTaZ5Print default.obj(default)

Error 42: Symbol Undefined _D5tango2io6Stdout6StdoutC5tango2io5Print12T5Print

TaZ5Print --- errorlevel 4

Posted: 05/31/09 19:03:06

Any chance that someone can try compiling the tutorial and letting me know if it works for you?

Posted: 06/15/09 04:01:08

Sage Rat wrote:

Any chance that someone can try compiling the tutorial and letting me know if it works for you?

Someone has figured out in D NG that you'd better NEVER DO THAT.Create DLL in windows using D is a nightmare.Try DDL