您好,我正在尝试获取系统上 64 位进程的线程上下文。我试过使用具有正确功能的 32 位和 64 位解决方案。但我总是以错误“0x57”结束,参数无效。来自 64 位代码的简短示例。
// open a handle to the thread
HANDLE hThread = OpenThread(THREAD_GET_CONTEXT | THREAD_SET_CONTEXT |
THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION, FALSE,
atoi(argv[1]));
if(hThread == NULL) {
printf("Error opening thread handle.. 0x%08x\n", GetLastError());
return 0;
}
// suspend the thread
if(Wow64SuspendThread(hThread ) == -1) {
printf("Error suspending thread.. 0x%08x\n", GetLastError());
CloseHandle(hThread );
return 0;
}
// get the thread context
WOW64_CONTEXT orig_ctx = {WOW64_CONTEXT_FULL };
if(GetThreadContext(hThread , &orig_ctx) == FALSE) {
printf("Error 0x%08x\n", GetLastError());
CloseHandle(hThread );
return 0;
}
我怀疑句柄是错误的,代码在 32 位进程上工作正常。我将不胜感激任何帮助或建议。提前致谢!
最佳答案
以下代码在编译为 64 位应用程序时成功检索 64 位线程的线程上下文。
// threadcontext.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
// open a handle to the thread
HANDLE hThread = OpenThread(THREAD_GET_CONTEXT | THREAD_SET_CONTEXT |
THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION, FALSE, _ttoi(argv[1]));
if(hThread == NULL) {
printf("Error opening thread handle.. 0x%08x\n", GetLastError());
return 0;
}
// suspend the thread
if(SuspendThread(hThread ) == -1) {
printf("Error suspending thread.. 0x%08x\n", GetLastError());
CloseHandle(hThread );
return 0;
}
// get the thread context
CONTEXT orig_ctx = { 0 };
orig_ctx.ContextFlags = CONTEXT_FULL;
if(GetThreadContext(hThread , &orig_ctx) == FALSE) {
printf("Error 0x%08x\n", GetLastError());
CloseHandle(hThread );
return 0;
}
return 0;
}
需要注意的一件事是,常规调用和 Wow64 调用没有混合。 Wow64 调用用于获取有关在 64 位系统上运行的 32 位进程的信息。
另一个更正是 ContextFlags 成员的设置。您试图在初始化期间设置它,但 ContextFlags 成员不是结构中的第一个成员。
关于无法从 Windows 64 位进程获取线程上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11396034/