Why is ExitProcess necessary under Win32 when you can use a RET?

后端 未结 1 1387
时光说笑
时光说笑 2020-11-30 13:09

I\'ve noticed that many assembly language examples built using straight Win32 calls (no C Runtime dependency) illustrate the use of an explicit call to ExitProcess() to end

相关标签:
1条回答
  • 2020-11-30 13:29

    If your main function is being called from the C runtime library, then exiting will result in a call to ExitProcess() and the process will exit.

    If your main function is being called directly by Windows, as may well be the case with assembly code, then exiting will only cause the thread to exit. The process will exit if and only if there are no other threads. That's a problem nowadays, because even if you didn't create any threads, Windows may have created one or more on your behalf.

    As far as I know this behaviour is not properly documented, but is described in Raymond Chen's blog post, "If you return from the main thread, does the process exit?".

    (I have also tested this myself on both Windows 7 and Windows 10 and confirmed that they behaved as Raymond describes.)

    Addendum: in recent versions of Windows 10, the process loader is itself multi-threaded, so there will always be additional threads present when the process first starts.

    0 讨论(0)
提交回复
热议问题