#include "stdafx.h" #include <windows.h> typedef int (*DoMath)(int, int) ; int _tmain(int argc, _TCHAR* argv[]) { HMODULE hMod = LoadLibrary ("exampleDLL.dll"); if (NULL != hMod) { DoMath mf1 = (DoMath) GetProcAddress(hMod,"DoMath"); if( mf1 != NULL ) { printf ("DoMath(8,7)==%d \n", mf1(8,7) ); } else { printf ("GetProcAddress Failed \n"); } FreeLibrary(hMod); } else { printf ("LoadLibrary failed\n"); return 1; } return 0; },c++,delphi,dll,delphi-7,C++,Delphi,Dll,Delphi 7" /> #include "stdafx.h" #include <windows.h> typedef int (*DoMath)(int, int) ; int _tmain(int argc, _TCHAR* argv[]) { HMODULE hMod = LoadLibrary ("exampleDLL.dll"); if (NULL != hMod) { DoMath mf1 = (DoMath) GetProcAddress(hMod,"DoMath"); if( mf1 != NULL ) { printf ("DoMath(8,7)==%d \n", mf1(8,7) ); } else { printf ("GetProcAddress Failed \n"); } FreeLibrary(hMod); } else { printf ("LoadLibrary failed\n"); return 1; } return 0; },c++,delphi,dll,delphi-7,C++,Delphi,Dll,Delphi 7" />

从c+调用函数+;Delphi中的动态链接库 我在VS2010中创建了一个新的C++ DLL项目,它公开了1个函数 #include "stdafx.h" #define DllImport extern "C" __declspec( dllimport ) #define DllExport extern "C" __declspec( dllexport ) DllExport int DoMath( int a, int b) { return a + b ; } 然后我用VS2010创建了一个C++应用程序来测试DLL。VS2010中的测试应用程序构建可以调用C++ DLL,并得到预期的结果。p> #include "stdafx.h" #include <windows.h> typedef int (*DoMath)(int, int) ; int _tmain(int argc, _TCHAR* argv[]) { HMODULE hMod = LoadLibrary ("exampleDLL.dll"); if (NULL != hMod) { DoMath mf1 = (DoMath) GetProcAddress(hMod,"DoMath"); if( mf1 != NULL ) { printf ("DoMath(8,7)==%d \n", mf1(8,7) ); } else { printf ("GetProcAddress Failed \n"); } FreeLibrary(hMod); } else { printf ("LoadLibrary failed\n"); return 1; } return 0; }

从c+调用函数+;Delphi中的动态链接库 我在VS2010中创建了一个新的C++ DLL项目,它公开了1个函数 #include "stdafx.h" #define DllImport extern "C" __declspec( dllimport ) #define DllExport extern "C" __declspec( dllexport ) DllExport int DoMath( int a, int b) { return a + b ; } 然后我用VS2010创建了一个C++应用程序来测试DLL。VS2010中的测试应用程序构建可以调用C++ DLL,并得到预期的结果。p> #include "stdafx.h" #include <windows.h> typedef int (*DoMath)(int, int) ; int _tmain(int argc, _TCHAR* argv[]) { HMODULE hMod = LoadLibrary ("exampleDLL.dll"); if (NULL != hMod) { DoMath mf1 = (DoMath) GetProcAddress(hMod,"DoMath"); if( mf1 != NULL ) { printf ("DoMath(8,7)==%d \n", mf1(8,7) ); } else { printf ("GetProcAddress Failed \n"); } FreeLibrary(hMod); } else { printf ("LoadLibrary failed\n"); return 1; } return 0; },c++,delphi,dll,delphi-7,C++,Delphi,Dll,Delphi 7,Delphi7项目的结果是6155731,而我预期的结果是5。我检查了结果的二进制文件,认为它可能与数据类型有关,但在我看来它是随机的。当我重新编译/重新运行应用程序时,每次都会得到相同的结果 我对德尔福了解不多,这是我第一次处理它,我发现它令人困惑 关于下一步检查什么有什么建议吗?您需要指定调用约定,在本例中为cdecl: TMyFunction = function(X, Y: Integer): Integer; cdecl; 您的代码使用默认的Delphi调用约定,即register,

Delphi7项目的结果是6155731,而我预期的结果是5。我检查了结果的二进制文件,认为它可能与数据类型有关,但在我看来它是随机的。当我重新编译/重新运行应用程序时,每次都会得到相同的结果

我对德尔福了解不多,这是我第一次处理它,我发现它令人困惑


关于下一步检查什么有什么建议吗?

您需要指定调用约定,在本例中为
cdecl

TMyFunction = function(X, Y: Integer): Integer; cdecl;
您的代码使用默认的Delphi调用约定,即
register
,并通过寄存器传递参数。
cdecl
调用约定在堆栈上传递参数,因此这种不匹配解释了两个模块之间通信失败的原因


还有一些评论:

LoadLibrary
的故障模式是返回
NULL
,即
0
。检查返回值是否为
=32

使用隐式链接导入此函数更简单。用以下简单声明替换所有
LoadLibrary
GetProcAddress
代码:

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

<>系统加载程序将在执行程序时解决这个问题,这样就不必担心链接的细节。在柏林RAD Studio上,

< P>,使用CLAN编译器作为C++部分,一个外部的“C”的CDEL函数将有一个下划线,传统UNIX“C”风格加上它的名字。上述代码在这种情况下不起作用,但使用外部声明的name属性来解决问题:

函数DoMath(X,Y:Integer):整数;cdecl;外部“exampleDLL.dll”名称“\u DoMath”

没有在其他编译器中尝试过,因此这可能是cdecl的一个普遍问题。Windows API不使用cdecl,但使用与Delphi相同的调用约定,因此,例如,DLL函数的Winapi.Windows声明没有添加下划线

如果使用GetProcAddress,则正确的调用是GetProcAddress(hDLL,“_DoMath”);否则返回nil


希望这有助于任何人努力让德尔福和C++对话。< /P>我有德尔菲RAD 10.2,并且和(A+B+C)工作得很好,在DLL中,用<代码> INTHON-DEXSPEC(DLReutix:StdCaldStDelk CalcSUM(int a,int b,int c){a+b+c;})。dyn调用是:`calcsum:=GetProcAddress(hmod,'calcsum');`而且效果也很好。但我在获取函数return的char*(或string)时遇到了很多问题。你知道为什么吗?你能给我一个建议吗?大卫·赫弗南你是个明星☼
function DoMath(X, Y: Integer): Integer; cdecl; external 'exampleDLL.dll';