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\n",i);
    }
    return 0;
}

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

DWORD WINAPI ThreadProc2(LPVOID lpParamter)
{
    for (int j = 0;j<50;j++)
    {
        Sleep(50);
        printf("ThreadProc2********* %d\n",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\n",context.Eax,context.Ecx); context.Eax = 1; context.Ecx = 2;

  //把修改好的context传入thread SetThreadContext(hThread[
0],&context); printf("%x -- %x\n",context.Eax,context.Ecx); GetThreadContext(hThread[0],&context); printf("%x -- %x\n",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; }

 

posted @ 2020-06-16 14:04  温暖了寂寞  阅读(1981)  评论(0编辑  收藏  举报