码迷,mamicode.com
首页 > 其他好文 > 详细

GetThreadContext和SetThreadContext

时间:2020-06-16 14:42:24      阅读:52      评论:0      收藏:0      [点我收藏+]

标签:class   exit   ace   create   好的   integer   and   获取   printf   

 

#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; }

 

GetThreadContext和SetThreadContext

标签:class   exit   ace   create   好的   integer   and   获取   printf   

原文地址:https://www.cnblogs.com/ganxiang/p/13140624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!