Delphi中一些DLL的運用。

來源:互聯網
上載者:User
 臨時轉載一些別人的代碼: 代碼

dll 調用部分:

{****************************************************************}
{ }
{ Project: DllDebug                                  }
{ Copyright(c) 2003, 2005                 }
{ Unit for UCommonUnit                          }
{ Create  : 2003-01-05 by 林紅衛             }
{ Modify  : 2003-01-16 by 林紅衛             }
{                                                                }
{****************************************************************}

unit UCommonUnit;

interface

uses
  Windows,
  SysUtils,
  Forms;

type
  TRunDLL = procedure(DLLName, FormName, FormCaption: PChar;
    aApp: TApplication; Scr: TScreen) stdcall;

procedure RunDLLForm(DLLName, FormName, FormCaption: string;
  aApp: TApplication; Scr: TScreen) stdcall;

implementation

procedure RunDLLForm(DLLName, FormName, FormCaption: string;
  aApp: TApplication; Scr: TScreen) stdcall;
var
  RunDLL: TRunDLL;
  GetDllHWND: HWND;
begin
  GetDllHWND := LoadLibrary(PChar(DllName));
  try
    if GetDllHWND < 32 then
    begin
      MessageBox(0, '沒有找到附帶DLL檔案,請確認程式是否完整!',
        '載入DLL失敗', MB_OK);
      Exit;
    end;

    @RunDLL := GetProcAddress(GetDllHWND, 'RunDLL');
    if @RunDLL <> nil then
    try
      RunDLL(PChar(UpperCase(Trim(DLLName))), PChar(UpperCase(Trim(FormName))),
        PChar(FormCaption), aApp,Scr);
    except
      raise Exception.Create('T' + FormName + '不存在!');
    end;
  finally
    FreeLibrary(GetDllHWND);
  end;
end;

end.

dll :

{****************************************************************}
{ }
{ Project: UDllTest                      }
{ Copyright(c) 2003, 2005                 }
{ Unit for UDllTest                             }
{ Create  : 2003-01-05 by 林紅衛             }
{ Modify  : 2003-01-16 by 林紅衛             }
{                                                                }
{****************************************************************}

library UDllTest;

uses
  SysUtils,
  Forms,
  Messages,
  Variants,
  Windows,
  Classes,
  UFrmTestForm1 in 'UFrmTestForm1.pas' {Form1},
  UFrmTestForm2 in 'UFrmTestForm2.pas' {Form2};

var
  DLLApp: TApplication;
  DLLScreen: TScreen;

procedure RunDLL(DLLName, FormName, FormCaption: PChar;
  aApp: TApplication; Scr: TScreen) stdcall;
var
  TheClass: TPersistentClass;
  aForm: TForm;
begin
  Application := aApp;
  Screen := Scr;
  RegisterClasses([TForm1, TForm2]);

  TheClass := GetClass('T' + FormName);
  if (TheClass = nil) then
    GetLastError;

  if TheClass.InheritsFrom(TForm)
    and (TheClass <> TForm) then
  begin
    aForm := TForm(TheClass.Create).Create(nil);
    aForm.Caption := FormCaption;
    try
      aForm.ShowModal;
    finally
      FreeAndNil(aForm);
    end;
  end;
end;

procedure DLLUnloadProc(dwReason: DWORD);
begin
  if dwReason = DLL_PROCESS_DETACH then
  begin
    Application := DLLApp; //恢複
    Screen := DLLScreen;
  end;
end;

exports
  RunDLL;

begin
  DLLApp := Application; //儲存 DLL 中初始的 Application 對象
  DLLScreen := Screen;
  DLLProc := @DLLUnloadProc; //保證 DLL 卸載時恢複原來的 Application
  DLLUnloadProc(DLL_PROCESS_DETACH);
end.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.