AllocConsole() not displaying cout

前端 未结 3 732
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 23:07

I have a DLL where I use AllocConsole() and cout to display data for debugging purposes.
It used to work fine but since I updated my compiler (Visual Studio 2012) to the

3条回答
  •  时光说笑
    2020-12-02 23:23

    This works using vs2015 with the line std::cout.clear()

    if (!AllocConsole())
        MessageBox(NULL, L"The console window was not created", NULL, MB_ICONEXCLAMATION);
    
    FILE* fp;
    
    freopen_s(&fp, "CONOUT$", "w", stdout);
    
    printf("Hello console on\n");
    
    std::cout.clear();
    
    std::cout << "Cout line one." << std::endl;
    
    cout << "Cout line two." << std::endl;
    
    MessageBox(NULL, (L"Pause to see console output."), (L"Pause Here"), MB_OK | MB_SYSTEMMODAL | MB_ICONEXCLAMATION);
    
    fclose(fp);
    
    if (!FreeConsole())
        MessageBox(NULL, L"Failed to free the console!", NULL, MB_ICONEXCLAMATION);
    

提交回复
热议问题