Import C++ DLL

Don Clugston dac at nospam.com.au
Sat May 19 11:55:39 PDT 2007


Frits van Bommel wrote:
> CrowBar wrote:
>> Please bear with me, I'm very new to C/C++ & D.
>>
>> I want to Import a function from a DLL made with VC++ express edition 
>> into
>> D.
>>
> [snip]
>>
>> The program compiles but does not execute, it returns "Error Access
>> Violation", (it should display the number 42).
>>
>>
> [snip]
>>     fnMyDLL = cast(fnMyDLL_fp) GetProcAddress(h, "fnMyDLL");
>>
>>     printf("value=%i\n", fnMyDLL());
> [snip]
> 
> GetProcAddress is probably returning null, you forgot to check for that. 
> It seems to think the function doesn't exist. The most likely cause is 
> name mangling.
> 
> Try adding an underscore before the first character in the function 
> name. (IIRC that's the mangling for C functions on Windows)
> 
> If that didn't work, make sure the function is either compiled as C code 
> or marked 'extern "C"' (directly or in an 'extern "C" {}' block) if it's 
> compiled as C++ code.

It's incredibly painful to get VC++ to produce extern(C) DLLs. It 
insists on doing something like:
_fnMyDLL at 0
essentially ignoring the "extern(C)" declaration. All the __declspec 
garbage doesn't do much, either. I keep reverting to .DEF files.

However, it should be quite simple to make a Microsoft name mangler as 
as a compile-time function. Hmmm... would make a nice little project.
--
You can also use the coffimplib tool (from the DM ftp site) to convert 
the MS lib to DMD format, and link it statically. Probably easier.


More information about the Digitalmars-d-learn mailing list