Search code examples
windowswinapiinlinewndprocwinmain

How to make WndProc inline?


Is it possible, and if so, how to make WndProc inline?

That is, I would like to process Windows messages WM_... within WinMain, so as to avoid seemingly needless function calls.

Thank you.


Solution

  • You cannot inline a window procedure. This is by design.

    You can easily see the architectural limitation when registering a window class. This is done by calling RegisterClassExW, passing along a WNDCLASSEXW structure. That structure requires a valid lpfnWndProc. You cannot take the address of an inlined function.

    There are other aspects that require the window procedure to be an actual function. For example, the stored window procedure address serves as a customization point and allows subclassing controls, e.g. to tweak the behavior of a standard control.

    There's nothing you can do to avoid the function call. If you want to limit the scope of variables, you can assign the result of a lambda expression to the lpfnWndProc member. Visual Studio makes sure to synthesize the correct function signature.