Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <tlhelp32.h>
- #include <stdio.h>
- #include <wchar.h>
- DWORD GetProcessIdByName(const wchar_t* name);
- void ListProcessThreads(DWORD owner_pid);
- int main() {
- DWORD pid = GetProcessIdByName(L"Gw2.exe");
- if (pid == 0) return 1;
- ListProcessThreads(pid);
- return 0;
- }
- DWORD GetProcessIdByName(const wchar_t* name) {
- HANDLE process_snapshot;
- PROCESSENTRY32 process_entry;
- process_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if(process_snapshot == INVALID_HANDLE_VALUE) return 0;
- process_entry.dwSize = sizeof(PROCESSENTRY32);
- if(!Process32First(process_snapshot, &process_entry)) return 0;
- do {
- if (wcscmp(process_entry.szExeFile, name) == 0) {
- CloseHandle(process_snapshot);
- return process_entry.th32ProcessID;
- }
- } while(Process32Next( process_snapshot, &process_entry));
- CloseHandle(process_snapshot);
- return 0;
- }
- void PrintTIB(DWORD thread_id) {
- HANDLE thread_handle = OpenThread(THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, thread_id);
- if (thread_handle == NULL) return;
- SuspendThread(thread_handle);
- CONTEXT context;
- context.ContextFlags = CONTEXT_SEGMENTS;
- if (!GetThreadContext(thread_handle, &context)) {
- CloseHandle(thread_handle);
- return;
- }
- LDT_ENTRY ldtSel;
- if (!GetThreadSelectorEntry(thread_handle, context.SegFs, &ldtSel)) return;
- ResumeThread(thread_handle);
- DWORD fs_base = (ldtSel.HighWord.Bits.BaseHi << 24 ) | ( ldtSel.HighWord.Bits.BaseMid << 16 ) | ( ldtSel.BaseLow );
- fwprintf(stdout, L"[i] FS:[0] (TIB) is @ 0x%08X\n", fs_base);
- }
- void ListProcessThreads(DWORD owner_pid) {
- HANDLE thread_snapshot = INVALID_HANDLE_VALUE;
- THREADENTRY32 thread_entry;
- thread_snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
- if(thread_snapshot == INVALID_HANDLE_VALUE) return;
- thread_entry.dwSize = sizeof(THREADENTRY32);
- if(!Thread32First(thread_snapshot, &thread_entry )) return;
- do {
- if (thread_entry.th32OwnerProcessID == owner_pid) {
- fwprintf(stdout, L"[i] thread_id = %d \n", thread_entry.th32ThreadID);
- PrintTIB(thread_entry.th32ThreadID);
- }
- } while(Thread32Next(thread_snapshot, &thread_entry ));
- CloseHandle(thread_snapshot);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement