[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1022
  • Last Modified:

Visual studio 2005 typecast problem with legacy code

I'm trying to get some old visual studio 6 code to compile in visual studio 2005. I'm getting the following compile time error:

"2>c:\documents and settings\user\desktop\xlistctrl_demo\xlistctrl\xtrace.h(74) : error C2440: '=' : cannot convert from 'const wchar_t *' to 'LPTSTR' "

This is the offending function:
            if (!XTRACE_SHOW_FULLPATH)
                  cp = _tcsrchr(m_file, _T('\\'));
                  if (cp)

Please advise
2 Solutions
Kent OlsenData Warehouse Architect / DBACommented:

Hi sternocera,

I'm not sure if the failure is due to the 'const' modifier or the recast from wchar_t* to LPTSTR.  I would suspect that if the problem were with 'const' that it would have already been a problem so let's focus on the recast.

Compilers differ greatly in their behavior regarding pointer types.  Some flag errors when trying to set (or pass) a char* when the destination type is unsigned char*, others think it just fine.  The other extreme is the old C compiler on the DEC TRU64 systems which allowed you to set an integer variable to a pointer value (and vice versa) because they were both 32 bit.

In your case you've got a pointer to a wide char (16-bit) and a pointer to a char (8-bit).  Check to see if there is a compiler option to be more forgiving of this.  If not, a quick change to the source code to perform a recast should do fine.

  cp = (wchar_t*) _tcsrchr (m_file, _T('\\'));

Good Luck,
Either declare the variable as 'LPCTSTR' (which might not be applicable, since it is incremented) or use a 'const_cast()', i.e.

                  cp = const_cast<LPTSTR>(_tcsrchr(m_file, _T('\\')));

sternoceraAuthor Commented:
Thanks. That worked.

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now