Saturday, March 7, 2009

How ExitThread works!!! [WinCE]


This function ends a thread.This API takes exit code for the calling thread. To retrieve a thread's exit code, use the GetExitCodeThread function.
The steps are:
  1. It will release the owner process by using API GetCurrentThreadId which gives it current thread and check for the current process which own the thread and if it's the same application which own the thread then release all CS.
  2. Check for thread type:
    1. If it is the primary thread then
      1. Kill all other thread.
      2. Detach all DLL from the process.
      3. Call Dllmain function of dll with thread detach ioctl.
      4. Call core dll to detach the thread.
      5. Close all handle
    2. Else
      1. Get the thread attach status and call dllmain of dll with thread detach ioctl.
  3. Now it will call NKTerminateThread.
Note: If the primary thread calls ExitThread, the application will exit.