2

The szTip field is 128 characters long, and unicode. It is of type TCHAR, which is typedef'd as WCHAR. So i have no clue why the following code snippet will not compile.

nid.szTip = _T("ToolTip");

The compile error is

error C2440: '=' : cannot convert from 'const wchar_t [8]' to 'WCHAR [128]'

Any advice?

Steve Barna
  • 1,378
  • 3
  • 13
  • 23

1 Answers1

5

Your code would work if you were assigning to a TCHAR*. However, szTip is not a TCHAR*, it is declared as TCHAR szTip[64].

So you need to copy the contents of the string to the buffer. Like this:

_tcscpy(nid.szTip, _T("ToolTip"));

Do you really need to support both ANSI and Unicode builds? If not then stop using TCHAR and switch to Unicode. Then you could write a more readable version.

wcscpy(nid.szTip, L"ToolTip");
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • There's nothing inherently less-readable about the tchar version.... it's just each person and what they're more familiar with, so why not use it? – Mahmoud Al-Qudsi Oct 01 '11 at 02:01
  • FWIW, I've seen plenty of supposedly TCHAR-based code that's actually UNICODE-only: code that calls UNICODE-only APIs (eg SysAllocString or other COM-based APIs) with TCHARs, which would fail to compile outright if it was ever compiled as ANSI. So the code is then potentially misleading: using TCHARs suggests that "this code works as either ANSI or UNICODE" (because that's why TCHAR exists in the first place). Of course if you don't care what other people think of your code, this isn't an issue. – BrendanMcK Oct 01 '11 at 03:38