Shared libraries/DLLs

Tomasz Polachowski sprytnyserek at fotka.pl
Sun Feb 3 06:06:22 PST 2008


Unknown W. Brackets Wrote:

> Consider the following simple "library":
> 
> ---
> module library;
> 
> import loader;
> 
> // (the DllMain stuff from D's doc pages.)
> 
> extern (C)
> export int foo()
> {
> 	return 42 * loader.bar();
> }
> ---
> 
> And also consider this "application":
> 
> ---
> module loader;
> 
> import std.c.windows.windows;
> import std.string, std.stdio;
> 
> extern (C)
> alias int function() example_f;
> 
> void main()
> {
> 	HMODULE m = cast(HMODULE) LoadLibraryA(toStringz("library.dll"));
> 
> 	example_f f = cast(example_f) GetProcAddress(m, toStringz("foo"));
> 
(...)

Why did you insert 'extern (C)' in 'library' and 'loader' when you'd like to call D code from D module?

getProcAddress takes a decorated name only as a 2nd argument (as you can see in 'Win32 DLLs in D'). Just compile 'library' (or a file containing WinMain that imports it) with -L/map switch to receive a complete symbol list in one *.map file. All the imports are under the "Address   Export   Alias' section. I've been using an 'Alias' name passed into getProcAddress. [Btw for 'int library.foo()', decorated name alias is _D7library3fooFZi'] Decorated name contains condensed information about module and function names, return type and types of the function args.


More information about the Digitalmars-d-learn mailing list