Frage

I am working on a c++/CLI application assignment in VS 2012. For testing purposes, I am trying to print some output to the console (to test methods as I build them), but there is no console window for this windows form application. Is there a way I can get the console window to show?

Or does anyone have a suggestion as to how I can display method output/results?

Thanks.

Edit - I figured out how to get the Console Window to work. Thanks David for the response.

War es hilfreich?

Lösung

As @David Points out, Debug::WriteLine is an excellent way to trace or send state to the output window.

System::Diagnostics::Debug::WriteLine(L" -- Object State or Tracing");

However, if you are still wanting a console window for your windows application, consider the following:

// Beginning of Application
#if _DEBUG
    if (::AllocConsole())   // <-- http://msdn.microsoft.com/en-us/library/windows/desktop/ms681952(v=vs.85).aspx
        if (!::AttachConsole(ATTACH_PARENT_PROCESS))  // -1 == ATTACH_PARENT_PROCESS or Process ID
            System::Windows::MessageBox::Show(L"Unable to attach console window", L"Error", System::Windows::MessageBoxButton::OK, System::Windows::MessageBoxImage::Exclamation);
#endif

// Application End
#if _DEBUG
    ::FreeConsole();       // <-- http://msdn.microsoft.com/en-us/library/windows/desktop/ms683150(v=vs.85).aspx
#endif

Note that this will only be seen when built using the debug configuration.

Hope this helps.

Andere Tipps

I'd recommend you use Debug::WriteLine. As you test things in the debugger, it will write to the output window of Visual Studio.

For VS 2022 C++/CLI applications:

Project->Properties->Linker->System --> Subsystem = Not Set

The console window and the form window will open together.

Console::WriteLine("Hello C++/CLI"); or 
std::cout << "Hello C++/CLI" << endl;

both will work for both managed code and native code.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top