Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LPCWSTR to LPWSTR confusion

Status
Not open for further replies.

Vachaun22

Programmer
Oct 7, 2003
171
US
I'm getting lost in all the MS specific type definitions.

I know that LPCWSTR = WCHAR const * and LPWSTR = WCHAR* but how would you go about converting LPCWSTR to a LPWSTR?

I don't understand what how to convert these types. I'm only understanding normal C++ type conversions, like int to double, or int to int*, etc.

Could someone shed some light on the subject for me? I've been googling this for some time, with no success.
 
Well here's the evil (i.e. dangerous) way:
Code:
LPWSTR lpwstr = const_cast<LPWSTR>( lpcwstr );
Depending on what you're actually going to do with it, this might be OK. If you're going to change the value of the string, you should make a copy of it, since changing a const string might work, or it might blow up.
 
The other way is to create a WCHAR array that is the same size as the LPCWSTR string, copy the contents and assign it to the lpwstr.

Be very careful using types with const in them: the prototypes saved for the libraries do not always match up with the prototypes used in the program. Sometimes they appear as const wchar*, other times they appear as wchar const* and the linker just moans like there is no tomorrow.

 
Ok, I guess the safe way would be to copy the string over, which is kind of what I was thinking after I had thought about it.

Of course I'm having issues with that now. Here is the code that I used, but it doesn't work (no big surprise to me being as inexperienced as I am).

Code:
BOOL CExtEditBase::GetCueBanner(__out_ecount_z(cchText) LPWSTR lpwText, __in int cchText)
{
	ASSERT(::IsWindow(m_hWnd));
	ASSERT(lpwText != NULL);
	if (lpwText != NULL)
	{
		lpwText[0]=L'\0';
	}
	WCHAR wText[cchText + 1];	// Create temp wchar
	memset( &wText, 0, sizeof(WCHAR * (cchText + 1)) );
	memcpy( &wText, m_lpcwText, wcslen( m_lpcwText ) + 1 );
	lpwText = &wText;
	return TRUE;
}

Me and any kind of C++ character arrays don't work well together for some reason. All my errors are with the 4 lines before the return statement (excluding memcpy doesn't throw any compilation errors).

Some more information, m_lpcwText is a LPCWSTR member variable. All I want to do is return that value back if this function is called.
 
Code:
// from the if statement
lpwText = new WCHAR[cchText + 1];
wcscpy (lpwText, m_lpcwText);
 
You might want to switch from raw C strings to STL strings. They have lots of functionality built in and grow automatically...
std::string is for char strings & std::wstring is for wchar_t strings.
 
I would switch th STL strings, but the functions I'm using are basically replacements for functions that exist in XP and new MS operating systems.

The implementation in the common controls version 6 dll file implements the SetCueBanner using a LPCWSTR as the input parameter, and GetCueBanner uses LPWSTR as the output parameter.

I'm just extending CEdit to have cuebanner support in pre XP operating systems, since of the most part it's only a handful of lines to code to implement it.

I appreciate all the help and insight everyone has given me. I got everything functioning as of now. Thanks again.
 
The c_str() member function of STL wstrings returns a const wchar_t* which is the same as LPCWSTR.
You can also use a vector<wchar_t> if you need a non-const string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top