请问怎样通过Process ID获得Thread handle,不用debug api (100分)

//用 TOOL HELP API (TLHelp32)<br>//ThreadsListView 是 TListView<br><br>procedure TMainForm.BuildThreadsList(ProcessID: DWORD);<br>var<br>&nbsp; SnapProcHandle: THandle;<br>&nbsp; ThreadEntry: TThreadEntry32;<br>&nbsp; Next: Boolean;<br>begin<br>&nbsp; with ThreadsListView do<br>&nbsp; try<br>&nbsp; &nbsp; Items.BeginUpdate;<br>&nbsp; &nbsp; Items.Clear;<br>&nbsp; &nbsp; SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);<br>&nbsp; &nbsp; if SnapProcHandle &lt;&gt; THandle(-1) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ThreadEntry.dwSize := Sizeof(ThreadEntry);<br>&nbsp; &nbsp; &nbsp; Next := Thread32First(SnapProcHandle, ThreadEntry);<br>&nbsp; &nbsp; &nbsp; while Next do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if ThreadEntry.th32OwnerProcessID = ProcessID then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with Items.Add, ThreadEntry do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Caption := Format('%.8x', [th32ThreadID]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Data := Pointer(th32ThreadID);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SubItems.AddObject(Format('%d', [tpDeltaPri]), Pointer(tpDeltaPri));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; Next := Thread32Next(SnapProcHandle, ThreadEntry);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; CloseHandle(SnapProcHandle);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; AlphaSort;<br>&nbsp; &nbsp; ListViewFocusFirstItem(ThreadsListView);<br>&nbsp; finally<br>&nbsp; &nbsp; Items.EndUpdate;<br>&nbsp; end;<br>end;<br>
 
拜托,我要的是Thread handle,不是Thread ID!<br>有人能帮忙么?
 
你无法获得其它进程的Handle,除非你对它进行调试。
 
