开发者

print TCHAR[] on console

I'm quite sure that it is a stupid issue but it drives me crazy..

how could i print on the console a TCHAR array?

DWORD error = WSAGetLastError();
TCHAR errmsg[512];
int ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, 开发者_StackOverflow社区error, 0, errmsg, 511, NULL);

i need to print errmsg...


It depends on what TCHAR is. If you compile with Unicode enabled, 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 an ordinary char and you can use the ordinary std::cout:

std::cout << "Error: " << errmsg << '\n';


A Google search revealed this discussion which essentially recommends tprintf.


#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);


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 really explain why it works and don't have time to search it now. Later!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