怎样在DLL中使用数据模块(50分)

3

328xy

Unregistered / Unconfirmed
GUEST, unregistred user!
我写的DLL中一函数要调用数据库,应怎样在DLL加入数据模块,函数又该如何调用
 
Z

zw84611

Unregistered / Unconfirmed
GUEST, unregistred user!
试试动态生成。
 
3

328xy

Unregistered / Unconfirmed
GUEST, unregistred user!
如何动态生成呢
 
X

Xelloss

Unregistered / Unconfirmed
GUEST, unregistred user!
不需要阿<br>Dll的工程里面也可以创建Form的。
 

狄克

Unregistered / Unconfirmed
GUEST, unregistred user!
W

wqthai

Unregistered / Unconfirmed
GUEST, unregistred user!
你可以让EXE调用一个创建数据模块的输出函数阿,比如<br>exports<br>&nbsp; CreateDM,<br>&nbsp; FreeDM;<br>在Dll里实现这些函数就可以拉!
 
Q

quiben

Unregistered / Unconfirmed
GUEST, unregistred user!
给出以前我作过的例子,该例子是调用一个Form,该Form可以访问MySQL数据库.<br>{具体的实现单元}<br>unit DllFormUnit;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, DB, ExtCtrls, DBCtrls, Grids, ADODB, ComCtrls,<br>&nbsp; ShellCtrls, DBGrids,TConMySQLUnit;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; ADOQuery1: TADOQuery;<br>&nbsp; &nbsp; DataSource1: TDataSource;<br>&nbsp; &nbsp; DBGrid1: TDBGrid;<br>&nbsp; &nbsp; DBNavigator1: TDBNavigator;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>procedure ShowDllForm(AHandle:THandle;ACaption:String);stdcall;<br><br>implementation<br><br>{$R *.dfm}<br>procedure ShowDllForm(AHandle:THandle;ACaption:String);<br>var<br>&nbsp; DLLForm:TForm1;<br>begin<br>&nbsp; Application.Handle :=AHandle;<br>&nbsp; try<br>&nbsp; &nbsp; DLLForm:=TForm1.Create(Application);<br>&nbsp; &nbsp; if DLLForm&lt;&gt;nil then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; DLLForm.Caption:=ACaption;<br>&nbsp; &nbsp; &nbsp; DLLForm.ShowModal;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; DLLForm.Free;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; ADOQuery1.Close;<br>&nbsp; ADOQuery1.SQL.Clear;<br>&nbsp; ADOQuery1.SQL.Add('Select * from user');<br><br>&nbsp; try<br>&nbsp; &nbsp; TConMySQL.Create(ADOQuery1);<br>&nbsp; except<br>&nbsp; &nbsp; MessageBox(0,'shibai','info',MB_OK+MB_ICONINFORMATION);<br>&nbsp; end;<br><br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; StrFliter:String;<br>begin<br>&nbsp; StrFliter:=InputBox(输入查询的关键字','请输入人名','');<br>&nbsp; if StrFliter='' then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(0,'Sorry','Info',MB_OK+MB_ICONINFORMATION);<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; ADOQuery1.Close;<br>&nbsp; ADOQuery1.SQL.Clear;<br>&nbsp; ADOQuery1.SQL.Add('Select * from mytable where Name=:"StringFliter"');<br>&nbsp; ADOQuery1.Prepared;<br>&nbsp; ADOQuery1.Parameters[0].Value:=StrFliter;<br><br>&nbsp; try<br>&nbsp; &nbsp; TConMySQL.Create(ADOQuery1);<br>&nbsp; except<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; MessageBox(0,'sorry','info',MB_OK+MB_ICONINFORMATION);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; ADOQuery1.ConnectionString:='DefaultDir=;Driver={myodbc driver};DATABASE=mysql;UID=root;PWD=;SERVER=192.168.0.100';<br>&nbsp; DataSource1.DataSet:=ADOQuery1;<br>&nbsp; DBGrid1.DataSource:=DataSource1;<br>&nbsp; DBNavigator1.DataSource:=DataSource1;<br>end;<br><br>end.<br><br>{Project文件}<br>library DllFormProject;<br><br>{ Important note about DLL memory management: ShareMem must be the<br>&nbsp; first unit in your library's USES clause AND your project's (select<br>&nbsp; Project-View Source) USES clause if your DLL exports any procedures or<br>&nbsp; functions that pass strings as parameters or function results. This<br>&nbsp; applies to all strings passed to and from your DLL--even those that<br>&nbsp; are nested in records and classes. ShareMem is the interface unit to<br>&nbsp; the BORLNDMM.DLL shared memory manager, which must be deployed along<br>&nbsp; with your DLL. To avoid using BORLNDMM.DLL, pass string information<br>&nbsp; using PChar or ShortString parameters. }<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; DllFormUnit in 'DllFormUnit.pas' {TForm},<br>&nbsp; TConMySQLUnit in 'TConMySQLUnit.pas';<br><br>{$R *.res}<br><br>exports<br>&nbsp; ShowDllForm;<br><br>begin<br>end.<br><br><br>{调用单元}<br>unit DllFormUnit;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, DB, ExtCtrls, DBCtrls, Grids, ADODB, ComCtrls,<br>&nbsp; ShellCtrls, DBGrids,TConMySQLUnit;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; ADOQuery1: TADOQuery;<br>&nbsp; &nbsp; DataSource1: TDataSource;<br>&nbsp; &nbsp; DBGrid1: TDBGrid;<br>&nbsp; &nbsp; DBNavigator1: TDBNavigator;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>procedure ShowDllForm(AHandle:THandle;ACaption:String);stdcall;<br><br>implementation<br><br>{$R *.dfm}<br>procedure ShowDllForm(AHandle:THandle;ACaption:String);<br>var<br>&nbsp; DLLForm:TForm1;<br>begin<br>&nbsp; Application.Handle :=AHandle;<br>&nbsp; try<br>&nbsp; &nbsp; DLLForm:=TForm1.Create(Application);<br>&nbsp; &nbsp; if DLLForm&lt;&gt;nil then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; DLLForm.Caption:=ACaption;<br>&nbsp; &nbsp; &nbsp; DLLForm.ShowModal;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; DLLForm.Free;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; ADOQuery1.Close;<br>&nbsp; ADOQuery1.SQL.Clear;<br>&nbsp; ADOQuery1.SQL.Add('Select * from user');<br><br>&nbsp; try<br>&nbsp; &nbsp; TConMySQL.Create(ADOQuery1);<br>&nbsp; except<br>&nbsp; &nbsp; MessageBox(0,'shibai',Info'',MB_OK+MB_ICONINFORMATION);<br>&nbsp; end;<br><br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; StrFliter:String;<br>begin<br>&nbsp; StrFliter:=InputBox('请输入','人名','');<br>&nbsp; if StrFliter='' then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(0,'sorry','info',MB_OK+MB_ICONINFORMATION);<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; ADOQuery1.Close;<br>&nbsp; ADOQuery1.SQL.Clear;<br>&nbsp; ADOQuery1.SQL.Add('Select * from mytable where Name=:"StringFliter"');<br>&nbsp; ADOQuery1.Prepared;<br>&nbsp; ADOQuery1.Parameters[0].Value:=StrFliter;<br><br>&nbsp; try<br>&nbsp; &nbsp; TConMySQL.Create(ADOQuery1);<br>&nbsp; except<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; MessageBox(0,'sorry','info',MB_OK+MB_ICONINFORMATION);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; ADOQuery1.ConnectionString:='DefaultDir=;Driver={myodbc driver};DATABASE=mysql;UID=root;PWD=;SERVER=192.168.0.100';<br>&nbsp; DataSource1.DataSet:=ADOQuery1;<br>&nbsp; DBGrid1.DataSource:=DataSource1;<br>&nbsp; DBNavigator1.DataSource:=DataSource1;<br>end;<br><br>end.
 
3

328xy

Unregistered / Unconfirmed
GUEST, unregistred user!
我想用下面的dll函数返回数据库中的一个值<br>Function Test(AHandle:THandle):pchar;stdcall;<br>begin<br>&nbsp; Application.Handle:=AHandle;<br>&nbsp; DataMode.Create(Application);<br>&nbsp; DataMode.ADOQuery1.SQL.Add('select * from ItemTable');<br>&nbsp; DataMode.ADOQuery1.Open;<br>&nbsp; Result:=Pchar(DataMode.ADOQuery1.FieldByName('F2').AsString);<br>&nbsp; DataMode.Free;<br>end;<br>主程序调用:<br>Function Test(AHandle:THandle):pChar;<br>Stdcall;external 'jdcl.dll';<br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>Var S:string;<br>&nbsp; &nbsp; V:Integer;<br>begin<br>&nbsp; S:=Test(Application.Handle);<br>end;<br><br>报什么存取错误,正确的调用方法应该为什么
 
3

328xy

Unregistered / Unconfirmed
GUEST, unregistred user!
有谁知道啊
 
3

328xy

Unregistered / Unconfirmed
GUEST, unregistred user!
wqthai所说的CreateDM, &nbsp;FreeDM应该怎样写呢?<br>
 

晨晨

Unregistered / Unconfirmed
GUEST, unregistred user!
不是太明白你的问题,如果,我猜,你是想写一个DLL,并且DLL中包含TDataModule<br>那么直接可以用,你点那个New图标就可以了。<br>如果你是想使用主程序中的TDataModule,那也许你应该写个函数把TDataModule传过去。<br>猜的。。。。[:D]我半年没有写程序了
 
X

Xelloss

Unregistered / Unconfirmed
GUEST, unregistred user!
&gt;&gt;<br>&nbsp; Application.Handle:=AHandle;<br>&nbsp; DataMode.Create(Application);<br>&nbsp; DataMode.ADOQuery1.SQL.Add('select * from ItemTable');<br>&nbsp; DataMode.ADOQuery1.Open;<br>&nbsp; Result:=Pchar(DataMode.ADOQuery1.FieldByName('F2').AsString);<br>&nbsp; DataMode.Free;<br><br>你创建DataMode的方法不对吧。<br>试试DataMode := TDataModule.Create(self);
 
W

wqthai

Unregistered / Unconfirmed
GUEST, unregistred user!
哥们:我想得和328xy一样。。<br>能不能用你试试啊!
 
顶部