WinAPI AttachConsole?

时间:2017-07-08 01:14:09

标签: c++ winapi

我有以下代码,我不确定它应该是== TRUE还是!= FALSE

这是现在的代码:

void AttachConsole() {
    bool has_console = ::AttachConsole(ATTACH_PARENT_PROCESS) == TRUE;

    if (!has_console) {
        // We weren't launched from a console, so just return.
        // We could alloc our own console, but meh:
        // has_console = AllocConsole() == TRUE;
        has_console_attached_ = false;

        return;
    }

    has_console_attached_ = true;
}

我认为它应该是!= FALSE,但我不确定?

1 个答案:

答案 0 :(得分:3)

返回值仅记录为0表示失败或非零表示成功。

是的,你可以使用!= FALSE或者你可以使用:

bool has_console = ::AttachConsole(ATTACH_PARENT_PROCESS);

从BOOL(实际为整数)到bool的转换会将0转换为false,将其他任何转换为​​true - 正是您想要的。