当前位置 : 主页 > 编程语言 > delphi >

Delphi CreateThread的线程传多个参数处理方法

来源:互联网 收集:自由互联 发布时间:2021-06-23
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type MYPARA = record title: pchar; str: pchar; end ; PMYPARA = ^MYPARA; type TForm1 = class (TForm) btn1: TButton; Memo
unit Unit1;

interface

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

type
  MYPARA = record
    title: pchar;
    str: pchar;
  end;

  PMYPARA = ^MYPARA;

type
  TForm1 = class(TForm)
    btn1: TButton;
    Memo1: TMemo;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
    FCount: Integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;


implementation

{$R *.dfm}

function ThreadProc(Para: PMYPARA): DWORD; stdcall;
var
  h: hmodule;
  MyMessagebox: function(hWnd: hWnd; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall;
begin
  result := 0;
  h := LoadLibrary(user32.dll);
  if h > 0 then
  begin
    Para^.title := 测试Thread;
    @MyMessagebox := GetProcAddress(h, MessageBoxA);
    if @MyMessagebox <> nil then
      MyMessagebox(0, Para^.str, Para^.title, 0);
    freeLibrary(h);
  end;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  s: string;
  P: PMYPARA;
  ThreadHandle: THandle;
  TheThread: DWORD;
begin
  GetMem(P, sizeof(P)); //分配内存
  ThreadHandle := 0;
  try
    P.title := 测试;    //填充
    P.str := 线程MessageBoxA;
    ThreadHandle := createthread(nil, 0, @ThreadProc, P, 0, TheThread);
  finally
    if ThreadHandle <> 0 then
      closehandle(ThreadHandle);
    if P <> nil then
      FreeMem(P);
  end;
end;

end.
View Code

网友评论