|
[c]
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
#include <vector>
// 线程信息结构体
struct ThreadInfo {
DWORD threadId;
HANDLE hThread;
DWORD startTime;
};
// 获取进程的主线程ID
DWORD GetMainThreadId(DWORD processId) {
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
std::cerr << "CreateToolhelp32Snapshot failed: " << GetLastError() << std::endl;
return 0;
}
THREADENTRY32 te32;
te32.dwSize = sizeof(THREADENTRY32);
if (!Thread32First(hSnapshot, &te32)) {
std::cerr << "Thread32First failed: " << GetLastError() << std::endl;
CloseHandle(hSnapshot);
return 0;
}
std::vector<ThreadInfo> threads;
do {
if (te32.th32OwnerProcessID == processId) {
HANDLE hThread = OpenThread(THREAD_QUERY_INFORMATION, FALSE, te32.th32ThreadID);
if (hThread) {
FILETIME createTime, exitTime, kernelTime, userTime;
if (GetThreadTimes(hThread, &createTime, &exitTime, &kernelTime, &userTime)) {
ULARGE_INTEGER uli;
uli.LowPart = createTime.dwLowDateTime;
uli.HighPart = createTime.dwHighDateTime;
ThreadInfo info;
info.threadId = te32.th32ThreadID;
info.hThread = hThread;
info.startTime = uli.QuadPart;
threads.push_back(info);
}
else {
CloseHandle(hThread);
}
}
}
} while (Thread32Next(hSnapshot, &te32));
CloseHandle(hSnapshot);
// 找到最早创建的线程(主线程)
DWORD mainThreadId = 0;
ULARGE_INTEGER earliestTime = {0xFFFFFFFF, 0xFFFFFFFF};
for (const auto& thread : threads) {
ULARGE_INTEGER threadTime = {thread.startTime, 0};
if (threadTime.QuadPart < earliestTime.QuadPart) {
earliestTime = threadTime;
mainThreadId = thread.threadId;
}
CloseHandle(thread.hThread);
}
return mainThreadId;
}
// 在目标进程的主线程上下文中执行代码
bool ExecuteOnMainThread(DWORD processId, LPVOID remoteFunction, LPVOID param = NULL) {
// 获取目标进程句柄
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);
if (!hProcess) {
std::cerr << "OpenProcess failed: " << GetLastError() << std::endl;
return false;
}
// 获取主线程ID
DWORD mainThreadId = GetMainThreadId(processId);
if (mainThreadId == 0) {
std::cerr << "Failed to get main thread ID" << std::endl;
CloseHandle(hProcess);
return false;
}
// 获取主线程句柄
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, mainThreadId);
if (!hThread) {
std::cerr << "OpenThread failed: " << GetLastError() << std::endl;
CloseHandle(hProcess);
return false;
}
// 分配远程内存用于存储参数
LPVOID remoteParam = NULL;
if (param) {
remoteParam = VirtualAllocEx(hProcess, NULL, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!remoteParam) {
std::cerr << "VirtualAllocEx failed: " << GetLastError() << std::endl;
CloseHandle(hThread);
CloseHandle(hProcess);
return false;
}
if (!WriteProcessMemory(hProcess, remoteParam, param, 4096, NULL)) {
std::cerr << "WriteProcessMemory failed: " << GetLastError() << std::endl;
VirtualFreeEx(hProcess, remoteParam, 0, MEM_RELEASE);
CloseHandle(hThread);
CloseHandle(hProcess);
return false;
}
}
// 暂停主线程
SuspendThread(hThread);
// 创建远程线程在主线程上下文中执行
HANDLE hRemoteThread = CreateRemoteThread(
hProcess,
NULL,
0,
(LPTHREAD_START_ROUTINE)remoteFunction,
remoteParam,
0,
NULL
);
if (!hRemoteThread) {
std::cerr << "CreateRemoteThread failed: " << GetLastError() << std::endl;
ResumeThread(hThread);
if (remoteParam) VirtualFreeEx(hProcess, remoteParam, 0, MEM_RELEASE);
CloseHandle(hThread);
CloseHandle(hProcess);
return false;
}
// 等待远程线程执行完成
WaitForSingleObject(hRemoteThread, INFINITE);
// 清理资源
CloseHandle(hRemoteThread);
ResumeThread(hThread);
if (remoteParam) VirtualFreeEx(hProcess, remoteParam, 0, MEM_RELEASE);
CloseHandle(hThread);
CloseHandle(hProcess);
return true;
}
// 示例使用方法
int main() {
// 目标游戏进程ID
DWORD targetProcessId = 1234; // 替换为实际游戏进程ID
// 目标游戏中的函数地址(需要通过分析获取)
LPVOID targetFunctionAddress = (LPVOID)0x12345678; // 替换为实际函数地址
// 函数参数(如果有)
struct FunctionParams {
int param1;
float param2;
// 其他参数...
} params = {123, 4.56f};
// 在主线程上下文中执行目标函数
if (ExecuteOnMainThread(targetProcessId, targetFunctionAddress, ¶ms)) {
std::cout << "Function executed successfully on main thread" << std::endl;
} else {
std::cerr << "Failed to execute function on main thread" << std::endl;
}
return 0;
}
[/c] |
|