Setting Thread Context Error C++

0

So I'm trying to hijack a thread, and make it run a different function. Here's how I'm currently testing it:

CONTEXT ctxOldContext { CONTEXT_FULL };

void PX_API ThreadTest( )
{
    while ( true )
        Tools::Wait( 1 );
}

void PX_API ThreadHijack( )
{
    MessageBox( nullptr, L"Thread has been hijacked!", L"Thread has been hijacked!", MB_OK );
    SetThreadContext( GetCurrentThread( ), &ctxOldContext );
}

void PX_API OnLaunch( )
{
    DWORD dwThreadID;
    auto hThread = CreateThread( nullptr, 0, LPTHREAD_START_ROUTINE( ThreadTest ), nullptr, 0, &dwThreadID );

    SuspendThread( hThread );

    GetThreadContext( hThread, &ctxOldContext );
    auto ctxThread = ctxOldContext;
    ctxThread.Eip = DWORD( ThreadHijack );
    SetThreadContext( hThread, &ctxThread );
    ResumeThread( hThread );

    system( "pause" );
}

Seems simple enough to me, and it does work. I get the message box indicating that the thread has been hijacked. The only issue is after the message box, when the function returns I assume: I get an exception thrown with the following message:

Exception thrown at 0x00000000 in *.exe: 0xC0000005: Access violation executing location 0x00000000. occurred

I don't know what the hell is trying to read at that location, nor do I have a first clue on how to solve this. I just read up on threads on MSDN and used the information they gave me to write this.

As a side note, PX_API is defined as __cdecl, not sure if that is relevant or not.

Thanks in advance to anyone who replies.

c++
multithreading
asked on Stack Overflow Aug 6, 2018 by Cole W

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0