为什么非要传递参数到DLL中?
在我的方法中,根本就不要把Database做为参数传递嘛,
在主EXE中已经定义了Database,DLL可以象使用别名
一样使用这个Database.
只需要在DLL中使用TDatabase的名字。
下面是一个使用Interbase的例子,我想Oracle也应该可以的,
该程序在Delphi 4及Interbase下完全通过调试。
Intebase数据库: c:/temp.gdb
Table: AAA
Project1.Dpr
------------------------
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Form1.DFM
-----------
object Form1: TForm1
Left = 186
Top = 107
Width = 435
Height = 300
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object DBGrid1: TDBGrid
Left = 88
Top = 72
Width = 273
Height = 169
DataSource = DataSource1
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
end
object Button1: TButton
Left = 200
Top = 16
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object Database1: TDatabase
Connected = True
DatabaseName = 'TestDB'
DriverName = 'INTRBASE'
LoginPrompt = False
Params.Strings = (
'SERVER NAME=c:/temp.gdb'
'USER NAME=sysdba'
'OPEN MODE=READ/WRITE'
'SCHEMA CACHE SIZE=8'
'LANGDRIVER='
'SQLQRYMODE='
'SQLPASSTHRU MODE=SHARED AUTOCOMMIT'
'SCHEMA CACHE TIME=-1'
'MAX ROWS=-1'
'BATCH COUNT=200'
'ENABLE SCHEMA CACHE=FALSE'
'SCHEMA CACHE DIR='
'ENABLE BCD=FALSE'
'BLOBS TO CACHE=64'
'BLOB SIZE=32'
'PASSWORD=masterkey')
SessionName = 'Default'
Left = 104
Top = 24
end
object Table1: TTable
Active = True
DatabaseName = 'TestDB'
TableName = 'AAA'
Left = 24
Top = 72
end
object DataSource1: TDataSource
DataSet = Table1
Left = 56
Top = 72
end
end
Unit1.Pas
------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, DBGrids, Db, DBTables;
type
TForm1 = class(TForm)
Database1: TDatabase;
Table1: TTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure Test; external 'project2';
procedure TForm1.Button1Click(Sender: TObject);
begin
Test;
end;
end.
Project2.DPR
--------------
library Project2;
uses
SysUtils,
Classes,
Unit1DLL in 'Unit1DLL.pas' {Form1};
procedure Test;
begin
form1 := tform1.create(nil);
form1.show;
end;
exports Test;
begin
end.
Unit1DLL.DFM
-------------
object Form1: TForm1
Left = 186
Top = 103
Width = 417
Height = 221
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object DBGrid1: TDBGrid
Left = 48
Top = 32
Width = 320
Height = 120
DataSource = DataSource1
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
end
object Button1: TButton
Left = 256
Top = 8
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object Table1: TTable
DatabaseName = 'TestDB'
TableName = 'AAA'
Left = 144
Top = 16
end
object DataSource1: TDataSource
DataSet = Table1
Left = 96
Top = 16
end
end
Unit1DLL.PAS
-----------
unit Unit1DLL;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DBTables, Grids, DBGrids;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Table1: TTable;
DataSource1: TDataSource;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
table1.open;
end;
end.