WinAPI: GetCurrentThread、GetCurrentThreadId、Get...

  1. {返回当前线程的虚拟句柄}  
  2. GetCurrentThread: THandle;  
  3.   
  4. {返回当前线程 ID}  
  5. GetCurrentThreadId: DWORD;  
  6.   
  7. {返回当前进程的虚拟句柄}  
  8. GetCurrentProcess: THandle;  
  9.   
  10. {返回当前进程 ID}  
  11. GetCurrentProcessId: DWORD;  
  12. <hr>  

提示:
ID 是系统唯一的标识.
所谓虚拟句柄, 就是该句柄只在调用进程的进程中有效, 也不能被继承;
如果用于其他进程需要用 DuplicateHandle 复制句柄;
GetCurrentProcess 返回的虚拟句柄可以通过 OpenProcess 创建一个真实的句柄.

举例:

[Delphi]  view plain copy
  1. unit Unit1;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  7.   Dialogs, StdCtrls, Grids;  
  8.   
  9. type  
  10.   TForm1 = class(TForm)  
  11.     StringGrid1: TStringGrid;  
  12.     procedure FormCreate(Sender: TObject);  
  13.   end;  
  14.   
  15. var  
  16.   Form1: TForm1;  
  17.   
  18. implementation  
  19.   
  20. {$R *.dfm}  
  21.   
  22. procedure TForm1.FormCreate(Sender: TObject);  
  23. begin  
  24.   StringGrid1.Cells[0,0] := '当前线程虚拟句柄: ';  
  25.   StringGrid1.Cells[0,1] := '当前线程 ID: ';  
  26.   StringGrid1.Cells[0,2] := '当前进程虚拟句柄: ';  
  27.   StringGrid1.Cells[0,3] := '当前进程 ID: ';  
  28.   
  29.   StringGrid1.Cells[1,0] := IntToStr(GetCurrentThread);  
  30.   StringGrid1.Cells[1,1] := IntToStr(GetCurrentThreadID);  
  31.   StringGrid1.Cells[1,2] := IntToStr(GetCurrentProcess);  
  32.   StringGrid1.Cells[1,3] := IntToStr(GetCurrentProcessId);  
  33.   
  34.   {下面是显示格式的调整}  
  35.   StringGrid1.Align := alClient;  
  36.   StringGrid1.FixedRows := 0;  
  37.   StringGrid1.RowCount := 4;  
  38.   StringGrid1.ColCount := 2;  
  39.   StringGrid1.ColWidths[0] := StringGrid1.Canvas.TextWidth(StringGrid1.Cells[0,0]) + 4;  
  40.   StringGrid1.ColWidths[1] := StringGrid1.Canvas.TextWidth(StringGrid1.Cells[0,1]) + 4;  
  41. end;  
  42.   
  43. end.  

你可能感兴趣的:(WinAPI: GetCurrentThread、GetCurrentThreadId、Get...)