library Native;uses
SysUtils,
Classes,
M0001 in 'M0001.pas' {FrmNative};Exports NativeShow;
{$R *.res}
beginend.unit m0001;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, StdCtrls, DB, ADODB;type
TFrmNative = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
Label1: TLabel;
Label2: TLabel;
butsearchcode: TButton;
butsearchdesc: TButton;
EdSearchDesc: TEdit;
EditSearchCode: TEdit;
GroupBox3: TGroupBox;
DBGrid1: TDBGrid;
GroupBox4: TGroupBox;
butAddnew: TButton;
butEdit: TButton;
butDelete: TButton;
butRefresh: TButton;
butBrowse: TButton;
butQuit: TButton;
GroupBox5: TGroupBox;
Label3: TLabel;
Label4: TLabel;
EdEditCode: TEdit;
EdEditDesc: TEdit;
butUpdate: TButton;
butCancel: TButton;
ADOQuery1: TADOQuery;
DataSource1: TDataSource; private public
EditFlag:Boolean;
end;var
FrmNative: TFrmNative;Function NativeShow(DataConnect:String):WordBool;Export;
implementation{$R *.dfm}Function NativeShow(DataConnect:String):WordBool;
Begin
Result:=False;
ADOQuery1.ConnectionString:=DataConnect;
//DataConnect 相当于'Provider=SQLOLEDB.1;Password=test;Persist Security Info=True;User ID=test;Initial Catalog=test;Data Source=test'//我想通过调用函数传递数据库连接字符,但是编译时出错,不知如何解决
IF Application.FindComponent('FrmNative')=nil THEN
begin
FrmNative:=TFrmNative.Create(Application);
FrmNative.Show;
end
else
FrmNative.Show;end;end.
SysUtils,
Classes,
M0001 in 'M0001.pas' {FrmNative};Exports NativeShow;
{$R *.res}
beginend.unit m0001;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, StdCtrls, DB, ADODB;type
TFrmNative = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
Label1: TLabel;
Label2: TLabel;
butsearchcode: TButton;
butsearchdesc: TButton;
EdSearchDesc: TEdit;
EditSearchCode: TEdit;
GroupBox3: TGroupBox;
DBGrid1: TDBGrid;
GroupBox4: TGroupBox;
butAddnew: TButton;
butEdit: TButton;
butDelete: TButton;
butRefresh: TButton;
butBrowse: TButton;
butQuit: TButton;
GroupBox5: TGroupBox;
Label3: TLabel;
Label4: TLabel;
EdEditCode: TEdit;
EdEditDesc: TEdit;
butUpdate: TButton;
butCancel: TButton;
ADOQuery1: TADOQuery;
DataSource1: TDataSource; private public
EditFlag:Boolean;
end;var
FrmNative: TFrmNative;Function NativeShow(DataConnect:String):WordBool;Export;
implementation{$R *.dfm}Function NativeShow(DataConnect:String):WordBool;
Begin
Result:=False;
ADOQuery1.ConnectionString:=DataConnect;
//DataConnect 相当于'Provider=SQLOLEDB.1;Password=test;Persist Security Info=True;User ID=test;Initial Catalog=test;Data Source=test'//我想通过调用函数传递数据库连接字符,但是编译时出错,不知如何解决
IF Application.FindComponent('FrmNative')=nil THEN
begin
FrmNative:=TFrmNative.Create(Application);
FrmNative.Show;
end
else
FrmNative.Show;end;end.
你最好参考一下《Delphi 5开发人员指南》,了解一下Delphi中使用动态连接库的技巧和限制。在动态连接库中调用窗体,与在应用程序中调用窗体是不一样的!!!至少动态连接库中没有Application对象!!!
错误: function NativeShow(DataConnect:String):WordBool; Export;
正确: function NativeShow(DataConnect :PChar) :boolean; stdcall; export;
^^^^^ ^^^^^^^^^^^^^^^^^^
string 改 PChar 有必要, 因为这样才不要 borlndmm.dll
wordbool 没必要, boolean 返回是 integer, 就是非 0 就真
stdcall 重要!!! 不是 stdcall 的被认为是 __fastcall, 这个不能作 dll 导出, 问题是它也能正确执行, 但退出时才报错
function NativeShow(AppHandle :THandle; DataConnect :PChar) :boolean; stdcall; export;改实现
function NativeShow(AppHandle :THandle; DataConnect :PChar) :boolean; stdcall;
// 调用者要 Application.Handle 送到 AppHandle
begin
Application.Handle := AppHandle;
result := false;
// ADOQuery1.ConnectionString := DataConnect; 这是什么我始终不明白
if not Assigned(FrmNative) then // 利用那个全局变量
FrmNative := TFrmNative.Create(Application);
FrmNative.Show; // 没机会释放, 记得改 FrmNative的 OnClose 中加一句 Action := caFree;
end;
谢谢指点!但是我真正想知道的是如何通过function NativeShow(AppHandle :THandle; DataConnect :PChar) :boolean; 函数将DATACONNECT变量值传递到其它控件上.
if not Assigned(FrmNative) then
begin
FrmNative := TFrmNative.Create(Application);
FrmNative.XXXConnection.ConnectionString := DataConnect;
^^^^^^^^^, 建立好 Form 后, 里面变量谁你用(ps :[)
end