gpt4 book ai didi

无法从 Windows 64 位进程获取线程上下文

转载 作者:太空宇宙 更新时间:2023-11-04 00:08:55 28 4
gpt4 key购买 nike

您好,我正在尝试获取系统上 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com