【发布时间】:2011-02-06 15:59:21
【问题描述】:
编辑:愚蠢的问题,已经解决。 Form1 是 nil,因为我没有为它分配新的 TForm1,我忘记了 Delphi 不会像 C++ 那样为你这样做。
我有一个 Delphi DLL,我想将它用于我的 C++ 程序的 GUI,所以对于初学者,我创建了一个表单,并有一个函数可以显示导出的表单,以便 C++ 可以调用它。但是,程序在调用该函数时会崩溃。这是我的代码。 (我使用的是 Delphi 2010)
delphi部分:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Tabs, ComCtrls;
type
TForm1 = class(TForm)
TabControl1: TTabControl;
TabSet1: TTabSet;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function ShowForm(i: Integer) : Integer; export; cdecl;
exports
ShowForm name 'ShowForm';
implementation
{$R *.dfm}
function ShowForm(i: Integer) : Integer; export; cdecl;
begin
Form1.Show();
Result := 3; // random value, doesn't mean anything
end;
end.
这里是 C++ 代码:
HMODULE h = LoadLibrary("delphidll.dll");
if (!h) {
printf("Failed LoadLibrary (GetLastError: %i)\n", GetLastError());
return 0;
}
FARPROC p = GetProcAddress(h, "ShowForm");
if (p)
printf("Found it @ %p\n", p);
else
printf("Didn't find it\n");
((int(__cdecl *)(int))p)(34);
system("PAUSE");
return 0;
程序打印“找到它@”然后崩溃。如果我在 Delphi DLL 中注释掉 Form1.Show(),它不会崩溃,并且函数返回 3(由 printf 测试)。我错过了一些初始化还是什么?谢谢。
【问题讨论】:
标签: c++ delphi forms dll crash