dlL文件为:library DBConndll;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,
Forms,
Classes,
Registry,
inifiles,
windows,
DataSet in 'pas单元\DataSet.pas' {frmDataSet};{$R *.res}
var
DLLapp: Tapplication;procedure DBSet(App: Tapplication); stdcall;
begin
DLLapp:=application;
application:= App try
frmDataSet := tfrmDataSet.Create(application);
frmDataSet.ShowModal;
finally
frmDataSet.Free;
end;
application:=DLLapp;end;exports
DBSet;
begin
end.调用方代码为:
type
TDset=procedure(App:Tapplicaton);stdcall; {放在type TForm1 = class(TForm)上面了.}procedure TForm1.Button1Click(Sender: TObject);
var
Hinst: Thandle;
Tempstr: string;
Dset:TDset;
begin Hinst := loadlibrary(Pchar('DBConndll.dll')); //Load一个Dll,按文件名找。
try
if Hinst > 0 then
begin
Dset := Getprocaddress(Hinst,Pchar('DBSet'));
Dset(applicaton);
end
else
showmessage('DBConndll.dll没找到');
finally
FreeLibrary(Hinst);
end;运行会出错.
project DBconn.exe raised exception class EOLesysError with message '尚未调用
CoInitialize.',process stopped.user step or Run to comtinue;
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,
Forms,
Classes,
Registry,
inifiles,
windows,
DataSet in 'pas单元\DataSet.pas' {frmDataSet};{$R *.res}
var
DLLapp: Tapplication;procedure DBSet(App: Tapplication); stdcall;
begin
DLLapp:=application;
application:= App try
frmDataSet := tfrmDataSet.Create(application);
frmDataSet.ShowModal;
finally
frmDataSet.Free;
end;
application:=DLLapp;end;exports
DBSet;
begin
end.调用方代码为:
type
TDset=procedure(App:Tapplicaton);stdcall; {放在type TForm1 = class(TForm)上面了.}procedure TForm1.Button1Click(Sender: TObject);
var
Hinst: Thandle;
Tempstr: string;
Dset:TDset;
begin Hinst := loadlibrary(Pchar('DBConndll.dll')); //Load一个Dll,按文件名找。
try
if Hinst > 0 then
begin
Dset := Getprocaddress(Hinst,Pchar('DBSet'));
Dset(applicaton);
end
else
showmessage('DBConndll.dll没找到');
finally
FreeLibrary(Hinst);
end;运行会出错.
project DBconn.exe raised exception class EOLesysError with message '尚未调用
CoInitialize.',process stopped.user step or Run to comtinue;
解决方案 »
- delphi与vb之间怎么进行通信? 比如把vb当中的某个变量值传到delphi里
- Delphi 中如何写类、方法和属性
- 初学者关于菜单问题
- showmessage弹出时间控制的问题
- 使用adoquery或adotable如何返回当前记录号(数据库接连sql 2000)
- Sql的updata的问题,很简单,但是我不会
- 关于a console application 中的read,各位关照一下
- 给回复是0:00整的回复人的200分,看谁走运。。。呵呵
- delphi6,StayOnTop的问题
- Win 32开发人员参考库
- 急!每打印一页就下偏2CM,打N篇就下偏N*2CM
- 以前用ADO的时候有查询字段,现在用odac的查询控件,那里可以设置查询字段啊??????请各位老大指点
procedure DBSet(); stdcall;
begin
try
frmDataSet := tfrmDataSet.Create(nil);//这里完全可以不指定属主
frmDataSet.ShowModal;
finally
frmDataSet.Free;
end;
end;
project DBconn.exe raised exception class EOLesysError with message '尚未调用
CoInitialize.',process stopped.user step or Run to comtinue;
这个是什么错误,为什么会出现这个错误?弄明白了,以后碰到这样的问题就能知道了.就不用再烦大家了.
frmDataSet主要功能是设置数据库连接 代码如下.
unit DataSet;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, DB, ADODB, Registry, inifiles;type
TfrmDataSet = class(TForm)
GroupBox1: TGroupBox;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
Edit_ServerName: TEdit;
Label1: TLabel;
Edit_LoginName: TEdit;
Edit_LoginPW: TEdit;
Label2: TLabel;
Label3: TLabel;
Edit1: TEdit;
Label4: TLabel;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
ADOConnection_Server: TADOConnection;
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure RadioButton1Click(Sender: TObject);
private
strDBName: string;
Application_name: string;
{ Private declarations }
public
//function GetDBprivde(App: TApplication): Pchar;
{ Public declarations }
end;var
frmDataSet: TfrmDataSet;implementation{$R *.dfm}procedure TfrmDataSet.BitBtn3Click(Sender: TObject);
begin
self.ADOConnection_Server.Connected:=false;
//CloseWindow(Application.Handle);
Application.Terminate;
end;procedure TfrmDataSet.BitBtn1Click(Sender: TObject);
begin
strDBName := Trim(Edit1.Text);
screen.Cursor := crHourGlass;
try
ADOConnection_Server.Connected := False;
if self.RadioButton2.Checked then
ADOConnection_Server.ConnectionString := 'Provider=SQLOLEDB.1;' +
'Persist Security Info=False;' +
'User ID=' + Trim(Edit_LoginName.Text) +
';Password=' + Trim(Edit_LoginPW.Text) +
';Initial Catalog=' + strDBName +
';Data Source=' + Trim(Edit_ServerName.Text) + ''
else
ADOConnection_Server.ConnectionString := 'Provider=SQLOLEDB.1;' +
'Integrated Security=SSPI;' +
'Persist Security Info=False;' +
'User ID=sa;' +
'Initial Catalog=' + strDBName +
';Data Source=' + Trim(Edit_ServerName.Text) + '';
ADOConnection_Server.Connected := True;
MessageBox(Handle, '数据库连接成功!', '系统提示', 0);
except
MessageBox(Handle, '数据库连接失败!', '系统提示', 0);
end;
screen.Cursor := crDefault;
end;procedure TfrmDataSet.FormShow(Sender: TObject);
var
Reg: TRegistry;
strComputerName: pChar;
lLen: DWORD;
begin with Tinifile.Create(Extractfilepath(application.exename)+'DBCon.ini') do
begin
Application_name := ReadString('Application', 'exename', '');
free;
end;
if Application_name = '' then
begin
application.MessageBox('DBCon.ini文件没找到,或配置不正确', '提示', 48);
exit;
end; Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER; //判断该注册表项是否存在
if (Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\' + Application_name, false)) = false then
if Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion', True) then
begin
//不存在 则新建注册表项
Reg.CreateKey(Application_name);
Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\' + Application_name, True);
lLen := 255;
GetMem(strComputerName, 255);
GetComputerName(strComputerName, lLen);
Reg.WriteString('ServerName', strPas(strComputerName)); //初始化 [数据服务器名] 项的值
Reg.WriteString('constype', 'userpsw'); //保存设置
Reg.WriteString('LoginName', 'sa'); //初始化 [登录用户名] 项的值
Reg.WriteString('LoginPW', ''); //初始化 [登录口令] 项的值
Reg.WriteString('DataBaseName', ''); //数据库名
FreeMem(strComputerName);
end;
//读取 ServerName 项的值 并显示
Edit_ServerName.Text := Trim(Reg.ReadString('ServerName'));
if Trim(Reg.ReadString('constype')) <> 'userpsw' then
begin
RadioButton1.Checked := true;
Edit_LoginName.Enabled := false;
Edit_LoginPW.Enabled := false;
end;
//读取 LoginName 项的值 并显示
Edit_LoginName.Text := Trim(Reg.ReadString('LoginName'));
//读取 LoginPW 项的值 并显示
Edit_LoginPW.Text := Trim(Reg.ReadString('LoginPW'));
//读取 LoginPW 项的值 并显示
Edit1.Text := Trim(Reg.ReadString('DataBaseName'));
Reg.CloseKey;
finally
Reg.Free;
end;
end;procedure TfrmDataSet.BitBtn2Click(Sender: TObject);
var
Reg: TRegistry;
SetBool: Boolean;
begin
strDBName := Trim(Edit1.Text);
try
ADOConnection_Server.Connected := False;
if self.RadioButton2.Checked then
ADOConnection_Server.ConnectionString := 'Provider=SQLOLEDB.1;' +
'Persist Security Info=False;' +
'User ID=' + Trim(Edit_LoginName.Text) +
';Password=' + Trim(Edit_LoginPW.Text) +
';Initial Catalog=' + strDBName +
';Data Source=' + Trim(Edit_ServerName.Text) + ''
else
ADOConnection_Server.ConnectionString := 'Provider=SQLOLEDB.1;' +
'Integrated Security=SSPI;' +
'Persist Security Info=False;' +
'User ID=sa;' +
'Initial Catalog=' + strDBName +
';Data Source=' + Trim(Edit_ServerName.Text) + '';
ADOConnection_Server.Connected := True;
MessageBox(Handle, '数据库连接成功!', '系统提示', 0);
SetBool := True;
except
MessageBox(Handle, '数据库连接失败!', '系统提示', 0);
SetBool := False;
end;
if not SetBool then exit;
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER; //判断该注册表项是否存在
if Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\' + Application_name, True) then
begin
Reg.WriteString('ServerName', Trim(Edit_ServerName.Text)); //保存设置
if RadioButton1.Checked then
Reg.WriteString('constype', 'SSPI') //保存设置
else
Reg.WriteString('constype', 'userpsw'); //保存设置
Reg.WriteString('LoginName', Trim(Edit_LoginName.Text));
Reg.WriteString('LoginPW', Trim(Edit_LoginPW.Text));
Reg.WriteString('DataBaseName', Trim(Edit1.Text)); //数据库名
Reg.CloseKey;
end;
finally
inherited;
end;
Reg.Free;
self.ADOConnection_Server.Connected:=false;
//MessageBox(Handle,'数据库连接成功,请重新启动主系统!','系统提示',0);
Close;
end;procedure TfrmDataSet.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
//Ctrl + A 修改数据库登录名
if (ssCtrl in Shift) and (Key = Ord('A')) then
self.Edit_LoginName.Enabled := True;
end;procedure TfrmDataSet.RadioButton1Click(Sender: TObject);
begin
Edit_LoginName.Enabled := RadioButton2.Checked;
Edit_LoginPW.Enabled := RadioButton2.Checked;
end;end.