如何通过程序动态加载.DLL文件,如何卸载.DLL,卸载后有可以随时加载

解决方案 »

  1.   

    静态加载:Function 函数名(参数):返回值cdecl;external '路径+动态库名';
    动态加载:需要三个函数loadlibrary、Getprocess、Freelibrary即可.
      

  2.   

    delphi建立dll
    cb调用dll,type __fastcall type...cb建立dll
    delphi调用dll,function...: type; cdecl;谁知道vc开发的dll,调用时用什么修饰?
      

  3.   

    采用动态调用DLL时,可以用全局方式,先将其放在申明UNIT中,在程序创建调用初始化,
    结束时释放。
    //DLL申明UNIT dllclear.pas
    unit dllclear;interfaceuses
      Windows, SysUtils, hydeclear, libcs;type
      Thyclient = Record
        libload:Boolean;
        connected:Boolean;
        hyclient:THandle;
        apSendstrToHost:TFarproc;
        apConnectToHost:TFarproc;
        apDisConnectToHost:TFarproc;
        apNetWatchOption:TFarproc;
    end;type
      TNetWatchOption = function ():boolean;
      TSendstrToHost = function (sendstr:string):boolean;
      TConnectToHost = function ():boolean;
      TDisConnectToHost = function ():boolean;
    Function HyClientInit:boolean;
    Function HyClientDestroy:boolean;var
      hyclient:Thyclient;implementationFunction HyClientInit:boolean;
    begin
      try
        hyclient.hyclient:=Loadlibrary('hyclient.dll');
        hyclient.apNetWatchOption:=GetprocAddress(hyclient.hyclient,'NetWatchOption');
        hyclient.apSendstrToHost:=GetprocAddress(hyclient.hyclient,'SendstrToHost');
        hyclient.apConnectToHost:=GetprocAddress(hyclient.hyclient,'ConnectToHost');
        hyclient.apDisConnectToHost:=GetprocAddress(hyclient.hyclient,'DisConnectToHost');
        if hyclient.hyclient>0 then
          begin
            hyclient.libload:=True;
            Result:=True;
          end
        else
          begin
            hyclient.libload:=False;
            hyclient.connected:=False;
            Result:=False;
          end;
      except
        Result:=False;
      end;
    end;Function HyClientDestroy:boolean;
    begin
      try
        FreeLibrary(hyclient.hyclient);
        hyclient.libload:=False;
        hyclient.connected:=False;
        Result:=True;
      except
        Result:=False;
      end;
    end;end.主程序调用
    procedure Form.OnCreate(Sender:TObject);
    begin
       if fileexists('hyclient.dll') then
        begin
           if not HyclientInit then
              ShowError('网络监测库初始化失败!');
        end;
     end;procedure Form.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      if hyclient.libload then
        hyclientdestroy;
     end; 
      

  4.   

    procedure TForm1.Button1Click(Sender: TObject); type TIntFunc=function(i:integer):integer;stdcall; var Th:Thandle; Tf:TIntFunc; Tp:TFarProc; begin Th:=LoadLibrary(’Cpp.dll’); {装载DLL} if Th>0 then try Tp:=GetProcAddress(Th,PChar(’TestC’)); if Tp<>nil then begin Tf:=TIntFunc(Tp); Edit1.Text:=IntToStr(Tf(1)); {调用TestC函数} end else ShowMessage(’TestC函数没有找到’); finally FreeLibrary(Th); {释放DLL} end else ShowMessage(’Cpp.dll没有找到’); end;