2012-07-09 27 views
2

您好,我正试图获取系统上64位进程的线程上下文。我试过用正确的函数同时使用32位和64位解决方案。但我总是以错误'0x57',无效的参数结束。来自64位代码的简短示例。无法从Windows 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位进程上正常工作。我将不胜感激任何帮助或建议。提前致谢!

+0

哪个功能失败? – hmjd 2012-07-09 13:32:35

+0

所以你尝试'Wow64GetThreadContext()'? – alk 2012-07-09 13:42:00

+0

对不起,我没有澄清,WoW64GetThreadContext失败,错误'错误'0x57',无效参数'。 GetThreadContext也是如此。 – 2012-07-09 14:13:31

回答

0

以下代码在编译为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成员不是结构中的第一个成员。

相关问题