哦,大家都这样说。可我这里有能实现通过Process ID获得Thread handle的VC代码,<br>他是用未公开的的了一个结构。可我看不大懂,先贴出来,希望谁能翻成delphi并<br>分析一下。<br>关键代码:<br>#include "stdafx.h"<br>#include "InjectCode.h"<br><br>#ifdef _DEBUG<br>#define new DEBUG_NEW<br>#undef THIS_FILE<br>static char THIS_FILE[] = __FILE__;<br>#endif<br><br>typedef struct t_PDB<br>{<br>&nbsp; &nbsp; WORD &nbsp;Type;<br>&nbsp; &nbsp; WORD &nbsp;Refcount;<br>&nbsp; &nbsp; DWORD Unk0;<br>&nbsp; &nbsp; DWORD Unk1;<br>&nbsp; &nbsp; DWORD Unk2;<br>&nbsp; &nbsp; DWORD TermStatus;<br>&nbsp; &nbsp; DWORD Unk3;<br>&nbsp; &nbsp; DWORD DefaultHeap;<br>&nbsp; &nbsp; DWORD MemContext; &nbsp;<br>&nbsp; &nbsp; DWORD Flags;<br>&nbsp; &nbsp; DWORD pPsp;<br>&nbsp; &nbsp; WORD &nbsp;PSPSelector;<br>&nbsp; &nbsp; WORD &nbsp;MTEIndex;<br>&nbsp; &nbsp; WORD &nbsp;nThreads;<br>&nbsp; &nbsp; WORD &nbsp;nThreadsNotTerm;<br>&nbsp; &nbsp; WORD &nbsp;Unk5;<br>&nbsp; &nbsp; WORD &nbsp;nR0Threads;<br>&nbsp; &nbsp; DWORD HeapHandle;<br>&nbsp; &nbsp; WORD &nbsp;K16TDBSel;<br>&nbsp; &nbsp; WORD &nbsp;Unk6;<br>&nbsp; &nbsp; DWORD Unk7;<br>&nbsp; &nbsp; DWORD pEDB;<br>&nbsp; &nbsp; DWORD pHandleTable;<br>&nbsp; &nbsp; struct t_PDB *ParentPDB;<br>&nbsp; &nbsp; DWORD MODREFList;<br>&nbsp; &nbsp; DWORD ThreadList;<br>&nbsp; &nbsp; DWORD DebugeeCB;<br>&nbsp; &nbsp; DWORD LHFreeHead;<br>&nbsp; &nbsp; DWORD InitialR0ID;<br>}PDB, *PPDB;<br><br>typedef struct t_TCB<br>{<br>&nbsp; &nbsp; WORD &nbsp;Type;<br>&nbsp; &nbsp; WORD &nbsp;RefCount;<br>&nbsp; &nbsp; DWORD Unk1;<br>&nbsp; &nbsp; DWORD pvExcept;<br>&nbsp; &nbsp; DWORD TopOfStack;<br>&nbsp; &nbsp; DWORD BaseOfStack;<br>&nbsp; &nbsp; WORD &nbsp;K16TDB;<br>&nbsp; &nbsp; WORD &nbsp;StackSel16;<br>&nbsp; &nbsp; DWORD Unk2;<br>&nbsp; &nbsp; DWORD UserPointer;<br>&nbsp; &nbsp; DWORD pTIB;<br>&nbsp; &nbsp; WORD &nbsp;TIBflags;<br>&nbsp; &nbsp; WORD &nbsp;Win16MutxCnt;<br>&nbsp; &nbsp; DWORD DebugContext;<br>&nbsp; &nbsp; DWORD PtrToCurPri;<br>&nbsp; &nbsp; DWORD MsgQueue;<br>&nbsp; &nbsp; DWORD pTLSarray;<br>&nbsp; &nbsp; PPDB &nbsp;pParentPDB;<br>&nbsp; &nbsp; DWORD SelmanList;<br>&nbsp; &nbsp; DWORD Unk3;<br>&nbsp; &nbsp; DWORD Flags;<br>&nbsp; &nbsp; DWORD Status;<br>&nbsp; &nbsp; WORD &nbsp;TIBsel;<br>&nbsp; &nbsp; WORD &nbsp;EmulatorSel;<br>&nbsp; &nbsp; DWORD HandleCount;<br>&nbsp; &nbsp; DWORD WaitNodeList;<br>&nbsp; &nbsp; DWORD R0hThread;<br>&nbsp; &nbsp; DWORD ptdbx;<br>}TCB, *PTCB;<br><br>typedef DWORD (WINAPI*OTFUNC)(HANDLE*,DWORD,void*,void*);<br>typedef LPVOID (WINAPI *OBFUNC)(DWORD dwPTID);<br>LPVOID WINAPI GetTrueProcAddress(LPSTR lpMod, LPTSTR lpFunc);<br>HANDLE WINAPI OpenThreadNT(DWORD dwThreadID, BOOL bInherit);<br><br>LPVOID WINAPI XORProcessThreadID(DWORD dwPTID)<br>{<br>&nbsp; &nbsp; OBFUNC obfuscate;<br>&nbsp; &nbsp; DWORD dwMain,*lpdw,dw1;<br><br>&nbsp; &nbsp; dwMain = (DWORD)GetTrueProcAddress(_T("KERNEL32"),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_T("GetCurrentThreadId"));<br>&nbsp; &nbsp; lpdw = (LPDWORD)((DWORD)dwMain + 8);<br>&nbsp; &nbsp; dw1 = ((DWORD)dwMain + 12);<br><br>&nbsp; &nbsp; obfuscate = (OBFUNC)(dw1+*lpdw);<br><br>&nbsp; &nbsp; return(obfuscate(dwPTID));<br>}<br><br>//这个函数,传给他ThreadID,返回ThreadHandle<br>HANDLE WINAPI OpenThread2(DWORD dwThreadID, BOOL bInherit)<br>{<br>&nbsp; &nbsp; HANDLE hThread,hprc;<br>&nbsp; &nbsp; LPDWORD lp1;<br>&nbsp; &nbsp; DWORD dwProcessID,dwWhere,dwTable;<br>&nbsp; &nbsp; BOOL b1;<br>&nbsp; &nbsp; PTCB lpThreadObj;<br>&nbsp; &nbsp; PPDB ppdb;<br>&nbsp; &nbsp; OSVERSIONINFO osvi;<br><br>&nbsp; &nbsp; osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);<br>&nbsp; &nbsp; GetVersionEx(&amp;osvi);<br><br>&nbsp; &nbsp; SetLastError(50);<br>&nbsp; &nbsp; if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)<br>&nbsp; &nbsp; &nbsp; &nbsp; return OpenThreadNT(dwThreadID, bInherit);<br><br>&nbsp; &nbsp; ppdb = (PPDB)XORProcessThreadID(GetCurrentProcessId());<br>&nbsp; &nbsp; lpThreadObj = (PTCB)XORProcessThreadID(dwThreadID);<br><br>&nbsp; &nbsp; /* check to make sure its valid */<br>&nbsp; &nbsp; if(IsBadReadPtr(lpThreadObj, sizeof(TCB))) return NULL;<br><br>&nbsp; &nbsp; /* object type */<br>&nbsp; &nbsp; if(*(LPBYTE)lpThreadObj != 7) return NULL;<br><br>&nbsp; &nbsp; dwProcessID =<br>&nbsp; &nbsp; &nbsp; &nbsp; (DWORD)XORProcessThreadID((DWORD)lpThreadObj-&gt;pParentPDB);<br><br>&nbsp; &nbsp; if(dwProcessID == GetCurrentProcessId())<br>&nbsp; &nbsp; &nbsp; &nbsp; hprc = GetCurrentProcess();<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; hprc = OpenProcess(PROCESS_ALL_ACCESS,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FALSE, dwProcessID);<br>&nbsp; &nbsp; &nbsp; &nbsp; if(!hprc) return NULL;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; /*<br>&nbsp; &nbsp; &nbsp;* 4 is the lowest handle in the table.<br>&nbsp; &nbsp; &nbsp;* All processes have this handle.<br>&nbsp; &nbsp; &nbsp;*/<br>&nbsp; &nbsp; b1 = DuplicateHandle(hprc,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(HANDLE)4,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetCurrentProcess(),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;hThread,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;THREAD_ALL_ACCESS,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bInherit, 0);<br><br>&nbsp; &nbsp; if(hprc != GetCurrentProcess())<br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(hprc);<br>&nbsp; &nbsp; if(!b1) return NULL;<br><br>&nbsp; &nbsp; dwWhere = ((DWORD)hThread) &gt;&gt; 2;<br>&nbsp; &nbsp; dwTable = ppdb-&gt;pHandleTable;<br>&nbsp; &nbsp; lp1 = (LPDWORD)(dwTable+(dwWhere*8)+8);<br><br>&nbsp; &nbsp; *lp1 = (DWORD)lpThreadObj;<br><br>&nbsp; &nbsp; return(hThread);<br>}<br><br>HANDLE WINAPI OpenThreadNT(DWORD dwThreadID, BOOL bInherit)<br>{<br>&nbsp; &nbsp; HANDLE hThread = NULL;<br>&nbsp; &nbsp; DWORD struct1[] = {0x18, 0, 0, 0, 0, 0};<br>&nbsp; &nbsp; DWORD struct2[] = {0,dwThreadID};<br>&nbsp; &nbsp; HMODULE hLib = LoadLibrary(_T("ntdll.dll"));<br><br>&nbsp; &nbsp; OTFUNC OpenThatNTThread =<br>&nbsp; &nbsp; &nbsp; &nbsp; (OTFUNC)GetProcAddress(hLib, _T("NtOpenThread"));<br><br>&nbsp; &nbsp; struct1[3] = bInherit;<br><br>&nbsp; &nbsp; OpenThatNTThread(&amp;hThread, THREAD_ALL_ACCESS, struct1, struct2);<br><br>&nbsp; &nbsp; FreeLibrary(hLib);<br><br>&nbsp; &nbsp; return hThread;<br>}<br><br>LPVOID WINAPI GetTrueProcAddress(LPSTR lpMod, LPTSTR lpFunc)<br>{<br>&nbsp; &nbsp; LPVOID bla = GetProcAddress(GetModuleHandle(lpMod), lpFunc);<br><br>&nbsp; &nbsp; if(!bla) return NULL;<br><br>&nbsp; &nbsp; if(*(LPBYTE)bla == 0x68)<br>&nbsp; &nbsp; &nbsp; &nbsp; bla = (LPVOID)*(LPDWORD)((DWORD)bla + 1);<br><br>&nbsp; &nbsp; return bla;<br>}<br>
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
905
import
I
顶部