无法理解,我是搞不懂了。关于dll调用的问题
我写了一个dll,如下:
library DllDemo;
uses
ShareMem,
SysUtils,
Forms,
WIndows,
Classes,
calc in 'calc.pas',
Unitform1 in 'Unitform1.pas' {Form2};
var
DllApplication: TApplication;
{$R *.res}
procedure DLLUnloadProc(Reason: Integer); register;
begin
if Reason = DLL_PROCESS_DETACH then Application := DllApplication;
end;
procedure CreateChild(AHandle: THandle); stdcall;
begin
Application.Handle:= AHandle;
try
Form2:= TForm2.Create(Application);
Form2.ShowModal;
except
Form2.Free;
end;
end;
exports CreateChild;
begin
DllApplication := Application;
DLLProc := @DLLUnloadProc;
end.
调用方法如下:
procedure TForm1.Button1Click(Sender: TObject);
var
HInst:THandle;
MyFunc:TAdd;
begin
HInst:=LoadLibrary(DllName); //调用DLL
if HInst>0 then //如果成功
try
@MyFunc:=GetProcAddress(HInst,PChar('CreateChild')); //获得函数地址
if @MyFunc<>nil then
begin
MyFunc(Application.Handle);
end
else
messagebox(0,'Funtion Add not found','Warning',MB_OK);
finally
FreeLibrary(HInst);
end
else
messagebox(0,'DllName not found','Warning',MB_OK);
end;
我就搞不懂啊,一调用就出现内存地址错误。我看好多地方的例子就这样子写的啊!为什么会这样?