频道分类

delphi中复制线程

作者:admin 来源: 日期:2020/3/2 20:15:57 人气: 标签:

 
 DuplicateHandle函数可以用来复制线程等。

     在一个进程中,这个函数可根据位于不同进程内的现有句柄创建一个新句柄。可以从这两个进程中发出对这个函数的调用。进程必须提供PROCESS_DUP_HANDLE访问权限,否则函数执行不能成功

 
  句柄可以重复的对象包括控制台、文件(包括通信设备)、文件映射、事件、可等待计时器、互斥体、管道、进程、注册表项、信号机以及线程

var
  Form1: TForm1;
  ThreadHandle:THandle;
  TargetHandle:THandle;

implementation

{$R *.dfm}

function ThreadFunc(Info:Pointer):Integer;
var
  ICounter:Integer;
  FormDC:HDC;
begin
  FormDC :=GetDC(Form1.Handle);
  for ICounter :=1 to 10000 do
    TextOut(FormDC,10,50,PChar(IntToStr(ICounter)),Length(IntToStr(ICounter)));
  SuspendThread(TargetHandle);

  for ICounter :=1 to 10000 do
    TextOut(FormDC,110,50,PChar(IntToStr(ICounter)),Length(IntToStr(ICounter)));
  ReleaseDC(Form1.Handle,FormDC);
  ExitThread(5);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Duplicated:BOOL;
  currentProcess:THandle;
  CurrentThread:THandle;
  ThreadID:Dword;
begin
  ThreadHandle :=CreateThread(nil,0,@ThreadFunc,nil,0,ThreadID);
  currentProcess :=GetCurrentProcess;
  CurrentThread :=GetCurrentThread;
  Duplicated :=DuplicateHandle(currentProcess,ThreadHandle,currentProcess,@TargetHandle,0,False,DUPLICATE_SAME_ACCESS);
  if not (Duplicated) then
    ShowMessage('Duplicated is error!');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ResumeThread(TargetHandle);
end;

end.

https://www.cnblogs.com/djcsch2001/archive/2012/03/09/2388292.html