Shared libraries/DLLs
Unknown W. Brackets
unknown at simplemachines.org
Fri Feb 1 19:50:57 PST 2008
Okay.
Example "plugin" example.d:
---
module example;
extern (C)
export int foo();
version (Windows)
import utils.windows;
extern (C)
export int example()
{
return 42 + foo();
}
---
Compiled with:
$(DMD) $(DFLAGS_DEBUG) -op -of$(OUTPUT) $(LIBS) $(wildcard utils/*.d)
example.d example.def
Or:
$(GDMD) $(DFLAGS_DEBUG) -op -of$(OUTPUT) $(LIBS) $(wildcard utils/*.d)
example.d -fPIC -q,-rdynamic,-shared
Plugin linker definition file:
---
LIBRARY example.dll
DESCRIPTION 'Example'
EXETYPE NT
SUBSYSTEM WINDOWS 4.0
CODE PRELOAD SHARED DISCARDABLE
DATA PRELOAD SINGLE
EXPORTS
example
---
Example host program for Windows, load_test.d:
---
module load_test;
import std.c.windows.windows;
import std.string, std.stdio;
extern (C)
alias int function() example_f;
int main()
{
HMODULE m = cast(HMODULE) LoadLibraryA(toStringz("example.dll"));
example_f f = cast(example_f) GetProcAddress(m, toStringz("example"));
if (f)
writefln("%d", f());
FreeLibrary(m);
return 0;
}
extern (C)
export int foo()
{
return 43;
}
---
Quick DllMain file from samples, utils/windows.d:
---
module utils.windows;
version (Windows):
import std.c.windows.windows;
extern (C)
{
void gc_init();
void gc_term();
void _minit();
void _moduleCtor();
void _moduleUnitTests();
HINSTANCE g_hInst;
}
extern (Windows)
BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved)
{
switch (ulReason)
{
case DLL_PROCESS_ATTACH:
gc_init(); // initialize GC
_minit(); // initialize module list
_moduleCtor(); // run module constructors
_moduleUnitTests(); // run module unit tests
break;
case DLL_PROCESS_DETACH:
gc_term(); // shut down GC
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
default:
// Multiple threads not supported yet
return false;
}
g_hInst = hInstance;
return true;
}
---
I don't have the Linux loader in front of me but it's almost exactly the
same and works just fine (LoadLibraryA => dlopen, GetProcAddress =>
dlsym, FreeLibrary => dlclose, with some dlerror logic in there.) But
as stated, it appears that there's really no "reverse linking" of DLLs.
-[Unknown]
Jarrett Billingsley wrote:
> "Unknown W. Brackets" <unknown at simplemachines.org> wrote in message
> news:fnvd2q$229m$1 at digitalmars.com...
>> Well, the def being for the library, not for the program loading the
>> library. Otherwise I don't get exports named properly, etc.
>
> In that case, I'd be more interested to see how you're compiling all this
> code. The code itself looks very simplistic, and I don't see any apparent
> failure points.
>
>
More information about the Digitalmars-d-learn
mailing list