Sign in to follow this  
Decept

std::wstring for RegisterClassEx() class name

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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