Without creating global variables, is there a way to pass variables to the WindowProcedure that handles the messages?

For example, if I create a variable and initialize it in WinMain, is there any way to use it in my WindowProcedure?

What I am doing is this. I have a class called Map. I also have a class called Texture and Window.

When I create Texture and Window, certain variables are created such as texWidth, texHeight, windowWidth, and WindowHeight inside of the class.

In my Window Procedure, I need to use those variables held inside of Texture and Window for some function calls.

Is there any way to do this easily? I would really like to keep it simple and try to avoid global variables if possible.

9 Years
Discussion Span
Last Post by robgxxx

define a struct to hold the data/operations for your window

struct my_window_stuff
// functions          // ...
// data                     // Texture
// Window
// Map
// whatever else

In main/WinMain:
1. initialize a my_window_stuff structure.
2. pass its address (pointer) in the LPARAM argument of CreateWindow/CreateWindowEx

in the window procedure:
case WM_CREATE: store the pointer received (in CREATESTRUCT::lpCreateParams) in the window data
using SetWindowLong/SetWindowLongPtr with WL_USERDATA/GWLP_USERDATA as index.

subsequent messages: retrieve the pointer through GetWindowLong/GetWindowLongPtr,
cast it to my_window_stuff* and use it.

note: if the my_window_stuff needs to be allocated using new (for instance if CreateWindow/CreateWindowEx needs to be called from somewhere other than main/WinMain),
case WM_DESTROY: retrieve the pointer, cast and delete

Edited by happygeek: fixed formatting


What Vijayon is talking about I really can't guess.
Global structures and global variables are one and the same
or at least of the same generic route.

WindowProcedure is a a callback funtion, you use lParam amd wParm
to transport variables with the additional facility of HIWORD and LOWORD .

Both of you need to delve into win32 system services!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.