Shared libraries/DLLs
Bjoern
nanali at nospam-wanadoo.fr
Fri Feb 1 00:27:11 PST 2008
Unknown W. Brackets schrieb:
> 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]
Hm,
1-
__declspec(dllimport) int __stdcall foo();
becomes :
export extern(Windows) int foo();
so, I would give extern(Windows) instead of extern(C) a try to avoid
name mangeling problems.
2-
Just guessing :
You have module library "DLLMain()" and loader "main()" try to place
main() in a seperate file.
3-
I can imagine that using callbacks is an interesting option to establish
plugings, like :
alias extern(Windows) void function(char* token, size_t siz = 0, bool
error =false) DisplayCallBack;
export extern(Windows)
void ExecuteProcess(DisplayCallBack cb, char* _dir, char* _command,
char* _args)
{
//do someting and call CallBack function
}
HTH Bjoern
More information about the Digitalmars-d-learn
mailing list