DELPHI中使用DLL的方法以及INSTALLSHIELD中使用DLL方法

 调用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;

*************************************************************************

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值