why doesn't winmain set the errorlevel?

Posted by Brian R. Bondy on Stack Overflow See other posts from Stack Overflow or by Brian R. Bondy
Published on 2009-02-26T19:37:03Z Indexed on 2010/04/30 18:17 UTC
Read the original article Hit count: 325

Filed under:
|
|
|
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
  MessageBox(NULL, _T("This should return 90 no?"), _T("OK"), MB_OK);
  return 90;
}

Why does the above program correctly display the message box, but does not set the error level?

I compile this program to the name a.exe. Then from command prompt I type:

c:\> a.exe 
 (message box is displayed, I press ok)
c:\> echo %ERRORLEVEL% 
  0

I get the same results if I do exit(90); right before the return. It still says 0.

I also tried to start the program via CreateProcess and obtain the result with GetExitCodeProcess but it also returns 0 to me. I did error checking to ensure it was all started correctly.

I originally seen this problem in a more complex program. But made this simple program to verify the problem. Results are the same, both programs that have WinMain return 0 always.

I tried both x64, x86 and unicode and MBCS compiling options. All give 0 as an error level/status code.

© Stack Overflow or respective owner

Related posts about winmain

Related posts about visual-c++