VC调用Delphi DLL

别的没什么,是一定可以调用成功的。但是意外的是,ShowMessage函数在DLL里也可以轻易被调用。此外,Delphi里的var 相当于VC里的引用,需要在函数原型里正确标识,否则传递普通变量甚至常量是不行的。

VC++代码:

// callDLL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"

int main(int argc, char* argv[])
{
    printf("Hello World!\n");
    
    double a=10.4;
    HINSTANCE hDllInst = LoadLibrary("fonctionMathematique.DLL");    
    if(hDllInst)        
    {        
        typedef double (cdecl *MYFUNC)(double, double, double&); // 函数原型
        
        MYFUNC fun1 = NULL; // 函数别名        
        // fun1 = (MYFUNC)GetProcAddress(hDllInst,"_AddD"); // 函数名称
        fun1 = (MYFUNC)GetProcAddress(hDllInst,"_AddDouble"); // 在DLL中声明的函数名    
        
        if(fun1)            
        {            
            // printf("%f\n",fun1(5.3));
            printf("%f\n",fun1(1.1, 5.2, a));
            printf("%f\n",a);
        }        
        FreeLibrary(hDllInst);        
    }
    return 0;
}

Delphi代码(代码太多,只列举关键函数实现部分):

 function _AddDouble(iVarA: Double; iVarB: Double; var iResult:Double):Double; cdecl; export;
 begin
   ShowMessage(FloatToStr(iVarA));
   iResult:=iVarA+iVarB;
   result:=iResult;
 end;

 function _AddD(a: double): double; cdecl;
 begin
    result:=a+10.1;
 end;

另外,VC里可能默认使用cdecl方式。我没写传递方式就可以成功调用。

posted @ 2014-02-25 04:46  findumars  Views(2274)  Comments(0Edit  收藏  举报