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

标签 c visual-studio-2010 visual-studio winapi 64-bit

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

相关文章:

c - TCP 服务器 - 从 "Too many open files"恢复

.net - 有没有一种简单的方法可以在 VS 2010 中签署 C++ CLI 程序集?

visual-studio-2010 - 如何使 IIS Express 成为 Visual Studio 中的默认 Web 服务器?

c# - 在 Visual Studio 2010 中查找类后代

c - 从双链表中删除

c - 警告 - 赋值使指针来自整数而不进行强制转换

C函数剖析(地址好像有偏移)

javascript - 使用 YUI Compressor 任务 MSBuild Event 创建新目录

c++ - 为什么 MS Visual Studio #include <stdio.h> 默认情况下在 stdafx.h 中?

visual-studio - Visual Studio 部署项目 - 创建已部署可执行文件的快捷方式