MainFrm.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:2k
源码类别:

Delphi/CppBuilder

开发平台:

Delphi

  1. unit MainFrm;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, StdCtrls;
  6. type
  7.   //定义DLL中引出的过程类型
  8.   TShowDLLForm = procedure(aHandle: THandle); stdcall;
  9.   EDLLError = class(Exception);
  10.   TFrmMain = class(TForm)
  11.     btnShowModal: TButton;
  12.     btnShow: TButton;
  13.     procedure btnShowModalClick(Sender: TObject);
  14.     procedure FormCreate(Sender: TObject);
  15.     procedure btnShowClick(Sender: TObject);
  16.     procedure FormDestroy(Sender: TObject);
  17.   private
  18.   //指向加载后DLL句柄
  19.     DLLHandle: THandle;
  20.     { Private declarations }
  21.   public
  22.     { Public declarations }
  23.   end;
  24. var
  25.   FrmMain: TFrmMain;
  26. implementation
  27. {$R *.dfm}
  28. //窗体创建时,加载DLL
  29. procedure TFrmMain.FormCreate(Sender: TObject);
  30. begin
  31.   DLLHandle := LoadLibrary('..5-1DLLShowForm.dll');
  32.   {如果DLLHandle为0,代表加载DLL失败}
  33.   if DLLHandle = 0 then
  34.     raise EDLLError.Create('不能加载DLLShowForm.dll');
  35. end;
  36. //窗体释放时,卸载DLL
  37. procedure TFrmMain.FormDestroy(Sender: TObject);
  38. begin
  39.   FreeLibrary(DLLHandle);
  40. end;
  41. //模式显示窗口
  42. procedure TFrmMain.btnShowModalClick(Sender: TObject);
  43. var
  44.   ShowDLLModalForm: TShowDLLForm;
  45. begin
  46.   {链接DLL中的输出函数,以被调用}
  47.   if (@ShowDLLModalForm = nil) then
  48.     RaiseLastWin32Error;
  49.   ShowDLLModalForm(Application.Handle);
  50. end;
  51. //非模式显示窗口
  52. procedure TFrmMain.btnShowClick(Sender: TObject);
  53. var
  54.   ShowDLLForm: TShowDLLForm;
  55. begin
  56.   @ShowDLLForm := GetProcAddress(DLLHandle, 'ShowDLLForm');
  57.   if (@ShowDLLForm = nil) then
  58.     RaiseLastWin32Error;
  59.   ShowDLLForm(Application.Handle);
  60. end;
  61. end.