DELPHI中使用DLL的方法以及INSTALLSHIELD中使用DLL方法
来源:互联网 发布:linux挂载分区命令 编辑:程序博客网 时间:2024/06/05 10:07
调用DLL的方法无论是DELPHI还是INSTALLSHIELD,使用过程皆一致的,即声明,载入,使用,释放。
现在先看DELPHI调用DLL的情况,在此之前先用DELPHI做个DLL,现以一个创建SQL DB的CreateDB.DLL为例:
**********************************************************************
library CreateDB;
{ 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,
Classes,
ADODB, DB,
Windows,
Registry;
{$R *.res}
function MakeSqlServerCnn(strAddress,strUsername,strPassword,strDatabase:string):string;
begin
Result:='Provider=SQLOLEDB.1;'+
'Password='+strPassword+
';Persist Security Info=True;'+
'User ID='+strUsername+
';Initial Catalog='+strDatabase+
';Data Source='+strAddress+';';
end;
function MakeSqlServerCnn_WN(strAddress,strDatabase,Conn_Str:String):String;
begin
if Conn_Str='1' then
Result:='Provider=SQLOLEDB.1;'+
'Integrated Security=SSPI;Persist Security Info=False;User ID=sa;'+
'Initial Catalog='+strDatabase+
';Data Source='+strAddress+
';'
else
Result :='';
end;
Procedure GetValue(var ServerName,User,Mask,DBName,WN,BackupPath :String);
var
Reg :TRegistry;
begin
Reg :=TRegistry.Create;
Try
Reg.RootKey :=HKEY_LOCAL_MACHINE;
if Reg.OpenKey('SOFTWARE/Faceiv/HY1130',False) then
begin
ServerName :=Reg.ReadString('ServerName');
User :=Reg.ReadString('User');
Mask :=Reg.ReadString('Mask');
DBName :=Reg.ReadString('DBName');
WN :=Reg.ReadString('WN');
BackupPath :=Reg.ReadString('BackupPath');
end;
Finally
Reg.CloseKey;
Reg.Free;
End;
end;
Function CreateDataBase() :Boolean;
var
Step :integer;
Conn :TADOConnection;
Connc :TADOCommand;
ServerName,User,Mask,DBName,WN,BackupPath,DBPath :String;
begin
Try
Result :=False;
GetValue(ServerName,User,Mask,DBName,WN,BackupPath);
DBName :='Master';
WN:='1';
Conn :=TADOConnection.Create(nil);
Conn.LoginPrompt :=False;
//if WN='1' then
Conn.ConnectionString :=MakeSqlServerCnn_WN(ServerName,DBName,WN);
//else
// Conn.ConnectionString :=MakeSqlServerCnn(ServerName,User,Mask,DBName);
Conn.Connected :=True;
Connc :=TADOCommand.Create(nil);
Connc.Connection :=Conn;
DBPath :=Copy(BackupPath,1,Length(BackupPath)-6)+'DB/TimeBook';
Step :=0;
Connc.CommandText :='Use Master;Create DataBase TimeBook';
Connc.Execute;
Step :=1;
Connc.CommandText :='Restore DataBase TimeBook From Disk='''+DBPath+'''';
Connc.Execute;
Result :=True;
Except
if Step=0 then
Raise Exception.Create('创建数据库失败!');
if Step=1 then
Raise Exception.Create('恢复数据库失败!');
end;
end;
Exports
CreateDataBase;
begin
end.
*************************************************************************
注:由于这个DLL是为了INSTALLSHIELD中使用的,所以有个通过注册表进行信息传递的函数。
下面开始DELPHI中调用CreateDB.Dll的方法:
------------------------------------------------------------------------
1、声明:
Const
CreateDB ='CreateDB.dll';
type
TCreateDataBase =Function:Boolean;stdcall;
-------------------------------------------------------------------------
procedure TForm2.Button1Click(Sender: TObject);
var
CreateDataBase :TCreateDataBase;
Handle :integer;
begin
Handle :=LoadLibrary(CreateDB); //2、载入
if Handle >0 then
@CreateDataBase :=GetProcAddress(Handle,'CreateDataBase');
if @CreateDataBase<>nil then
if CreateDataBase then //3、使用
ShowMessage('SUCEED');
FreeLibrary(Handle); //4、释放
end;
*************************************************************************
动态调用最为常见,故只以动态调用为例,因为未指定此DLL的路径,所以只可与调用的程序放在一个目录下。
下面是INSTALLSHIELD中的DLL调用
prototype BOOL CreateDB.CreateDataBase(); //1、声明
function CreateDB()
STRING szDll;
NUMBER nResult;
begin
szDll =TARGETDIR^"Main//CreateDB.dll";
nResult = UseDLL (szDll); //2、载入
if (nResult =0) then
//MessageBox("UseDLL successful /n/n.DLL file loaded.",INFORMATION);
else
MessageBox("UseDLL failed./n/nCouldn't load CreateDB.DLL file.",INFORMATION);
abort;
endif;
if CreateDataBase() then //3、使用
//MessageBox("创建成功",INFORMATION);
else
MessageBox("创建失败",SEVERE);
abort;
endif;
if(UnUseDLL(szDll)<0) then //4、释放
MessageBox("UnUseDLL failed./n/nCreateDB.DLL still in memory.",SEVERE);
else
//MessageBox("UnUseDLL successful./n/n.DLL file removed from memory.",INFORMATION);
endif;
end;
*************************************************************************
- DELPHI中使用DLL的方法以及INSTALLSHIELD中使用DLL方法
- delphi中dll的使用
- 浅谈在Delphi中Dll调试方法以及怎样在Dll中使用数据模块DataModule(或表单Form)
- qt中使用dll库的方法
- Delphi中动态调用DLL的方法
- Delphi中动态调用DLL的方法
- Delphi中DLL的创建和使用
- Delphi中DLL的创建和使用
- Delphi中DLL的创建和使用
- 在Delphi中使用DLL
- 如何在java中使用dll方法
- MFC中使用C#编写的Dll的方法
- DEV-CPP中生成和使用DLL的方法
- DEV-CPP中生成和使用DLL的方法
- 分析在MFC正规dll中使用资源的方法
- VS2010中使用CL快速 生成DLL的方法
- 分析在MFC正规dll中使用资源的方法
- 一般使用动态链接库DLL中类的方法
- Unix下的解压命令 tar
- CSS 9宫格边框
- 字幕换色
- 让Web项目中Velocity的#parse支持相对路径
- 用Java动态代理实现AOP
- DELPHI中使用DLL的方法以及INSTALLSHIELD中使用DLL方法
- 恭贺新春, 祝大家新年快乐, 万事如意
- AutomatedQA AQTime v5.40.399
- Immunity Debugger v1.4
- ListView 自绘模式的一种实现
- 求逆矩阵的并行算法
- shell中的条件测试
- GotW #88: A Candidate For the "Most Important const"
- 总结不同层面的内存概念