Calling functions from a c++ DLL in Delphi

前端 未结 2 1471
感情败类
感情败类 2021-01-02 00:32

I created a new c++ DLL project in VS2010 that exposes 1 function

#include \"stdafx.h\"    
#define DllImport   extern \"C\" __declspec( dllimport )
#define         


        
2条回答
  •  失恋的感觉
    2021-01-02 01:39

    On RAD Studio Berlin, using CLANG compiler for the C++ part, a cdecl function which is extern "C" will have its name prepended with an underscore, traditional unix "C" style. The above code doesn't work in this case, but use the name attribute of the external declaration to fix the problem:

    function DoMath(X, Y: Integer): Integer; cdecl; external 'exampleDLL.dll' name '_DoMath';

    Not tried it with other compilers, so it might be a general issue with cdecl. The Windows API does not use cdecl, but uses same calling convention as Delphi so, for example, the Winapi.Windows declarations of DLL functions do not have the underscore added.

    Same true if using GetProcAddress, the correct call is GetProcAddress(hDLL, '_DoMath'); otherwise nil is returned.

    Hope this helps anyone struggling to get Delphi talking to C++.

提交回复
热议问题