Skip to content

Commit 655dd77

Browse files
committed
use function predefinition
1 parent 3c8ac38 commit 655dd77

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed

main.cpp

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,53 @@
1111
#include "threadInfo.h"
1212
#include "ntinfo.h"
1313

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+
1461
std::vector<DWORD> threadList(DWORD pid) {
1562
// solution from http://stackoverflow.com/questions/1206878/enumerating-threads-in-windows
1663
std::vector<DWORD> vect = std::vector<DWORD>();
@@ -25,12 +72,12 @@ std::vector<DWORD> threadList(DWORD pid) {
2572
if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
2673
sizeof(te.th32OwnerProcessID)) {
2774

28-
75+
2976
if (te.th32OwnerProcessID == pid) {
3077
printf("PID: %04d Thread ID: 0x%04x\n", te.th32OwnerProcessID, te.th32ThreadID);
3178
vect.push_back(te.th32ThreadID);
3279
}
33-
80+
3481
}
3582
te.dwSize = sizeof(te);
3683
} while (Thread32Next(h, &te));
@@ -45,7 +92,7 @@ DWORD GetThreadStartAddress(HANDLE processHandle, HANDLE hThread) {
4592
DWORD stacktop = 0, result = 0;
4693

4794
MODULEINFO mi;
48-
95+
4996
GetModuleInformation(processHandle, LoadLibrary("kernel32.dll"), &mi, sizeof(mi));
5097
stacktop = (DWORD)GetThreadStackTopAddress_x86(processHandle, hThread);
5198

@@ -76,15 +123,15 @@ DWORD GetThreadStartAddress(HANDLE processHandle, HANDLE hThread) {
76123
//find the stack entry pointing to the function that calls "ExitXXXXXThread"
77124
//Fun thing to note: It's the first entry that points to a address in kernel32
78125

79-
DWORD* buf32 = new DWORD[4096/4];
80-
126+
DWORD* buf32 = new DWORD[4096 / 4];
127+
81128
if (ReadProcessMemory(processHandle, (LPCVOID)(stacktop - 4096), buf32, 4096, NULL)) {
82129
for (int i = 4096 / 4 - 1; i >= 0; --i) {
83130
if (buf32[i] >= (DWORD)mi.lpBaseOfDll && buf32[i] <= (DWORD)mi.lpBaseOfDll + mi.SizeOfImage) {
84131
result = stacktop - 4096 + i * 4;
85132
break;
86133
}
87-
134+
88135
}
89136
}
90137

@@ -93,47 +140,3 @@ DWORD GetThreadStartAddress(HANDLE processHandle, HANDLE hThread) {
93140

94141
return result;
95142
}
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

Comments
 (0)