Shared libraries/DLLs

Unknown W. Brackets unknown at simplemachines.org
Thu Jan 31 20:38:45 PST 2008


This might be a silly or simple question.  I haven't really used shared 
libraries very much, but have need to now.

On Linux with gdc, everything (seems to be) fine.  I can make everything 
work there fine.  My problem is with Windows and dmd.

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"));

	if (f)
		writefln("%d", f());

	FreeLibrary(m);
}

extern (C)
export int bar()
{
	return 2;
}
---

This appears to work on Linux.  I expected it might work (or there might 
be some way to coax it to work) on Windows.  It seems logical, even not 
having used the LoadLibrary/etc. stuff before.  The library needs to 
access the caller's functions.

It does appear I can make it at least *compile* (but not run) by adding 
an IMPORTS directive to the .def file of the library, but this clearly 
is intended for static linking.  I'm wanting to load the DLL as a 
plugin, dynamically.  Its name and location might change.

Am I making some hopefully obvious and stupid mistake?  Please tell me I 
don't actually have to send pointers to all the api functions to the DLL 
when calling it.

Thanks,
-[Unknown]


More information about the Digitalmars-d-learn mailing list