Muhamad Hesham's T-Blog

A growing computer scientist mind

  • Control Panel

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 7 other followers

  • acmASCIS

  • Twitter Updates

    • إذا الشعب يوما أراد الحياة .. فلا بد أن يشرب بيريل 1 week ago
    • عزيزي الإسلامي صانع الفيديوهات: إما أن تضع موسيقي في الخلفية أو لا تضع في كلتي الحالتين هي أفضل من الهمي المستفز .. و شكرا 2 weeks ago
    • #IStrategizer has a semi functional Influence Map System, check the latest IM screen shots from inside StarCraft game https://t.co/0QVVyUvv 2 weeks ago
    • ممكن حد من اللي فاهمين يكسب في ثواب و يفهمني يعني إيه: سحب الثقة في الحكومة؟ عشان أنا مش عارف؟ 3 weeks ago
    • الأحزاب بتستهبل يا فوزية 3 weeks ago

Changing Thread Path of Execution

Posted by MHesham on August 11, 2011

Every thread has a context structure, which is maintained inside the thread’s kernel object. This context structure reflects the state of the thread’s CPU registers when the thread was last executing.

Every 20 milliseconds or so (as returned by the second parameter of the GetSystemTimeAdjustment function), Windows looks at all the thread kernel objects currently in existence. Of these objects, only some are considered schedulable. Windows selects one of the schedulable thread kernel objects and loads the CPU’s registers with the values that were last saved in the thread’s context. This action is called a context switch.

The code primary thread (main function) below creates a new thread where its entry point is ThreadFunc1, and while it is running it suspends this secondary and changes its path of execution to the address of another function.

Code

DWORD WINAPI ThreadFunc1(PVOID pvParam)
{
    _tprintf_s(_T("I am ThreadFunc1\n"));
    while(1)
    {

    }
    _tprintf_s(_T("Exiting ThreadFunc1\n"));

    return 0;
}

DWORD WINAPI ThreadFunc2(PVOID pvParam)
{
    _tprintf_s(_T("I am ThreadFunc2\n"));
    while(1)
    {

    }
    _tprintf_s(_T("Exiting ThreadFunc2\n"));
   
    return 0;
}

int _tmain(int argc, TCHAR* argv[])
{
    // create a new thread with ThreadFunc1 as its entry-point
    HANDLE hThread = chBEGINTHREADEX(NULL, 0, ThreadFunc1, NULL, 0, NULL);

    if(!hThread)
        PrintLastError();

    // lets give the thread some time to do some work
    Sleep(2000);

    SuspendThread(hThread);

    CONTEXT cThread;
    // get control registers such as EIP (instruction pointer)
    cThread.ContextFlags = CONTEXT_CONTROL;
    GetThreadContext(hThread, &cThread);

    // change the target thread path of execution to ThreadFunc2
    cThread.Eip = (DWORD)ThreadFunc2;
    SetThreadContext(hThread, &cThread);

    ResumeThread(hThread);

    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(hThread);

    return 0;
}

Output

image

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.