線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2469
推到 Plurk!
推到 Facebook!

能不能获得程序中当前有哪些线程?

答題得分者是:change.jian
h@visli
資深會員


發表:103
回覆:429
積分:431
註冊:2004-02-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-02-23 17:59:34 IP:219.133.xxx.xxx 未訂閱
我想获取程序中当前所有线程的ThreadID与其ClassName,请问有什么好办法?
------
------------------------
博采眾家之長,奉獻綿薄之力
------------------------
hotswin
中階會員


發表:72
回覆:92
積分:52
註冊:2003-11-06

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-02-24 09:26:15 IP:59.125.xxx.xxx 訂閱
以前的文章來的
=================
var
i: Integer;
nModule, nProcess: Integer;
hProcess: THandle;
//hCurrentProcess: THandle;
IdProcess, IdModule: array[1..1024] of THandle;
ProcessName, ProcessPath: array[1..1024] of Char;
cbNeed: DWORD;
Prev_TP, New_TP: TTokenPrivileges;
Token: THandle;
dwRetLen: DWord;
MBI: MEMORY_BASIC_INFORMATION;
pMem: DWORD;
begin
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_DEFAULT or TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
New_TP.PrivilegeCount := 1;
if (LookupPrivilegeValue(nil, 'SeDebugPrivilege', New_TP.Privileges[0].LUID)) then
begin
New_TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
dwRetLen := 0;
if AdjustTokenPrivileges(
Token, // handle to token that contains privileges
False, // modifies privileges
New_TP, // pointer to new privilege information
SizeOf(Prev_TP), // size, in bytes, of the TPPrev buffer
Prev_TP, // receives original state of changed privileges
dwRetLen // receives required size of the TPPrev buffer
)then
begin
/// Memo1.Lines.Add('Adjust Privileges - OK!');
/// Memo1.Lines.Add('');
end;
end;
CloseHandle(Token);
/////////////////////////////////////////////////////////////////////////
if EnumProcesses(@IdProcess, sizeof(IdProcess), cbNeed) then
begin
nProcess := cbNeed div 4;
for i := 1 to nProcess do // 依次讀取 Process
begin
hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, IdProcess[i]);
if (hProcess <> 0) then
begin
if (EnumProcessModules(hProcess, @IdModule, sizeof(IdModule), cbNeed)) then
begin
nModule := cbNeed div 4;
GetModuleBaseName(hProcess, IdModule[1], @ProcessName[1], 1024);
GetModuleFileNameEx(hProcess, IdModule[1], @ProcessPath[1], 1024);
// 這兩行快速測試用
// if Pos('DMBD.EXE', UpperCase(StrPas(@ProcessPath[1]))) <> 0 then
// begin

Memo1.Lines.Add(Format('PID: d', [IdProcess[i]]));
Memo1.Lines.Add(Format('* %-24s - %-200s', [StrPas(@ProcessName[1]), StrPas(@ProcessPath[1])]));
Memo1.Lines.Add(Format('PID: d : %-24s - %-200s', [IdProcess[i], StrPas(@ProcessName[1]), StrPas(@ProcessPath[1])]));
pMem := 0;
// iUse := 0;
while VirtualQueryEx(hProcess, Pointer(pMem), MBI, sizeof(MBI)) = sizeof(MBI) do
begin
// Application.ProcessMessages;
if pMem >= $10000000 then break;
pMem := pMem MBI.RegionSize;
end; //while VirtualQueryEx(hProcess, Pointer(pMem), MBI, sizeof(MBI)) = sizeof(MBI) do
//end; // 快速測試用 if Pos('NOTEPAD', UpperCase(StrPas(@ProcessPath[1]))) <> 0 then
end else begin
//Memo1.Lines.Add(' * ' IntToStr(GetLastError));
end; //if (EnumProcessModules(hProcess, @IdModule, sizeof(IdModule), cbNeed)) then
CloseHandle(hProcess);
end else begin
// Memo1.Lines.Add(Format('PID: %d Can''t Open! (Error Code: %d)', [IdProcess[i], GetLastError]));
end; // if (hProcess <> 0) then
// WSACleanup;
end; // for i := 1 to nProcess do
end else begin
// Memo1.Lines.Add('Not Found Processes !');
end; // if EnumProcesses(@IdProcess, sizeof(IdProcess), cbNeed) then
// Memo1.Lines.Add(TimeToStr(Time));
end;
------
xinjier禮品贈品
change.jian
版主


發表:29
回覆:620
積分:439
註冊:2003-06-02

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-02-26 14:12:33 IP:211.23.xxx.xxx 訂閱
1.使用 CreateToolhelp32Snapshot / Process32First 等關鍵字,可以收尋到取得目前作業系統中所有執行中的 process 及相關的資料.
2.欲取得 window 的 class name ,可以呼叫 GetClassName

