为什么不winmain设置错误级别? [英] why doesn't winmain set the errorlevel?
问题描述
为什么这个程序正确显示一个消息框,但是没有设置错误级别?
Why does this program correctly display a message box, but does not set the error level?
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;
}
我将上面的代码编译为名为a.exe的可执行文件。我在命令提示符下:
I compiled the code above to the name an executable called a.exe. The I did this in command prompt:
c:\> a.exe
(message box is displayed, I press ok)
c:\> echo %ERRORLEVEL%
0
如果我使用 exit(90);
就在返回之前。它仍然表示 0
。
I get the same results if I use exit(90);
right before the return. It still says 0
.
我也尝试通过 CreateProcess
启动程序并获取结果 GetExitCodeProcess
,但它也会返回 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.
我最初在一个更复杂的程序中看到这个问题,所以我做了这个简单的程序来验证问题。结果是一样的,两个程序都有 WinMain
总是返回 0
。
I originally saw this problem in a more complex program so I made this simple program to verify the problem. Results are the same, both programs that have WinMain
always return 0
.
我试过x64,x86和unicode和MBCS编译选项。所有给出 0
作为错误级别/状态代码。
I tried both x64, x86 and unicode and MBCS compiling options. All give 0
as an error level/status code.
推荐答案
如果您的程序是Windows应用程序,而不是控制台应用程序,命令解释程序不会等待它完成(在您按OK之前,查看命令窗口,您将看到它已准备好下一个命令)。
If your program is a Windows app, rather than a Console app, the command interpreter doesn't wait for it to complete (before you press OK, take a look at the command window and you'll see that it's ready for the next command).
如果是这样,应用程序作为控制台子系统应用程序将解决问题。如果你需要作为一个Windows应用程序运行,你可以尝试等待命令完成,看看是否有效(我没有尝试过,但它似乎是一个很好的方法):
If this is the case, building your application as Console subsystem app would solve the problem. If you need to run as a Windows app, you might try to wait for the command to complete and see if that works (I haven't tried this but it seems like a good approach):
start /wait a.exe
echo %ERRORLEVEL%
这篇关于为什么不winmain设置错误级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!