Click here to Skip to main content
15,887,214 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: LPCWSTR Problem.? [updated] Pin
CPallini2-Jan-11 5:12
mveCPallini2-Jan-11 5:12 
GeneralRe: LPCWSTR Problem.? Pin
Sauro Viti2-Jan-11 5:39
professionalSauro Viti2-Jan-11 5:39 
GeneralRe: LPCWSTR Problem.? Pin
CPallini2-Jan-11 5:59
mveCPallini2-Jan-11 5:59 
GeneralRe: LPCWSTR Problem.? Pin
Sauro Viti2-Jan-11 6:03
professionalSauro Viti2-Jan-11 6:03 
GeneralRe: LPCWSTR Problem.? Pin
CPallini2-Jan-11 6:33
mveCPallini2-Jan-11 6:33 
GeneralRe: LPCWSTR Problem.? Pin
goldenrose94-Jan-11 0:11
goldenrose94-Jan-11 0:11 
GeneralRe: LPCWSTR Problem.? Pin
CPallini4-Jan-11 0:27
mveCPallini4-Jan-11 0:27 
AnswerRe: LPCWSTR Problem.? [modified] Pin
Stefan_Lang10-Jan-11 0:21
Stefan_Lang10-Jan-11 0:21 
The problem you try to solve is passing a string to a caller, where the caller has no knowledge of the length of that string, and therefore cannot easily provide a buffer variable to store that string into.

The posted solution does not work, because the compiler will create a local copy of the string literal you used for initialization, and the address to that copy will be invalidated upon return.

If you used const wchar_t* instead of wchar_t*, and the appropriate const string types, it might have worked. But if you want a non-const string, then there are really only two possible ways to deal with this:

1. use a secondary function that enables the caller to inquire about the expected length of the string, and then pass a buffer of sufficient size to DisplayInfo.
const wchar_t* HELLO = _T("Yahoooooo");
int DisplayInfoSize()
{
    return wcslen(HELLO);
}
LPCWSTR __stdcall DisplayInfo(wchar_t* hello)
{
    return wcscpy(hello, HELLO);
}

2. Have DisplayInfo allocate memory on the heap to story that string.
const wchar_t* HELLO = _T("Yahoooooo");
LPCWSTR __stdcall DisplayInfo()
{
    wchar_t* hello = new wchar_t*[wcslen(HELLO)+1];
    return wcscpy(hello, HELLO);
}


Solution 1 is awkward, but it makes sure the caller can release the allocated memory once the string is no longer needed. Solution 2 might create a memory leak, if the caller has no way to properly release the string being passed to it (which may very well be the case if it's called from VB), but it would not require a change of the interface.




P.S.:

Just realized that the return type on your DisplayInfo function is a const string! This means that the compiler should indeed just return a pointer to the literal as others already pointed out. I'm sorry, I got confused by the cast in your return statement (which is non-const string!)

The suggestions above might still work if none of the other responses are helping.

modified on Monday, January 10, 2011 6:34 AM

QuestionAdding a reference to MFC Project.... Pin
AmbiguousName1-Jan-11 8:09
AmbiguousName1-Jan-11 8:09 
QuestionHow can I initialize an CListBox& null reference ? Pin
mesajflaviu1-Jan-11 4:57
mesajflaviu1-Jan-11 4:57 
AnswerRe: How can I initialize an CListBox& null reference ? Pin
BicycleTheif1-Jan-11 5:28
BicycleTheif1-Jan-11 5:28 
GeneralRe: How can I initialize an CListBox& null reference ? Pin
mesajflaviu1-Jan-11 5:32
mesajflaviu1-Jan-11 5:32 
GeneralRe: How can I initialize an CListBox& null reference ? Pin
«_Superman_»1-Jan-11 7:41
professional«_Superman_»1-Jan-11 7:41 
AnswerRe: How can I initialize an CListBox& null reference ? Pin
CPallini1-Jan-11 7:54
mveCPallini1-Jan-11 7:54 
AnswerRe: How can I initialize an CListBox& null reference ? Pin
bleedingfingers2-Jan-11 20:08
bleedingfingers2-Jan-11 20:08 
QuestionHow can send message to an notepad ? [modified] Pin
mesajflaviu1-Jan-11 2:53
mesajflaviu1-Jan-11 2:53 
AnswerRe: How can send message to an notepad ? Pin
Espen Harlinn1-Jan-11 4:43
professionalEspen Harlinn1-Jan-11 4:43 
AnswerRe: How can send message to an notepad ? Pin
«_Superman_»1-Jan-11 7:54
professional«_Superman_»1-Jan-11 7:54 
GeneralRe: How can send message to an notepad ? Pin
normanS1-Jan-11 14:32
normanS1-Jan-11 14:32 
AnswerRe: How can send message to an notepad ? Pin
Luc Pattyn1-Jan-11 16:15
sitebuilderLuc Pattyn1-Jan-11 16:15 
Question891011 - nested groups in a property grid Pin
ilostmyid231-Dec-10 22:48
professionalilostmyid231-Dec-10 22:48 
QuestionWhy is index in document gone? [modified] Pin
followait31-Dec-10 16:59
followait31-Dec-10 16:59 
AnswerRe: Why is index in document gone? Pin
Richard MacCutchan1-Jan-11 0:41
mveRichard MacCutchan1-Jan-11 0:41 
GeneralRe: Why is index in document gone? [modified] Pin
followait1-Jan-11 5:47
followait1-Jan-11 5:47 
QuestionWhy the first parameter of CreateEvent () method is NULL Pin
pandit8431-Dec-10 0:02
pandit8431-Dec-10 0:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.