Print TCHAR [] on the console

I'm absolutely sure this is a dumb problem, but it drives me crazy ..

How can I print a TCHAR array on the console?

DWORD error = WSAGetLastError(); TCHAR errmsg[512]; int ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, error, 0, errmsg, 511, NULL); 

I need to print errmsg ...

+6
c ++ visual-c ++
source share
4 answers

It depends on what TCHAR . If you compile Unicode, TCHAR is defined as wchar_t . Then you can use std::wcout , for example:

 std::wcout << L"Error: " << errmsg << '\n'; 

If Unicode is not enabled, TCHAR is a regular char , and you can use regular std::cout :

 std::cout << "Error: " << errmsg << '\n'; 
+10
source share

A Google search revealed this discussion , which essentially recommends tprintf .

+6
source share
 #include <tchar.h> /* _tprintf */ DWORD dwError; BOOL fOk; HLOCAL hlocal = NULL; // Buffer that gets the error message string fOk = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, dwError, 0, (PTSTR) &hlocal, 0, NULL); if (! fOk) hlocal = TEXT("Fehler FormatMessage"); _tprintf( TEXT("%d\t%s\n"), dwError, hlocal ); if (fOk) LocalFree(hlocal); 
+2
source share

I really don't know why, but this code worked for me:

 TCHAR NPath[MAX_PATH]; DWORD a = GetCurrentDirectory(MAX_PATH, NPath); string b = ""; for(int i=0; i<a;i++){ b+=NPath[i]; } cout << b; system("pause"); 

Sorry, but I can’t explain why it works, and I don’t have time to find it. Later!

-2
source share

All Articles