I include following files in my .cpp program:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
Nevertheless, when I write
LPCTSTR pMsg;
DWORD msgLen;
...
msgLen = _tcslen(pMsg);
The compiler prompts the following error:
C2664: 'size_t strlen(const char *)' : cannot convert argument 1 from 'LPCTSTR' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Of course I can easily fix the problem by casting to LPCSTR or using wcslen. But that's not right, shouldn't compiler decide all that stuff itself, depending on macro UNICODE? It's certainly defined, because when i write #define UNICODE in the beginning of the file, compiler prompts the warning, saying that it's already defined. What is the problem then? Why it chooses strlen instead of wcslen?
Here is the actual code:
BOOL PrintString(HANDLE hOut, ...)
{
DWORD msgLen, count;
LPCTSTR pMsg;
va_list pMsgList;
va_start(pMsgList, hOut);
while((pMsg = va_arg(pMsgList, LPCTSTR)) != NULL)
{
msgLen = _tcslen(pMsg);
if(!WriteConsole(hOut, pMsg, msgLen, &count, NULL) && !WriteFile(hOut, pMsg, msgLen*sizeof(TCHAR), &count, NULL))
{
va_end(pMsgList);
return FALSE;
}
}
va_end(pMsgList);
return TRUE;
}
I tried both, #define _UNICODE and #define _UNICODE but neither didn't help. Moreover, #define _UNICODE prompts the warning that it's already defined.
According to MSDN, what _tcslen expands to is controlled by the macro _UNICODE. LPCTSTR, on the other hand, is controlled by UNICODE. Make sure you have these two macros defined consistently, and before any header which might use them is included.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With