Wednesday, August 17, 2022

[FIXED] How do I show the console in a c++/CLI application?

Issue

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.


Solution

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.



Answered By - Jeff
Answer Checked By - Mildred Charles (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.