Sign in to follow this  

MFC: CString to LPCTSTR conversion

This topic is 3630 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
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

This topic is 3630 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this