0

In visual studio 2015 environment, I just made simple Win32 console application program project to study MFC. (Also, I check on adding common header file of MFC in project Wizard process)

And Here is main part of this project..

#include "stdafx.h"
#include "Practice02.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CWinApp theApp;

using namespace std;

int main()
{
    int nRetCode = 0;

    HMODULE hModule = ::GetModuleHandle(nullptr);

    if (hModule != nullptr)
    {
        if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0))
        {
            wprintf(L"error: sample\n");
            nRetCode = 1;
        }
        else
        {
            CString temp(L"Hello");
            cout << temp << endl;
        }
    }
    else
    {
        wprintf(L"Fatal Error: GetModuleHandle failure\n");
        nRetCode = 1;
    }

    return nRetCode;
}

My intention is to make simple program which prints CString object containing "hello" value on cmd screen.

However, after start this project, I only see the address value of this object. (EX. 0039841 or 003913E1 etc...)

Where should I modify this code to print real value of CString object?

4
  • 1
    Your CStrings are wide as you're compiling with _UNICODE defined, print to wcout instead. Commented Mar 21, 2016 at 7:23
  • Yes, this project use UNICODE setting. And, I change a code from "cout << temp << endl;" to "wcout << temp << endl;". However, outcome still is address value. ;( Commented Mar 21, 2016 at 7:28
  • I can't make any sense of that. For a console program, you can create a console project. Add #include <atlstr.h> and #include "windows.h" then you can use CString and WinAPI functions. You won't be able to use other MFC functions. Remove CWinApp theApp;, remove AfxWinInit ... Commented Mar 21, 2016 at 7:29
  • 1
    Just to explain what is going on: std::ostream has an overload for several pointer types. By default, it uses the void const* overload which then prints the hex-formatted address. Now, try to find out to which type your CString converts, e.g. by stepping through it with a debugger. Commented Mar 21, 2016 at 7:34

1 Answer 1

3

Use following :

std::wcout << temp.GetString();
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I'm looking for !! Thank you, :) But, can I ask you how this change make object to refer real value?
@구마왕 Internally CString has either char* or wchar*. GetString returns const wchar_t* and that works with wcout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.