用delphi写了个DLL,里面有些数据库的操作,用的是ADOQuery1,
DLL的出口函数是
function ShowRegion(hnd:THandle):string;
var
Form:TForm1;
begin
CoInitialize(nil);
Application.Handle:=hnd;
Form:=TForm1.Create(Application);
try
Form.ShowModal;
Result:=Form.title;
finally
Form.Free;
end;
CoUnInitialize;
end;调用这个函数是的代码是
procedure TForm1.edRegionDblClick(Sender: TObject);
begin
var
hnd:THandle;
ShowRegion:TShowRegion;
begin
hnd:=LoadLibrary('Region.dll');
try
if hnd<>0 then
begin
@ShowRegion:=GetProcAddress(hnd,'ShowRegion');
if not (@ShowRegion=nil) then
edRegion.Text:=ShowRegion(Application.Handle)
else
MessageDlg('没有找到出口函数,请确认', mtInformation,[mbOk], 0);
end
else
MessageDlg('没有找到DLL,请确认', mtInformation,[mbOk], 0);
finally
FreeLibrary(hnd);
end;
end;为什么每次关闭程序的时候都会错误,但是调用的时候没错误
DLL的出口函数是
function ShowRegion(hnd:THandle):string;
var
Form:TForm1;
begin
CoInitialize(nil);
Application.Handle:=hnd;
Form:=TForm1.Create(Application);
try
Form.ShowModal;
Result:=Form.title;
finally
Form.Free;
end;
CoUnInitialize;
end;调用这个函数是的代码是
procedure TForm1.edRegionDblClick(Sender: TObject);
begin
var
hnd:THandle;
ShowRegion:TShowRegion;
begin
hnd:=LoadLibrary('Region.dll');
try
if hnd<>0 then
begin
@ShowRegion:=GetProcAddress(hnd,'ShowRegion');
if not (@ShowRegion=nil) then
edRegion.Text:=ShowRegion(Application.Handle)
else
MessageDlg('没有找到出口函数,请确认', mtInformation,[mbOk], 0);
end
else
MessageDlg('没有找到DLL,请确认', mtInformation,[mbOk], 0);
finally
FreeLibrary(hnd);
end;
end;为什么每次关闭程序的时候都会错误,但是调用的时候没错误
这改变了application.handle,最后还要变回去好吧
OldHnd := Application.Handle;
Application.Handle:=hnd;
try
...
finally
Application.Handle:=OldHnd;
end;
var
Form:TForm1;
=======
dll 里有 string pchar 等操作,需要 sharemem 单元在支持
var
hnd:THandle;
ShowRegion:TShowRegion;
begin
hnd:=LoadLibrary('Region.dll');
try
if hnd<>0 then
begin
@ShowRegion:=GetProcAddress(hnd,'ShowRegion');
if not (@ShowRegion=nil) then
begin
edRegion.Text:=strpas(ShowRegion(Application.Handle));
end
else
MessageDlg('没有找到出口函数,请确认', mtInformation,[mbOk], 0);
end
else
MessageDlg('没有找到DLL,请确认', mtInformation,[mbOk], 0);
finally
FreeLibrary(hnd);
end;
end;DLL的出口函数为
function ShowRegion(hnd:THandle):PChar;
var
Form:TForm1;
OldHandle:THandle;
begin
CoInitialize(nil);
OldHandle:=Application.Handle;
Application.Handle:=hnd;
Form:=TForm1.Create(Application);
try
Form.ShowModal;
Result:=PChar(Form.returnvalues);
finally
Form.Free;
CoUnInitialize;
Application.Handle:=OldHandle;
end;
end;每次调用完DLL的时候出现"指令引用的“0x00000000”该内存不能为“read”,这个是怎么回事