freelibraryandexitthreadu.pas

来自「Delphi Win32核心API参考光盘源码 本书包含了常用的Windows」· PAS 代码 · 共 72 行

PAS
72
字号
unit FreeLibraryAndExitThreadU;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  {the thread function prototype}
  function ThreadFunc(Info: Pointer): Longint; stdcall;

var
  Form1: TForm1;
  hMod: THandle;            // holds the DLL module handle

  {the prototype for the imported DLL function}
  MyFunction: function(DllHandle: THandle; ExampleName,
                       Comments: ShortString): Boolean;

implementation

{$R *.DFM}

function ThreadFunc(Info: Pointer): Longint; stdcall;
begin
  {retrieve the address of the desired function}
  @MyFunction := GetProcAddress(hMod, 'ShowAboutBox' );

  {if an address to the desired function was retrieved...}
  if (@MyFunction<>nil) then
    {display the about box from the DLL}
    MyFunction(hMod, 'FreeLibraryAndExitThread Example',
             'This example demonstrates how to free a dynamic link library '+
             'from within a thread via the FreeLibraryAndExitThread function.');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  TheThread: DWORD;         // holds the thread identifier
  ThreadHandle: THandle;    // holds a handle to the thread
  ExitCode: cardinal;       // holds the DLL exit code
begin
  {explicitly load the DLL}
  hMod := LoadLibrary('EXAMPLE.DLL');

  {create a thread that uses the function inside the DLL}
  ThreadHandle := CreateThread(nil, 0, @ThreadFunc, nil, 0, TheThread);
  if ThreadHandle=0 then
    ShowMessage('Thread not started');

  {wait until the thread has finished execution}
  WaitForSingleObject(ThreadHandle, INFINITE);

  {retrieve the exit code of the thread (returned from the DLL)}
  GetExitCodeThread(ThreadHandle, ExitCode);

  {display the exit code}
  ShowMessage(IntToStr(ExitCode));
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?