i've been experimenting with wrapping some basic win32 functionality in classes while i work with d3d, and am stumped for why the CreateWindowEx function fails saying a class doesn't exist AFTER i create a valid class with RegisterClassEx, no errors to speak of :\ . I get the feeling i'm missing some silly little thing, but i can't find it. Here is some of the code:
I have a class that extends WNDCLASSEX like this, so that it has a normal std::string for a class name and a simplified constructor:
#ifndef WINDOWCLASS_H
#define WINDOWCLASS_H
#include <Windows.h>
#include <string>
#include "WindowAbstract.h"
using namespace std;
class WindowClass : public WNDCLASSEX
{
public:
WindowClass(string className, WindowAbstract * window);
~WindowClass();
bool Register();
string ClassName() {return m_className;}
friend class WindowAbstract;
private:
string m_className;
};
#endif
And here is the constructor for the class:
WindowClass::WindowClass(string className, WindowAbstract * window)
{
cbSize = sizeof(WNDCLASSEX);
style = 0;
lpfnWndProc = window->WndProc;
cbClsExtra = 0;
cbWndExtra = 0;
hInstance = hInstance;
hIcon = LoadIcon(NULL, IDI_APPLICATION);
hCursor = LoadCursor(NULL, IDC_ARROW);
hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
lpszMenuName = NULL;
lpszClassName = className.c_str();
hIconSm = LoadIcon(NULL, IDI_APPLICATION);
m_className = className;
}
here is the register function to be called after it's constructed:
bool WindowClass::Register()
{
if(RegisterClassEx(this) == 0)
return false;
return true;
}
The WindowAbstract class contains the window procedure and is created first, to pass the pointer to it's function to the WindowClass object.
#ifndef WINDOWABSTRACT_H
#define WINDOWABSTRACT_H
#include <Windows.h>
#include <string>
using namespace std;
class WindowAbstract
{
public:
WindowAbstract();
~WindowAbstract();
bool Create(string windowTitle, string className, DWORD styles, DWORD extendedStyles, int top, int left, int bot, int right, HWND parent, HMENU id);
void Show();
friend class WindowClass;
private:
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
HWND m_hwnd;
};
#endif
And here is the create function:
bool WindowAbstract::Create(string windowTitle, string className, DWORD styles, DWORD extendedStyles, int top, int left, int bot, int right, HWND parent, HMENU id)
{
m_hwnd = CreateWindowEx(extendedStyles, className.c_str() , windowTitle.c_str(), styles, top, left, bot, right, parent, id, GetModuleHandle(NULL), NULL);
if(!m_hwnd)
return false;
return true;
}
so after seeing all that, here is the actual winmain where i test it out:
#include "WindowAbstract.h"
#include "WindowClass.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
DWORD error;
bool result;
char buffer[100];
WindowAbstract * window = new WindowAbstract();
WindowClass * myClass = new WindowClass("myClass", window);
result = myClass->Register();
if(!result)
{
error = GetLastError();
sprintf_s(buffer, "error: %i", error);
MessageBox(NULL, buffer, "Registration Failed!", MB_OK);
}
result = window->Create("my Window", myClass->ClassName(), WS_OVERLAPPEDWINDOW, WS_EX_CLIENTEDGE, 20, 20, 200, 200, NULL, NULL);
if(!result)
{
error = GetLastError();
sprintf_s(buffer, "error: %i", error);
MessageBox(NULL, buffer, "Window Creation Failed!", MB_OK);
}
window->Show();
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(window)
{
delete window;
window = 0;
}
if(myClass)
{
delete myClass;
myClass = 0;
}
return msg.wParam;
}
so, in conclusion, this is so confusing because the Register function of the WindowClass object returns fine, but the create function fails in the WindowAbstract object because there is no valid class,(error 1407), with that name? Whuh?
I think the problem is lpszClassName = className.c_str()
in the WindowClass
constructor. In general, you shouldn't rely on the value returned from c_str()
being available for any length of time.
In this case, you're (effectively) taking the address of a local variable which may have ceased to exist by the time you call RegisterClassEx
. So RegisterClassEx
is succeeding, but who knows what name it is seeing?
lpszClassName = m_className.c_str()
would probably work (if you assigned m_className
first) but it's still sketchy. Better to call c_str()
immediately before you call RegisterClassEx
.