OutputDebugString cannot convert `TCHAR*' to `char*'

I got this from EE and try to use it but got errors.

void DbgPrintf(LPTSTR fmt,...    )
{
    va_list marker;
    TCHAR szBuf[256];

    va_start(marker, fmt);
    wvsprintf(szBuf, fmt, marker);
    va_end(marker);

    OutputDebugString(szBuf);
    OutputDebugString(TEXT("\r\n"));
}

I used it like this:
int number = 1;
DbgPrintf("DEBUG: Number is %d", number);

But I got the following error message:

error: cannot convert `const char*' to `TCHAR*' for argument `1' to `void DbgPrintf(TCHAR*, ...)'

Thanks for the help.

Ming
LVL 1
mybluegrassAsked:
Who is Participating?
 
evilrixSenior Software Engineer (Avast)Commented:
Or: DbgPrintf(TEXT("DEBUG: Number is %d"), number);
0
 
evilrixSenior Software Engineer (Avast)Commented:
Try: DbgPrintf(_T("DEBUG: Number is %d"), number);
0
 
pgnatyukCommented:
or simply add 'L' before the const char* in the place you call this DbgPrintf function. Like
DbgPrintf(L"x = %d", x);
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
evilrixSenior Software Engineer (Avast)Commented:
>> or simply add 'L' before the const char*

You can do that but it will then fail to build in non Unicode builds. the _T or TEXT macro ensures L is used where necessary and not used where it's not necessary.
0
 
ambienceCommented:
You need to change the function a bit
void DbgPrintf(LPCTSTR fmt,...    )
i.e. LPTSTR -> LPCTSTR
 
If that does not solve the issue, then also apply the suggestions mentioned earlier.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> i.e. LPTSTR -> LPCTSTR
Although I agree passing as a const is better this is not the cause of the error. This is clear by looking at the error message.

cannot convert `TCHAR*' to `char*'
0
 
pgnatyukCommented:
and then here will be an error:
OutputDebugString(szBuf);

This is OutputDebugStringW in this project (it does support Unicode character set).
0
 
pgnatyukCommented:
Sorry, the error will here first
  wvsprintf(szBuf, fmt, marker);
0
 
pgnatyukCommented:
BTW, "Unicode character set" is the default now. For pure Windows app it is a nonsense to support Multibyte.
 
0
 
ambienceCommented:
@evilrix:
error: cannot convert `const char*' to `TCHAR*' for argument `1' to `void DbgPrintf(TCHAR*, ...)'
0
 
evilrixSenior Software Engineer (Avast)Commented:
Heh. Ok, it seems the error was presented twice with conflicting information. I missed that. Thanks for pointing it out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.