3

我想寻求帮助。我知道,有很多地方,我可以得到这些信息。但是,无论如何,我在将 Delphi DLL 连接到我的 C++ Builder 项目时遇到了问题。

例如,我的 Delphi DLL 如下所示:

library f_dll;

uses
  SysUtils,
  Classes,
  Forms,
  Windows;

procedure HW(AForm : TForm);
        begin
                MessageBox(AForm.Handle, 'DLL message', 'you made it!',MB_OK);
        end;
exports
        HW;

{$R *.res}

begin

end.

这就是我连接 DLL 和内部函数的方式:

//defining a function pointer type
typedef void (*dll_func)(TForm* AForm);

dll_func HLLWRLD = NULL;

HMODULE hDLL = LoadLibrary("f_dll.dll");
if (!hDLL) ShowMessage("Unable to load the library!");

//getting adress of the function
HLLWRD = (dll_func)GetProcAddress(hDLL, "_HW"); 

if (!pShowSum) ShowMessage("Unable to find the function");

HLLWRLD(Form1);

FreeLibrary(hDLL);

我没有来自编译器的错误消息,我只有消息框说 dll 未连接。我已将我的 dll 放在项目文件夹中的 Debug 文件夹中。但没有任何联系。

拜托,我请求你帮助我。我的错误是什么?

编辑:我发布了有错误的 C++ 代码,所以这是正确的(这是给有类似问题的人的):

//defining a function pointer type
typedef void (*dll_func)(TForm* AForm);

dll_func HLLWRLD = NULL;

HMODULE hDLL = LoadLibrary("f_dll.dll");
if (!hDLL) ShowMessage("Unable to load the library!");

//getting adress of the function
HLLWRD = (dll_func)GetProcAddress(hDLL, "HW");  //HW instead _HW

if (!HLLWRLD) ShowMessage("Unable to find the function"); //HLLWRLD instead pShowSum

HLLWRLD(Form1);

FreeLibrary(hDLL);
4

1 回答 1

3
  1. 如果 DLL 与可执行文件位于同一目录中,则会找到它。
  2. Delphi DLL 导出的名称是 HW 而不是 _HW。
  3. 调用约定可能不匹配。我怀疑它在 Delphi 中注册,在 C++ 中注册 cdecl。请注意,我不是 100% 确定 C++ Builder 在此处默认为 cdecl,您可以检查一下。

更严重的问题是您根本无法像这样通过 DLL 边界传递 TForm。当您在 DLL 中调用对象上的方法时,您调用的是 DLL 中的代码,而不是宿主 exe 中的代码。但它是您需要调用的 exe 中的代码,因为这是属于该对象的代码。

您需要切换到运行时包或接口。

于 2011-10-26T07:07:08.170 回答