0

I have a dll with exporting function

extern "C" __declspec(dllexport) IDriver * __stdcall GetDriver() 

There is a programm which is written on Delphi. It can't see the function GetDriver(). It's double awful because I can not get and modify sources of this program.

What may be the reason of successful loading my dll (according log file) and failed call exporting function? Thank you.

Window 7 x64, Visual Studio 2010, C++ project for x86 target

Myosotis
  • 288
  • 4
  • 15

1 Answers1

4

The most likely explanation is that the function will have been exported with a decorated name. I'd expect it to have been exported with the name GetDriver@0. So you could import it like this:

function GetDriver: IDriver; stdcall; external 'DllName.dll' name 'GetDriver@0';

Use a tool like Dependency Walker to check the exact name used to export the function.

If you cannot modify the Delphi code, then you'll need to make your C++ DLL match. Do that by using a .def file which allows you control over the exported name.


The other problem you will face is that Delphi's ABI for return values differs from that used by most other tools on the Windows platform. Specifically a return value is semantically a var parameter. On the other hand, your C++ compiler will regard the return value as an out parameter. My question on Delphi WideString return values covers exactly this issue.

Because of this, I'd expect the function declaration above to lead to access violations. Instead you should declare the return value to be a Pointer and cast it to an interface reference in your Delphi code. You'll need to double check and make sure that the reference counting is handled appropriately.

Again, if you cannot modify the Delphi code, you need to make the C++ code match. A Delphi interface return value is implemented as an additional var parameter following the other parameters. So, to make your C++ function match, declare it like this:

void __stdcall GetDriver(IDriver* &retVal);
Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490