简体   繁体   中英

WinAPI AttachConsole?

I have the following code and I'm not sure if it should be == TRUE or != FALSE .

This is the code now:

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

I think it should be != FALSE but I'm not sure?

The return value is documented only as being 0 for failure or non-zero for success.

So yes, you could use != FALSE or you could just use:

bool has_console = ::AttachConsole(ATTACH_PARENT_PROCESS);

The conversion from BOOL (really an integer) to bool will convert 0 to false , and anything else to true --exactly what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM