0

我有一段代码:

HANDLE  hProcess;
DWORD   dwExitCode;
DWORD   dwDelay;

while (GetExitCodeProcess(hProcess, &dwExitCode))
{
    if (STILL_ACTIVE == dwExitCode)
    {
        Sleep(dwDelay);
    }
    else
    {
        strMsg.Format("Process completed with exit code %d", dwExitCode);
        LogComment(strMsg);
        return;
    }
}
dwExitCode = GetLastError();
strMsg.Format("Error %d getting exit code.", dwExitCode);
LogComment(strMsg)

这行得通吗?我的问题基本上是,如果在调用函数以获取while循环表达式时出错,它会跳出该循环并让我捕获错误吗?还是我需要为此设置类似try...catch块的东西?

4

2 回答 2

2

如果错误是指异常,那么是的,如果您不将其放入try/catch块中,它将从那里反弹。

于 2013-09-03T17:30:08.213 回答
1

根据 MSDN 上的 GetExitCodeProcess

Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

如果此函数返回 0,即特别是 GetExitCodeProcess() 调用以某种方式失败,那么是的,您的循环将终止。

while (GetExitCodeProcess())

方法

while (GetExitCodeProcess() == true)

或 while (GetExitCodeProcess() != 0)

[这两个语句是等价的;零为假,非零为真]

这相当于:

for ( ; ; ) {
    auto gecpResult = GetExitCodeProcess(...);
    if (gecpResult == 0)
        break;
于 2013-09-03T17:50:41.003 回答