(exe)
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
dmdmain in '..\Common\dmdMain.PAS' {dmMain: TDataModule};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TdmMain, dmMain);
Application.Run;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ADODb, Grids, DBGrids, DB, ExtCtrls;
type THandleFormDlg = Procedure(AppHandle : THandle; AdoCon : _Connection); stdcall;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
ADOQuery1: TADOQuery;
Button1: TButton;
Panel1: TPanel;
DBGrid1: TDBGrid;
Label1: TLabel;
Label2: TLabel;
quAct: TADOQuery;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses dmdmain;
{$R *.dfm}
procedure TForm1.FormShow(Sender: TObject);
begin
dmMain.dbCon.Connected := True;
ADOQuery1.Active := True;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
dmMain.dbCon.Connected := False;
ADOQuery1.Active := False;
end;
procedure TForm1.Button1Click(Sender: TObject);
var Fn : Array[0..255] of Char;
P : THandleFormDlg;
AppLib: THandle;
FileName : ShortString;
FAdoCon : _Connection;
begin
FileName := 'Project2.dll';
StrPCopy(Fn,FileName);
If FileExists(FileName) then begin
With quAct, Sql do begin
Close; Clear;
Add('create table #test (V Varchar(10) Null)');
Add('Insert into #Test Values (''12345'')');
Add('Insert into #Test Values (''67890'')');
ExecSql;
end;
FAdoCon := dmMain.dbCon.ConnectionObject;
AppLib := LoadLibrary(Fn);
@P := GetProcAddress(AppLib,'ShowFormDlg');
P(Application.Handle, FAdoCon);
dmMain.dbCon.ConnectionObject := Nil;
FreeLibrary(AppLib); //connection lost after free library
dmMain.dbCon.ConnectionObject := FAdoCon; //not effect
If dmMain.dbCon.Connected then begin
ShowMessage('database is connected');
end else begin
ShowMessage('database is not connected after library, Why ???????');
end;
end else begin
ShowMessage('File '+FileName+' not found');
end;
end;
end.
(dll)
library Project2;
uses
SysUtils,
Classes,
ADODb,
Unit1 in 'Unit1.pas' {Form1},
dmdmain in '..\Common\dmdMain.PAS' {dmMain: TDataModule};
{$R *.res}
Procedure ShowFormDlg(AppHandle : THandle; AdoCon : _Connection); stdcall;
var Id : LongInt;
begin
Id := Show_FormDlg(AppHandle, AdoCon);
Close_FormDlg(Id);
end;
Exports ShowFormDlg;
begin
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, ExtCtrls, StdCtrls, DB, ADODB;
type
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
Button1: TButton;
Panel1: TPanel;
DBGrid1: TDBGrid;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
Function Show_FormDlg(AppHandle : THandle; AdoCon : _Connection) : Integer;
Procedure Close_FormDlg(Id : Longint);
var
Form1: TForm1;
var TmpH : THandle;
implementation
uses dmdmain;
{$R *.dfm}
Function Show_FormDlg(AppHandle : THandle; AdoCon : _Connection) : Integer;
begin
TmpH := Application.Handle;
Application.Handle := AppHandle;
Application.CreateForm(TdmMain,dmMain);
dmMain.dbCon.ConnectionObject := AdoCon;
Application.CreateForm(TForm1, Form1);
Result := LongInt(Form1);
Form1.ShowModal;
end;
Procedure Close_FormDlg(Id : Longint);
begin
dmMain.dbCon.ConnectionObject := Nil;
Application.Handle := TmpH;
TForm1(Id).Free;
//dmMain.dbCon.Connected := False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
ADOQuery1.Active := True;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ADOQuery1.Active := False;
end;
end.
(dmmain)
unit dmdmain;
interface
uses
SysUtils, Classes, DB, DBTables, ADODB, Dialogs, Controls;
type
TdmMain = class(TDataModule)
dbCon: TADOConnection;
private
{ Private declarations }
public
{ Public declarations }
end;
var
dmMain: TdmMain;
implementation
uses Math;
{$R *.dfm}
end.
dmmain.dfm
object dmMain: TdmMain
OldCreateOrder = False
Left = 330
Top = 181
Height = 176
Width = 245
object dbCon: TADOConnection
ConnectionString =
'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initi' +
'al Catalog=Northwind;Data Source=Hasan;Use Procedure for Prepare' +
'=1;Auto Translate=True;Packet Size=4096;Workstation ID=Jupiter;Use' +
' Encryption for Data=False;Tag with column collation when possib' +
'le=False'
LoginPrompt = False
Mode = cmShareDenyRead
Provider = 'SQLOLEDB.1'
Left = 16
Top = 16
end
end