Windows application that optionally writes to a console in C++?

前端 未结 1 384
情歌与酒
情歌与酒 2021-01-01 03:11

I\'d like to have a windows application with the following behaviour:
1. if it is started from an existing command line window (cmd.exe) then it writes its stdout to tha

1条回答
  •  被撕碎了的回忆
    2021-01-01 03:13

    Found out the answer here: http://dslweb.nwnexus.com/~ast/dload/guicon.htm

    DWORD ret = AttachConsole(-1);
    if (ret != 0) {
       HANDLE lStdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
       int hConHandle = _open_osfhandle((intptr_t)lStdHandle, 0);
       FILE* fp = _fdopen( hConHandle, "w" );
       *stdout = *fp;
    }
    

    And as for making cmd.exe wait, that doesn't seem possible: http://blogs.msdn.com/b/oldnewthing/archive/2009/01/01/9259142.aspx

    0 讨论(0)
提交回复
热议问题