GetThreadContext和SetThreadContext

Posted ganxiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GetThreadContext和SetThreadContext相关的知识,希望对你有一定的参考价值。

 

#include "stdafx.h" 
#include <iostream>
#include <windows.h>

using namespace std;

DWORD WINAPI ThreadProc(LPVOID lpParamter)
{
    for (int i = 0;i<10;i++)
    {
        Sleep(1000);
        printf("ThreadProc1********* %d
",i);
    }
    return 0;
}

DWORD WINAPI ThreadProc1(LPVOID lpParamter)
{
    for (int i = 0;i<50;i++)
    {
        Sleep(100);
        printf("ThreadProc1********* %d
",i);
    }
    return 0;
}

DWORD WINAPI ThreadProc2(LPVOID lpParamter)
{
    for (int j = 0;j<50;j++)
    {
        Sleep(50);
        printf("ThreadProc2********* %d
",j);
    }
    return 5;
}

int main()
{
    //unsigned long ulThreadId = 0;
    HANDLE hThread[2];
    DWORD dwr1;
    DWORD dwr2;


    hThread[0] = CreateThread(NULL, 0, ThreadProc1, NULL, 0, NULL);
    //hThread[1] = CreateThread(NULL, 0, ThreadProc2, NULL, 0, NULL);

    //Sleep(2000);
    SuspendThread(hThread[0]);

    CONTEXT context;
    context.ContextFlags = CONTEXT_INTEGER;

  //从线程获取寄存器的值存入context GetThreadContext(hThread[
0],&context); printf("%x -- %x ",context.Eax,context.Ecx); context.Eax = 1; context.Ecx = 2;

  //把修改好的context传入thread SetThreadContext(hThread[
0],&context); printf("%x -- %x ",context.Eax,context.Ecx); GetThreadContext(hThread[0],&context); printf("%x -- %x ",context.Eax,context.Ecx); ResumeThread(hThread[0]); // WaitForMultipleObjects(2,hThread,TRUE,INFINITE); // GetExitCodeThread(hThread[0],&dwr1); // GetExitCodeThread(hThread[1],&dwr2); cout << "线程结束了!" <<endl; getchar(); CloseHandle(hThread[0]); CloseHandle(hThread[1]); //system("pause"); return 0; }

 

以上是关于GetThreadContext和SetThreadContext的主要内容,如果未能解决你的问题,请参考以下文章

Win 64bit GetThreadContext 返回归零的寄存器,或 0x57 错误代码

多线程编程(Context)

傀儡进程脱壳三步曲

Ollydbg中F8的Bug

注入理解之挂起线程

通过 FS 寄存器访问衍生进程的 TIB?