11
11
#include " threadInfo.h"
12
12
#include " ntinfo.h"
13
13
14
+ std::vector<DWORD> threadList (DWORD pid);
15
+ DWORD GetThreadStartAddress (HANDLE processHandle, HANDLE hThread);
16
+
17
+ bool isGameAvail;
18
+
19
+ int main (int argc, char ** argv) {
20
+ std::string gameName = " osu!" ;
21
+ LPCSTR LGameName = " osu!" ;
22
+
23
+ HWND hGameWindow = NULL ;
24
+ DWORD dwProcID = NULL ;
25
+ HANDLE hProcHandle = NULL ;
26
+ isGameAvail = false ;
27
+
28
+ // keep polling until target process is opened
29
+ std::cout << " Looking for " << gameName << std::endl;
30
+ while (!isGameAvail) {
31
+ hGameWindow = FindWindow (NULL , LGameName);
32
+ GetWindowThreadProcessId (hGameWindow, &dwProcID);
33
+ if (!dwProcID)
34
+ continue ;
35
+
36
+ std::cout << " Found it PID " << dwProcID << std::endl;
37
+ std::cout << " Grabbing handle" << std::endl;
38
+ hProcHandle = OpenProcess (PROCESS_ALL_ACCESS, FALSE , dwProcID);
39
+
40
+ if (hProcHandle == INVALID_HANDLE_VALUE || hProcHandle == NULL ) {
41
+ std::cerr << " Failed to open process -- invalid handle" << std::endl;
42
+ }
43
+ else {
44
+ std::cout << " Success" << std::endl;
45
+ isGameAvail = true ;
46
+ }
47
+ }
48
+
49
+ std::vector<DWORD> threadId = threadList (dwProcID);
50
+ int stackNum = 0 ;
51
+ for (auto it = threadId.begin (); it != threadId.end (); ++it) {
52
+ HANDLE threadHandle = OpenThread (THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE , *it);
53
+ DWORD threadStartAddress = GetThreadStartAddress (hProcHandle, threadHandle);
54
+ printf (" TID: 0x%04x = THREADSTACK%2d BASE ADDRESS: 0x%04x\n " , *it, stackNum, threadStartAddress);
55
+ stackNum++;
56
+ }
57
+
58
+ return EXIT_SUCCESS;
59
+ }
60
+
14
61
std::vector<DWORD> threadList (DWORD pid) {
15
62
// solution from http://stackoverflow.com/questions/1206878/enumerating-threads-in-windows
16
63
std::vector<DWORD> vect = std::vector<DWORD>();
@@ -25,12 +72,12 @@ std::vector<DWORD> threadList(DWORD pid) {
25
72
if (te.dwSize >= FIELD_OFFSET (THREADENTRY32, th32OwnerProcessID) +
26
73
sizeof (te.th32OwnerProcessID )) {
27
74
28
-
75
+
29
76
if (te.th32OwnerProcessID == pid) {
30
77
printf (" PID: %04d Thread ID: 0x%04x\n " , te.th32OwnerProcessID , te.th32ThreadID );
31
78
vect.push_back (te.th32ThreadID );
32
79
}
33
-
80
+
34
81
}
35
82
te.dwSize = sizeof (te);
36
83
} while (Thread32Next (h, &te));
@@ -45,7 +92,7 @@ DWORD GetThreadStartAddress(HANDLE processHandle, HANDLE hThread) {
45
92
DWORD stacktop = 0 , result = 0 ;
46
93
47
94
MODULEINFO mi;
48
-
95
+
49
96
GetModuleInformation (processHandle, LoadLibrary (" kernel32.dll" ), &mi, sizeof (mi));
50
97
stacktop = (DWORD)GetThreadStackTopAddress_x86 (processHandle, hThread);
51
98
@@ -76,15 +123,15 @@ DWORD GetThreadStartAddress(HANDLE processHandle, HANDLE hThread) {
76
123
// find the stack entry pointing to the function that calls "ExitXXXXXThread"
77
124
// Fun thing to note: It's the first entry that points to a address in kernel32
78
125
79
- DWORD* buf32 = new DWORD[4096 / 4 ];
80
-
126
+ DWORD* buf32 = new DWORD[4096 / 4 ];
127
+
81
128
if (ReadProcessMemory (processHandle, (LPCVOID)(stacktop - 4096 ), buf32, 4096 , NULL )) {
82
129
for (int i = 4096 / 4 - 1 ; i >= 0 ; --i) {
83
130
if (buf32[i] >= (DWORD)mi.lpBaseOfDll && buf32[i] <= (DWORD)mi.lpBaseOfDll + mi.SizeOfImage ) {
84
131
result = stacktop - 4096 + i * 4 ;
85
132
break ;
86
133
}
87
-
134
+
88
135
}
89
136
}
90
137
@@ -93,47 +140,3 @@ DWORD GetThreadStartAddress(HANDLE processHandle, HANDLE hThread) {
93
140
94
141
return result;
95
142
}
96
-
97
- bool isGameAvail;
98
-
99
- int main (int argc, char ** argv) {
100
- std::string gameName = " osu!" ;
101
- LPCSTR LGameName = " osu!" ;
102
-
103
- HWND hGameWindow = NULL ;
104
- DWORD dwProcID = NULL ;
105
- HANDLE hProcHandle = NULL ;
106
- isGameAvail = false ;
107
-
108
- // keep polling until target process is opened
109
- std::cout << " Looking for " << gameName << std::endl;
110
- while (!isGameAvail) {
111
- hGameWindow = FindWindow (NULL , LGameName);
112
- GetWindowThreadProcessId (hGameWindow, &dwProcID);
113
- if (!dwProcID)
114
- continue ;
115
-
116
- std::cout << " Found it PID " << dwProcID << std::endl;
117
- std::cout << " Grabbing handle" << std::endl;
118
- hProcHandle = OpenProcess (PROCESS_ALL_ACCESS, FALSE , dwProcID);
119
-
120
- if (hProcHandle == INVALID_HANDLE_VALUE || hProcHandle == NULL ) {
121
- std::cerr << " Failed to open process -- invalid handle" << std::endl;
122
- }
123
- else {
124
- std::cout << " Success" << std::endl;
125
- isGameAvail = true ;
126
- }
127
- }
128
-
129
- std::vector<DWORD> threadId = threadList (dwProcID);
130
- int stackNum = 0 ;
131
- for (auto it = threadId.begin (); it != threadId.end (); ++it) {
132
- HANDLE threadHandle = OpenThread (THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE , *it);
133
- DWORD threadStartAddress = GetThreadStartAddress (hProcHandle, threadHandle);
134
- printf (" TID: 0x%04x = THREADSTACK%2d BASE ADDRESS: 0x%04x\n " , *it, stackNum, threadStartAddress);
135
- stackNum++;
136
- }
137
-
138
- return EXIT_SUCCESS;
139
- }
0 commit comments