狄
狄克
Unregistered / Unconfirmed
GUEST, unregistred user!
我创建一个带数据的DLL窗体,同时有DataModule窗体,窗体关闭时报非法访问内存地址<br>DLL的代码如下:<br>library datadll;<br>uses<br> SysUtils,<br> Classes,<br> Forms,<br> Ole2,<br> Unit1 in 'Unit1.pas' {DataModule1: TDataModule},<br> Unit2 in 'Unit2.pas' {Form2};<br><br>{$R *.res}<br>function showme(AHandle:THandle) : string ; stdcall ;<br>begin<br> CoInitialize(nil);<br> DataModule1 := TDataModule1.Create(Application) ;<br> Form2 := TForm2.Create(Application) ;<br><br> Form2.ShowModal ;<br><br> FreeAndNil(DataModule1);<br> FreeAndNil(Form2) ;<br> coUnInitialize;<br> Result := Form2.DBGrid1.SelectedField.AsString ;<br>end ;<br><br>exports<br> showme ;<br> <br>begin<br>end.<br><br>Form2是数据显示主窗体,有一个DBGRID用来显示DataModule中表的内容,没有任何其它代码,<br><br>DataModule的代码如下:<br>unit Unit1;<br><br>interface<br><br>uses<br> SysUtils, Classes, DB, ADODB;<br><br>type<br> TDataModule1 = class(TDataModule)<br> ADOConnection1: TADOConnection;<br> ADODataSet1: TADODataSet;<br> DataSource1: TDataSource;<br> procedure DataModuleCreate(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> DataModule1: TDataModule1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TDataModule1.DataModuleCreate(Sender: TObject);<br>begin<br> if not ADODataSet1.Active then<br> ADODataSet1.Open ;<br>end;<br><br>end.<br><br>调用该DLL的代码如下:<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> Tshowme = function (AHandle: THandle): string; StdCall;<br> EDLLLoadError = class(Exception);<br><br> TForm1 = class(TForm)<br> Edit1: TEdit;<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> LibHandle : THandle;<br> Showme: Tshowme ;<br>begin<br> LibHandle := LoadLibrary('F:/DATADLLTEST/DLL/DATADLL.DLL');<br> try<br> if LibHandle = 0 then<br> raise EDLLLoadError.Create('Unable to Load DLL');<br> @Showme := GetProcAddress(LibHandle, 'showme');<br> if not (@Showme = nil) then<br> Edit1.Text := Showme(Application.Handle)<br> else<br> RaiseLastWin32Error;<br> finally<br> FreeLibrary(LibHandle); // Unload the DLL.<br> end;<br>end;<br><br>end.<br><br>请高手指教,得到答案后立即送分<br>在线等待<br>