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

时间:2019-10-02
本文章向大家介绍Delphi CreateThread的线程传多个参数处理方法,主要包括Delphi CreateThread的线程传多个参数处理方法使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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

原文地址:https://www.cnblogs.com/studycode/p/11617629.html