setthreadcontext problem

Hi
I am trying to change the threadcontext to make it execute a different function ...in that different function I am keeping a sleep after few instructions and then I am trying to restore the context back to execute the original function...
but it is crashing in "win64".
Let me know where I am going wrong.
Here is the code snippet:
=============================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#define NUMTHREADS 4
#define STACKTHREADS 3

DWORD WINAPI BckgThreadFunc(LPVOID arg)
{
    DWORD   sret=0;
    int i=0,j=0;
    
    for(i=1;i<NUMTHREADS;i++)
    {
        ResetEvent(hEvnt);
        sret = SuspendThread (hThread[i]);
        if ((DWORD) -1 == sret)
        {
            printf("BckgThreadFunc::SuspendThread1 failed for thread%d,threadid %d,ErrorCode %d\n",i,threadId[i],GetLastError());
        }
        else
        {
            CONTEXT tempctx,ctxThread;
            memset(&ctxThread,0,sizeof(CONTEXT));
            memset(&tempctx,0,sizeof(CONTEXT));
      
            ctxThread.ContextFlags = CONTEXT_ALL;
            if (!GetThreadContext(hThread[i], &ctxThread))
            {
              printf("BckgThreadFunc::GetThreadContext1 failed for thread%d,threadid %d,ErrorCode %d\n",i,threadId[i],GetLastError());
            }
            else
            {
                int status=0;
                tempctx=ctxThread;

                ctxThread.Rip=(DWORD64 )&PrintStackTrc;
                status=SetThreadContext(hThread[i],&ctxThread);
                if(status==0)
                   printf("BckgThreadFunc::SetThreadContext1 failed for thread%d,threadid %d,ErrorCode %d\n",i,threadId[i],GetLastError());
                   
                if ((DWORD)-1 == ResumeThread (hThread[i]))
                {
                    printf("BckgThreadFunc::ResumeThread1 failed for thread%d,threadid %d,ErrorCode %d\n",i,threadId[i],GetLastError());
                }
                else
                {
                    WaitForSingleObject(hEvnt,INFINITE);
                    sret = SuspendThread (hThread[i]);
                    if ((DWORD) -1 == sret)
                    {
                        printf("BckgThreadFunc::SuspendThread2 failed for thread%d,threadid %d,ErrorCode %d\n",i,threadId[i],GetLastError());
                    }
                    else
                    {
                        status=SetThreadContext(hThread[i],&tempctx);
                        if(status==0)
                            printf("BckgThreadFunc::SetThreadContext2 failed for thread%d,threadid %d,ErrorCode %d\n",i,threadId[i],GetLastError());
                    }
                    if ((DWORD)-1 == ResumeThread (hThread[i]))
                    {
                        printf("BckgThreadFunc::ResumeThread2 failed for thread%d,threadid %d,ErrorCode %d\n",i,threadId[i],GetLastError());
                    }
                    for(j = 0; j < count_st; j++)
                        printf("BckgThreadFunc::*** %d called from %016I64LX\n", j, callers[j]);
                }
            }
      }
  }
	return 0;
}

int  PrintStackTrc()
{
	//call to rtlcapturestacktrc using fn pointer
	count_st=(ProcAdd)(0, 62, callers, NULL);  
   
  if (! SetEvent(hEvnt) ) 
  {
      printf("SetEvent failed (%d)\n", GetLastError());
      return 1;
  }
	Sleep(120000);
	return 0;
}
Last edited on
Topic archived. No new replies allowed.