⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 freelibraryandexitthreadu.pas

📁 DelphiWin32核心API参考光盘内容.是学习书籍中的源码,便于学习.
💻 PAS
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -