WindowProc - need to pass it some data

Started by
7 comments, last by Endurion 15 years, 10 months ago
Is there anyway to pass an additional varible to the WindowProc function? I need to give it a pointer to the window class that created the window. I suppose I could create a map to link the HWND with a pointer to a class but that is hardly an efficent way of doing it...:(

	class Base
	{
		HANDLE Loop;//thread for the windows message pump
		public:
		static LRESULT CALLBACK WindowProc(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam);//this needs a pointer to the base class instance...

		HWND Hwnd;

		Base();
		~Base();
		bool WindowCreate(unsigned width, unsigned Height, const std::string &Caption, bool FullScreen);
		bool WindowDestroy();
...
	};

Advertisement
SetWindowLong(hwnd, GWL_USERDATA, YOUR_DATA);

...

Foo f = GetWindowLong(hwnd, GWL_USERDATA));

More information is of course on MSDN.
Here are a couple links that might help.

Creating a Win32 Window Wrapper Class

Creating a Win32 Window Wrapper Class - extended discussion

subclassing, extending from Oluseyi's article - GameDev.Net ...
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Why is this not working? It doesn't get the pointer and as a result crashes when the WindowProc needs to use it :(
		struct WindowLoopData		{			Base* Owner;			std::string Caption;			unsigned Width, Height;			bool Fullscreen;			WindowLoopData(Base* Owner, std::string Caption, unsigned Width, unsigned Height, bool Fullscreen)			{				this->Owner      = Owner;				this->Caption    = Caption;				this->Width      = Width;				this->Height     = Height;				this->Fullscreen = Fullscreen;			}		};		unsigned __stdcall WindowLoop(void* Param)		{			WindowLoopData *Data = (WindowLoopData*)Param;			Base* Owner = Data->Owner;			//create a window			Owner->Hwnd = CreateWindowEx(				WS_EX_CLIENTEDGE,				"BaseWc", Data->Caption.c_str(),				WS_OVERLAPPEDWINDOW/*WS_EX_TOPMOST*/,				0, 0, Data->Width, Data->Height,				0, 0, hInstance, Owner);			if(!Owner->Hwnd)			{				lib::ErrorSet("Base::WindowCreate failed - Window creation failed");				return false;			}			ShowWindow(Owner->Hwnd, SW_SHOW);			MSG Msg;			while(GetMessage(&Msg, Owner->Hwnd, 0, 0) > 0)			{				TranslateMessage(&Msg);				DispatchMessage (&Msg);			}						delete Data;			ThreadExit(Msg.wParam);			return 0;		}

	LRESULT CALLBACK Base::WindowProcRouter(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)	{		if(Msg == WM_NCCREATE)//first Msg = 36 ???		{			Base *Owner = (Base*)((LPCREATESTRUCT)lParam)->lpCreateParams;			SetWindowLong(Hwnd, GWL_USERDATA, (long)Owner);			Owner->Hwnd = Hwnd;			return 0;		}		else		{			Base *Owner = (Base*)(GetWindowLong(Hwnd, GWL_USERDATA));			return Owner->WindowProc(Msg, wParam, lParam);		}	}
WM_NCCREATE is not necessarily the first window message that the window receives; it's just the first window message that the window receives that you can extract the create parameters from. You need to handle the case where other window messages come before you've extracted the create parameters from WM_NCCREATE.
ok...now Ive got another problem

case WM_NCCREATE:	SetWindowLongPtr(Hwnd, DWLP_USER, (long)((LPCREATESTRUCT)lParam)->lpCreateParams);//Hwnd = 0x001c05b8, base pointer = 0x003376c0	break;

And on the next message
Base *Window = (Base*)GetWindowLongPtr(Hwnd, DWLP_USER);//Hwnd = 0x001c05b8, base pointer = NULL ???

Wheres my stored value disappearing too...

EDIT: never mind..stupid mistake/
Where's that DWLP_USER coming from? That's for dialogs, it should be GWLP_USERDATA.
THat was the stupid mistake :)

Anyway new problem...The windows are created and their message pumps handeled by another thread. When my main thread calls "DestroyWindow(Hwnd);" for a window the window is not destroyed and a WM_DESTROY message is not sent :(
HWNDs are thread sensitive. You can only pump messages in the same thread that created the window.

Never separate that. That also applies to a lot of window related functions.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement