在 C++ 中连接和使用 Delphi DLL建设者

发布于 2024-12-11 19:50:43 字数 1420 浏览 0 评论 0原文

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

I wanted to ask for some help.
I know, that there are a lot of places, where i can get this information. But, anyway, I have a problem connecting a Delphi DLL to my C++ Builder project.

For example, my Delphi DLL looks like:

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.

And this is how i connect a DLL and a function inside:

//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);

I have no mistake messages from compiler, I only have my Message Box saying, that dll is not connected. I have put my dll in project folder, in Debug folder. but there is just no connection.

Please, I am asking you to help me. What is my mistake?

EDIT: i've posted C++ code with mistakes, so here is the right one (this is for people, that have similar problems):

//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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

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

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

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

  1. If the DLL is in the same directory as the executable it will be found.
  2. The name exported by the Delphi DLL is HW rather than _HW.
  3. The calling conventions likely do not match. It's register in Delphi and cdecl in C++ I suspect. Note that I'm not 100% sure C++ Builder defaults to cdecl here, you can check.

The more serious problem is that you simply cannot pass a TForm across a DLL boundary like this. When you call a method on the object in your DLL you are calling code in the DLL rather than code in the host exe. But it's the code in the exe that you need to be called since that's the code that belongs with the object.

You need to switch to runtime packages or interfaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文