llk
llk

Reputation: 2561

MessageBox producing different output than Cout C++

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!

Upvotes: 0

Views: 150

Answers (2)

Roman Byshko
Roman Byshko

Reputation: 9032

Maybe you will have better luck with

std::wcout << buf;

Upvotes: 2

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143249

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

Upvotes: 5

Related Questions