代码之家  ›  专栏  ›  技术社区  ›  vacuumhead

在基于Windows MFC对话框的应用程序中,标准输出不会显示在控制台上

  •  3
  • vacuumhead  · 技术社区  · 7 年前

    我想做的事

    我正在使用Visual Studio编写一个简单的基于对话框的程序。它按其应该的方式执行其主要功能。然后我添加了对从命令提示符运行应用程序的支持。当我从命令行提示符调用程序时,它仍然可以正常工作,但任何写入的消息 stdout stderr 只是没有显示。

    我试过的

    我尝试了多种不同的 _tprintf() _ftprintf(stdout, etc) 等等不管怎样,我在控制台上没有得到任何输出。

    它如何决定是否显示对话框

    Go(arg1, arg2) 作用当命令行没有足够的参数时,将使用对话框。

    我的代码

    这是一个非常精简的示例。剩下的大部分内容是由Visual Studio生成的,我刚刚添加了几行自己的代码。这个例子实际上不会编译,因为有未解析的外部引用。

    BOOL CCNCSplineApp::InitInstance()
    {
        // InitCommonControlsEx() is required on Windows XP if an application
        // manifest specifies use of ComCtl32.dll version 6 or later to enable
        // visual styles.  Otherwise, any window creation will fail.
        INITCOMMONCONTROLSEX InitCtrls;
        InitCtrls.dwSize = sizeof(InitCtrls);
        // Set this to include all the common control classes you want to use
        // in your application.
        InitCtrls.dwICC = ICC_WIN95_CLASSES;
        InitCommonControlsEx(&InitCtrls);
        std::time_t* p_clock = &time_now;
    
        CWinApp::InitInstance();
        AfxEnableControlContainer();
    
        // Create the shell manager, in case the dialog contains
        // any shell tree view or shell list view controls.
        CShellManager *pShellManager = new CShellManager;
    
        // Standard initialization
        SetRegistryKey(_T("MyCompany"));
    
        LPCTSTR cmdline = ::GetCommandLineW();
        int argc;
        LPWSTR* argv = ::CommandLineToArgvW(cmdline, &argc);
        if (2 < argc)
        {
            FILE*tgt = _tfopen(argv[2], _T("w"));
            if (0 == tgt)
            {
                _tprintf(L"Unable to open output file \"%s\"\n", argv[2]);
                exit(-1);
            }
            CString inputFileName = argv[1];
            Go(inputFileName, tgt);
            CString status = StatusText();
            if (status.GetLength() > 0)
            {
                _tprintf(_T("%s\n"), (LPCTSTR)status);
            }
            return FALSE;
        }
    
        MyDialog dlg;
        m_pMainWnd = &dlg;
        dlg.DoModal();
    
        // Delete the shell manager created above.
        if (pShellManager != NULL)
        {
            delete pShellManager;
        }
    
        // Since the dialog has been closed, return FALSE so that we exit the
        //  application, rather than start the application'time_doa message pump.
        return FALSE;
    }
    

    我需要在这段代码中更改什么才能获得各种 _tprintf() 调用以在控制台上显示文本?

    1 回复  |  直到 7 年前
        1
  •  5
  •   zett42    4 年前

    将此代码插入 InitInstance() printf() , std::cout 等应起作用:

    if( AttachConsole( ATTACH_PARENT_PROCESS ) )
    {
        freopen( "CONIN$",  "rb", stdin );   // reopen stdin handle as console window input
        freopen( "CONOUT$", "wb", stdout );  // reopen stdout handle as console window output
        freopen( "CONOUT$", "wb", stderr );  // reopen stderr handle as console window output
    }
    

    你也可以打电话 _setmode(_fileno(stdout), _O_U16TEXT) (与 stderr enable Unicode output to the console .