为什么 winmain 不设置错误级别?

发布于 2024-07-14 02:17:53 字数 890 浏览 6 评论 0原文

为什么这个程序可以正确显示消息框,但没有设置错误级别?

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 的可执行文件。 我在命令提示符下执行此操作:

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

如果在返回之前使用 exit(90); ,我会得到相同的结果。 它仍然显示0

我还尝试通过 CreateProcess 启动程序并使用 GetExitCodeProcess 获取结果,但它也向我返回 0 。 我进行了错误检查以确保一切都正确启动。

我最初在一个更复杂的程序中看到这个问题,所以我做了这个简单的程序来验证这个问题。 结果是相同的,两个具有 WinMain 的程序始终返回 0

我尝试了 x64、x86 以及 unicode 和 MBCS 编译选项。 全部给出 0 作为错误级别/状态代码。

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;
}

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

I get the same results if I use 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 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2024-07-21 02:17:53

如果您的程序是 Windows 应用程序,而不是控制台应用程序,则命令解释器不会等待它完成(在按“确定”之前,请查看命令窗口,您会发现它已准备好执行下一个命令) )。

如果是这种情况,将应用程序构建为控制台子系统应用程序可以解决问题。 如果您需要作为 Windows 应用程序运行,您可以尝试等待命令完成,看看是否有效(我没有尝试过,但这似乎是一个很好的方法):

start /wait a.exe
echo %ERRORLEVEL%

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).

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%
末蓝 2024-07-21 02:17:53

要使 %ERRORLEVEL% 正常工作,您必须启用命令扩展(我认为这是默认设置,天知道什么时候)。

尝试执行以下操作:

echo %CMDEXTVERSION%

查看是否启用了扩展。 当它们打开时,我得到“2”输出;当它们关闭时,我得到“%CMDEXTVERSION%”输出。

您还可以使用旧样式测试错误级别:

if errorlevel 1 echo errorlevel is 1 or more...

无论扩展如何,或者是否有人设置了名为“ERRORLEVEL”的环境变量,它都应该有效

For %ERRORLEVEL% to work you have to have command extensions enabled (which I think is the default since God know when).

Try doing:

echo %CMDEXTVERSION%

To see if extentions are enabled. I get '2' output when they are on and "%CMDEXTVERSION%" when they are off.

You can also test the errorlevel using the old style:

if errorlevel 1 echo errorlevel is 1 or more...

That should work regardless of extensions or if someone has set an enviroment variable with the name "ERRORLEVEL"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文