Question

I have a D DLL that is being loaded by a C++ program that I have no control over. The program LoadLibrarys my DLL and uses GetProcAddress to find a function named "extension_load" that takes one argument (a pointer). In my D DLL I have:

extern (C) int extension_load(void* ptr) {
    return 0;
}

And this name needs to be exported as extension_load but it is being exported as extension_load@4, so GetProcAddress cannot find it. How do I make it plain extension_load without the name mangling?

Was it helpful?

Solution

You'll need to provide the linker with a .def file that renames the export. Docs are here, you need EXPORTS.

OTHER TIPS

I got it working with some help from Hans Passant's link. Here is my .def file for anyone who will need it in the future (probably myself too):

EXETYPE NT

EXPORTS
    extension_load
    DllMain

The .def file I have is named dll.def. I have the function written as:

extern (C++) int extension_load(void* ptr) {

and the IDE I use is D-IDE, so to give the linker the def file, go to Project > Properties > Build Options and type

nameofdef.def

in the Extra Linking arguments text box. This assumes that the nameofdef.def file exists in your main project directory for D-IDE to find.

There is really no need for a def file. Just prepend your functions with export, e.g.:

    export extern (C) int extension_load(void* ptr) {
    return 0;
}

And compile via: dmd -ofmydll.dll mydll.d. Of course you'll need to define DllMain() as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top