sample:
[code delphi]
function GetClassNameWithHandle(h: HWND): String;
var
sN:array[0..254] of char;
begin
if GetClassName(h,sN,SizeOf(sN))>0 then
Result:=string(sN)
else
Result:='';
end;

[/code]
h@visli
資深會員


發表:103
回覆:429
積分:431
註冊:2004-02-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-02-29 18:25:47 IP:218.18.xxx.xxx 未訂閱
GetClassName是不是衹能獲得Window的ClassName?

int GetClassName(

HWND hWnd, // handle of window <--該參數
LPTSTR lpClassName, // address of buffer for class name
int nMaxCount // size of buffer, in characters
);

另外請問如何獲取Thread的Handle?我可以通過下面的代碼獲取所有Thread,但還不能取得每個Thread的ClassName:

[code delphi]
procedure TForm1.Button5Click(Sender: TObject);
var
FsnapShotHandle: Thandle;
FThreadEntry32: TThreadEntry32;
ListLoop: Boolean;
cid: Integer;
begin
cid := GetCurrentProcessId;
FsnapShotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
FThreadEntry32.dwSize := SizeOf(FThreadEntry32);
ListLoop := Thread32First(FsnapShotHandle, FThreadEntry32);
while ListLoop do
begin
if FThreadEntry32.th32OwnerProcessID = cid then
Memo1.Lines.Add(Format('PID: %d, TID: %d, Usage: %d',
[FThreadEntry32.th32OwnerProcessID, FThreadEntry32.th32ThreadID, FThreadEntry32.cntUsage]));
ListLoop := Thread32Next(FsnapShotHandle, FThreadEntry32);
end;
CloseHandle(FsnapShotHandle);

end;

[/code]
------
------------------------
博采眾家之長,奉獻綿薄之力
------------------------
編輯記錄
h@visli 重新編輯於 2008-02-29 18:30:17, 註解 無‧
change.jian
版主


發表:29
回覆:620
積分:439
註冊:2003-06-02

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-03-02 13:39:09 IP:140.131.xxx.xxx 訂閱
1.GetClassName 是只能取得 windows 的 class name
2.不清楚您所謂的 thread 的 class name 指的是什麼,依我認知的, class name 一般都是指 window 的 class name ,而 TThreadEntry32 所記錄的資訊,也沒有 class name.你可以舉個例子說明所謂 thread class name 是指什麼嗎?


===================引 用 h@visli 文 章===================
GetClassName是不是衹能獲得Window的ClassName?

int GetClassName(

HWND hWnd, // handle of window <--該參數
LPTSTR lpClassName, // address of buffer for class name
int nMaxCount // size of buffer, in characters
);

另外請問如何獲取Thread的Handle?我可以通過下面的代碼獲取所有Thread,但還不能取得每個Thread的ClassName:

[code delphi]
procedure TForm1.Button5Click(Sender: TObject);
var
FsnapShotHandle: Thandle;
FThreadEntry32: TThreadEntry32;
ListLoop: Boolean;
cid: Integer;
begin
cid := GetCurrentProcessId;
FsnapShotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
FThreadEntry32.dwSize := SizeOf(FThreadEntry32);
ListLoop := Thread32First(FsnapShotHandle, FThreadEntry32);
while ListLoop do
begin
if FThreadEntry32.th32OwnerProcessID = cid then
Memo1.Lines.Add(Format('PID: %d, TID: %d, Usage: %d',
[FThreadEntry32.th32OwnerProcessID, FThreadEntry32.th32ThreadID, FThreadEntry32.cntUsage]));
ListLoop := Thread32Next(FsnapShotHandle, FThreadEntry32);
end;
CloseHandle(FsnapShotHandle);

end;

[/code]
h@visli
資深會員


發表:103
回覆:429
積分:431
註冊:2004-02-13

發送簡訊給我
#6 引用回覆 回覆 發表時間:2008-03-03 08:32:46 IP:58.61.xxx.xxx 未訂閱
我所指的Class name是想取如:TMyThread = class(TThread)...end裡的"TMyThread"字符。不過我想這也是沒辦法通過Windows API做到的。

===================引 用 change.jian 文 章===================
1.GetClassName 是只能取得 windows 的 class name
2.不清楚您所謂的 thread 的 class name 指的是什麼,依我認知的, class name 一般都是指 window 的 class name ,而 TThreadEntry32 所記錄的資訊,也沒有 class name.你可以舉個例子說明所謂 thread class name 是指什麼嗎?
------
------------------------
博采眾家之長,奉獻綿薄之力
------------------------
系統時間:2024-04-19 10:49:13
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!