Jump to content
  • Advertisement
Sign in to follow this  
mind in a box

WndProc in a class? Is there a problem with this code?

This topic is 2903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello!

I looked around in the samples of the WindowsSDK and there I found out that Microsoft put the WindowProc-Callback-Function into a class. That was always a problem for me, and I had to make it a global which resulted into pretty confusing code. I couldn't put it into class because then it could be only static. So I'm happy that I found a solution now. However, there is a thing I am curious about.

In the sample they have a static WndProc in a class. When creating the window, they put the pointer to the Class into the lParam Value:


m_hwnd = CreateWindow(
L"D2DDemoApp",
L"Direct2D Combine Geometries",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
static_cast<UINT>(ceil(640.f * dpiX / 96.f)),
static_cast<UINT>(ceil(480.f * dpiY / 96.f)),
NULL,
NULL,
HINST_THISCOMPONENT,
this //<------- Here
);



and then, in the WM_CREATE:


if (message == WM_CREATE)
{
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
DemoApp *pDemoApp = (DemoApp *)pcs->lpCreateParams;

::SetWindowLongPtrW(
hwnd,
GWLP_USERDATA,
PtrToUlong(pDemoApp) //<----------
);

result = 1;
}



And to get the pointer:


DemoApp *pDemoApp = reinterpret_cast<DemoApp *>(static_cast<LONG_PTR>(
::GetWindowLongPtrW(
hwnd,
GWLP_USERDATA
)));



But I read so much about converting pointers to a number and 64-Bit compatibility issues. So, is the lParam always a 64-Bit number (It is a "long", isn't it?) And is the ULONG datatype always big enough?

Sorry if that are dumb questions, I just would like to know if there is a problem before I implement it into my project.

Share this post


Link to post
Share on other sites
Advertisement
I don't think there should be any concerns with using it that way.

The idea is you can have multiple windows under the one process provided by an object orientated environment. Having it global works (while being messy as you say) for one window but you have to use that pointer to take advantage of oop when making different windows.

The msdn page for CreateWindow is quite good.

Share this post


Link to post
Share on other sites
LPARAM is a typedef'd LONG_PTR, and the declaration for LONG_PTR is:

#if defined(_WIN64)
typedef __int64 LONG_PTR;
#else
typedef long LONG_PTR;
#endif

In addition, the Set/GetWindowLongPtr functions are 64 bit safe too. Your code uses GetWindowLongPtrW, which forces the unicode version of the function. If you take the W off the end, there are a bunch of #defines which automatically call the correct one depending on whether you compiled with unicode or not. I'm not sure if your code will work properly when you disable unicode or not. MSDN documents GetWindowLongPtr but not GetWindowLongPtrW.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!