Jump to content
  • Advertisement
Sign in to follow this  
Toolmaker

MFC: CString to LPCTSTR conversion

This topic is 3938 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having serious trouble with the whole CString => LPCTSTR conversion. I'm aware of the SingleByte / MultiByte stuff. The application I'm working on is compiled with the MultiByte Character set, and I need to convert a specific CString to an LPCTSTR(I need to set the m_pszHelpFilePath on a CWnd, and this happens to be a system owned string). Assigning the CString won't work; The target string is system owned, and I can't seem to find a Detach() method on CString. Therefor, if the application ends, it crashes with dual pointer deletion. Any hints on how I can convert? I tried some searching and it only confused me more. Any good reads on the whole SingleByte/MultiByte/Unicode thing in MFC are welcome aswell. Toolmaker

Share this post


Link to post
Share on other sites
Advertisement
From MSDN:

If you assign a value to m_pszHelpFilePath, it must be dynamically allocated on the heap. The CWinApp destructor calls free( ) with this pointer. You many want to use the _tcsdup( ) run-time library function to do the allocating. Also, free the memory associated with the current pointer before assigning a new value. For example:

//First free the string allocated by MFC at CWinApp startup.
//The string is allocated before InitInstance is called.
free((void*)m_pszHelpFilePath);
//Change the name of the .HLP file.
//The CWinApp destructor will free the memory.
m_pszHelpFilePath=_tcsdup(_T("d:\\somedir\\myhelp.hlp"));

Share this post


Link to post
Share on other sites
Quote:
Original post by zoki
From MSDN:

If you assign a value to m_pszHelpFilePath, it must be dynamically allocated on the heap. The CWinApp destructor calls free( ) with this pointer. You many want to use the _tcsdup( ) run-time library function to do the allocating. Also, free the memory associated with the current pointer before assigning a new value. For example:

//First free the string allocated by MFC at CWinApp startup.
//The string is allocated before InitInstance is called.
free((void*)m_pszHelpFilePath);
//Change the name of the .HLP file.
//The CWinApp destructor will free the memory.
m_pszHelpFilePath=_tcsdup(_T("d:\\somedir\\myhelp.hlp"));


I already knew that part, however, I misread the use of _tcsdup(). I interpreted it was ToUpper for some stupid reason.

Thank you for pointing out the obvious, rate++

Toolmaker

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!