0

I am trying to load the name of an image using its VM address by calling GetModuleFileName() which seems to return the value correctly into a TCHAR[] array. I am able to display the data correctly using MessageBox() but cout << seems to display some funky hexadecimal number.

TCHAR buf[MAX_PATH];
HMODULE hProc = LoadLibrary(TEXT("kernel32.dll"));
GetModuleFileName(hProc, buf, MAX_PATH);
cout << buf; //Produces the odd number
MessageBox(NULL, buf, NULL, MB_OK); //Produces correct filepath
FreeLibrary(hProc);

Am I supposed to set a flag for cout so it knows to print it correctly? Thank you!

3
  • 2
    In modern Windows programming (as of after the year 2000), don't use TCHAR. Use e.g. wchar_t. Then you know better what you're doing and what you're dealing with. Commented Dec 3, 2011 at 21:42
  • Out of curiosity, what is the difference? Compatibility? Commented Dec 3, 2011 at 21:45
  • TCHAR is a macro (pure text substitution) that is defined as either char or wchar_t, depending on whether the symbol UNICODE was defined when you included the relevant header, usually <windows.h>. Similarly, TEXT adds L prefix or not. This means that code that works with UNICODE defined, may and probably will not compile when UNICODE is not defined. The T scheme originally supported Windows 9x, but as of 2011 it is just a problematic extra layer of obfuscation. For example, it prevented you from knowing that you dealt with wchar_t. Cheers & hth., Commented Dec 3, 2011 at 22:52

2 Answers 2

5

Probably you need to use wcout, because your TCHAR might be unicodish. Or convert it.

Sign up to request clarification or add additional context in comments.

4 Comments

Had a feeling it was something simple like that. Never knew about wcout until now, thanks!
Sorry, I haven't seen your answer, when posting mine.. and I cannot delete it, because I've already deleted 5 my posts today....
Is there a tcout or something in Windows, that'd be cout for ANSI and wcout for Unicode?
There should be no TCHAR instead :) Actually, I don't know, I don't use windows for eons now. If there's nothing like that, you can #define it, perhaps.
2

Maybe you will have better luck with

std::wcout << buf;

Comments

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.