Calling functions from a c++ DLL in Delphi

前端 未结 2 1472
感情败类
感情败类 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:29

    You need to specify the calling convention, which in this case is cdecl:

    TMyFunction = function(X, Y: Integer): Integer; cdecl;
    

    Your code uses the default Delphi calling convention which is register and passes parameters through registers. The cdecl calling convention passes parameters on the stack and so this mis-match explains why communications between the two modules fail.


    Some more comments:

    The failure mode for LoadLibrary is to return NULL, that is 0. Check that rather than the return value being >=32.

    It's simpler to use implicit linking to import this function. Replace all the LoadLibrary and GetProcAddress code with this simple declaration:

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

    The system loader will resolve this import when your executable starts so you don't have to worry about the details of linking.

提交回复
热议问题