poncho
poncho

Reputation: 1150

Printing variable values to console visual c++ windows store Modern UI app

I'm just starting learning C++/XAML windows store app development but for the life of me I can't find a nice way to print variable values to the "Output" window in VS2012.

Debug.WriteLine() doesn't seem to exist for Windows Store apps and I can't find a way to print other than OutputDebugString() which I can't use to print variable values (without some heavy formatting).

Is there just an easy way to print the example line:

mouse position X: 12

for example, where 12 is an integer that comes from MouseDelta.

Thanks for your time,

Poncho

Upvotes: 5

Views: 4978

Answers (3)

tronman
tronman

Reputation: 10125

This solution uses a wrapper around OutputDebugString:

void WinLog(const wchar_t *text, int n)
{
    wchar_t buf[1024];
    _snwprintf_s(buf, 1024, _TRUNCATE, L"%s %d\n", text, n);
    OutputDebugString(buf);
}

which can be called as follows:

WinLog(L"The Answer is", 42);

Upvotes: 1

Rafael
Rafael

Reputation: 2817

Here is a nice alternative: http://seaplusplus.com/2012/06/25/printf-debugging-in-metro-style-apps/, basically it allocates a console for your Windows Store App, obviously this will fail certification but given that this may be just for debug purposes only, it will fine. I'm copying the relevant code here:

// Include Windows.h for WINBASEAPI and WINAPI:
#include <Windows.h>

// Declare AllocConsole ourselves, since Windows.h omits it:
extern "C" WINBASEAPI int WINAPI AllocConsole();

auto main(Platform::Array<Platform::String^>^) -> int
{
    AllocConsole();

    std::wcout << L"Hello there!" << std::endl;
    std::getchar();

    return EXIT_SUCCESS;
}

However if you want to see such output inside your app, then you may want to use Console Class for Modern UI Apps which implements part of the .NET System.Console and can be safely used inside Windows Store apps.

Upvotes: 1

Andy Rich
Andy Rich

Reputation: 1962

Not really, no. You could write a little function that formatted like printf and passed along the resulting string to OutputDebugString() but there's nothing more straightforward available.

I guess you could use ToString(), Platform::String::operator+, and Platform::String::Data() to accomplish this; though it's a little ugly:

OutputDebugString( ("mouse position X:" + MouseDelta.X.ToString())->Data() );

Upvotes: 1

Related Questions