std::wstring for RegisterClassEx() class name

Started by
6 comments, last by Decept 15 years, 5 months ago
I'm having a problem with RegisterClassEx (or rather RegisterClassExW). I'm trying to set the lpszClassName member of the WNDCLASSEXW class with an std::wstring, like so:

WNDCLASSEXW winclass;
winclass.lpszClassName = classNameStr.c_str();
RegisterClassExW( &winclass );

classNameStr is an std::wstring that is valid for all of app's life time. When I step through the code I see that c_str() returns the correct string, but when I check lpszClassName it contains only garbage. The RegisterClassExW then fails. If I set lpszClassName to L"1234567" everything works fine. I know wstring::c_str() returns a temporary string but I shouldn't it work even so?
Advertisement
Quote:Original post by Decept
I'm having a problem with RegisterClassEx (or rather RegisterClassExW). I'm trying to set the lpszClassName member of the WNDCLASSEXW class with an std::wstring, like so:

*** Source Snippet Removed ***

classNameStr is an std::wstring that is valid for all of app's life time. When I step through the code I see that c_str() returns the correct string, but when I check lpszClassName it contains only garbage. The RegisterClassExW then fails.

If I set lpszClassName to L"1234567" everything works fine.

I know wstring::c_str() returns a temporary string but I shouldn't it work even so?
wstring::c_str()'s return value is valid while the wstring is still in scope. I can't see any reason why this would fail - try doing a clean rebuild, and maybe even stepping into the c_str() function.
Can you create a small test case that shows this behaviour?
I tried making a small test app and it worked, but I soon discovered a small difference that makes all the eh...difference.

this works:
std::wstring classNameStr = L"Hello";WNDCLASSEXW winclass;winclass.lpszClassName = classNameStr.c_str();RegisterClassExW( &winclass );


this doesn't:
std::wstring someFn(){  return L"Hello";}WNDCLASSEXW winclass;winclass.lpszClassName = someFn().c_str();RegisterClassExW( &winclass );


The second method is the one I need working. But it doesn't.
An easy fix that works:

std::wstring someFn(){  return L"Hello";}std::wstring classNameStr = someFn();WNDCLASSEXW winclass;winclass.lpszClassName = classNameStr.c_str();RegisterClassExW( &winclass );


But I still don't understand why.
Quote:Original post by Decept
An easy fix that works:

*** Source Snippet Removed ***

But I still don't understand why.

Quote:Original post by Evil Steve
wstring::c_str()'s return value is valid while the wstring is still in scope
The string goes out of scope as soon as the call to c_str() completes, because it's a temporary object. That means that the pointer you stored is no longer valid.
Ah yes, I must be too tired, thank you!
So was this what was causing the problem? Or is the problem still there, and this was just a small side-track?
This was the actual problem. RegisterClassExW failed because the lpszClassName pointed to garbage. I have used this code for a long time with no problem, so I couldn't really understand why it failed. The thing is that I just recently started using std::wstring and that's when the problem started. Previously my getClassName() function returned a const wchar_t pointer to a wchar_t array.

This topic is closed to new replies.

Advertisement