gpt4 book ai didi

delphi - 在 C++ Builder 中连接和使用 Delphi DLL

转载 作者:行者123 更新时间:2023-12-03 15:55:11 26 4
gpt4 key购买 nike

我想寻求一些帮助。我知道,有很多地方可以获取这些信息。但是,无论如何,我在将 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);

最佳答案

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

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

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

关于delphi - 在 C++ Builder 中连接和使用 Delphi DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7899418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com