windowsx.h message crackers HANDLE_MSG

Jan 19, 2012 at 7:44pm
Yesterday in this post...

http://www.cplusplus.com/forum/windows/59568/

sloppy9 saw my message cracker scheme which is based on function pointers and is a bit complex and asked if I was aware of the message cracker scheme in windowsx.h based on the HANDLE_MSG macros. I wasn't, so I took a look at it and almost got it working. However, I must be doing something wrong, because its starting - but then terminating immediately. No crashes though. The WM_CREATE message is being processed, then the WM_DESTROY, then it ends normally. Its almost as if I'd forgotten break statements in a WndProc switch and they are all sequentially executing until a crash or WM_DESTROY is hit. Here is my program. Anyone can tell me what I need to fix?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <cstdio>
FILE* fp=NULL;

/* BOOL Cls_OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct) */
//#define HANDLE_WM_CREATE(hwnd, wParam, lParam, fn) ((fn)((hwnd), (LPCREATESTRUCT)(lParam)) ? 0L : (LRESULT)-1L)
//#define FORWARD_WM_CREATE(hwnd, lpCreateStruct, fn) (BOOL)(DWORD)(fn)((hwnd), WM_CREATE, 0L, (LPARAM)(LPCREATESTRUCT)(lpCreateStruct))

BOOL fnWndProc_OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct)
{
 fprintf(fp,"  Entering fnWndProc_OnCreate()\n");
 fprintf(fp,"    hWnd = %u\n",(unsigned)hWnd);
 fprintf(fp,"  Leaving fnWndProc_OnCreate()\n\n");

 return FALSE;
}

/*  void Cls_OnSize(HWND hwnd, UINT state, int cx, int cy) */
//  #define HANDLE_WM_SIZE(hwnd, wParam, lParam, fn) ((fn)((hwnd), (UINT)(wParam), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam)), 0L)
//  #define FORWARD_WM_SIZE(hwnd, state, cx, cy, fn)  (void)(fn)((hwnd), WM_SIZE, (WPARAM)(UINT)(state), MAKELPARAM((cx), (cy)))


void fnWndProc_OnSize(HWND hWnd, UINT state, int cx, int cy)
{
 fprintf(fp,"  Entering fnWndProc_OnSize()\n");
 fprintf(fp,"    Width = %d\tHeight = %d\n",cx,cy);
 fprintf(fp,"  Leaving fnWndProc_OnSize()\n\n");
}

/*  void Cls_OnPaint(HWND hwnd) */
//  #define HANDLE_WM_PAINT(hwnd, wParam, lParam, fn) ((fn)(hwnd), 0L)
//  #define FORWARD_WM_PAINT(hwnd, fn) (void)(fn)((hwnd), WM_PAINT, 0L, 0L)

void fnWndProc_OnPaint(HWND hWnd)
{
 PAINTSTRUCT ps;
 HDC hDC;

 fprintf(fp,"  Entering fnWndProc_OnPaint()\n");
 hDC=BeginPaint(hWnd,&ps);
 fprintf(fp,"    Painting Window!\n");
 fprintf(fp,"    ps.rcPaint.right  = %d\n",(int)ps.rcPaint.right);
 fprintf(fp,"    ps.rcPaint.bottom = %d\n",(int)ps.rcPaint.bottom);
 EndPaint(hWnd,&ps);
 fprintf(fp,"  Leaving fnWndProc_OnPaint()\n\n");
}


/* void Cls_OnClose(HWND hwnd) */
// #define HANDLE_WM_CLOSE(hwnd, wParam, lParam, fn) ((fn)(hwnd), 0L)
// #define FORWARD_WM_CLOSE(hwnd, fn) (void)(fn)((hwnd), WM_CLOSE, 0L, 0L)

void fnWndProc_OnClose(HWND hWnd)
{
 fprintf(fp,"  Entering fnWndProc_OnClose()\n");
 fprintf(fp,"    hWnd = %u\n",(unsigned)hWnd);
 DestroyWindow(hWnd);
 fprintf(fp,"  Leaving fnWndProc_OnClose()\n");
}

/* void Cls_OnDestroy(HWND hwnd) */
//  #define HANDLE_WM_DESTROY(hwnd, wParam, lParam, fn)    ((fn)(hwnd), 0L)
//  #define FORWARD_WM_DESTROY(hwnd, fn)   (void)(fn)((hwnd), WM_DESTROY, 0L, 0L)

void fnWndProc_OnDestroy(HWND hWnd)
{
 fprintf(fp,"    Entering fnWndProc_OnDestroy()\n");
 fprintf(fp,"      hWnd = %u\n",(unsigned)hWnd);
 PostQuitMessage(0);
 fprintf(fp,"    Leaving fnWndProc_OnDestroy()\n");
}


LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)
 {
  HANDLE_MSG(hwnd, WM_CREATE,  fnWndProc_OnCreate);
  HANDLE_MSG(hwnd, WM_SIZE,    fnWndProc_OnSize);
  HANDLE_MSG(hwnd, WM_PAINT,   fnWndProc_OnPaint);
  HANDLE_MSG(hwnd, WM_CLOSE,   fnWndProc_OnClose);
  HANDLE_MSG(hwnd, WM_DESTROY, fnWndProc_OnDestroy);
  default:return (DefWindowProc(hwnd, msg, wParam, lParam));
 }
}


int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 TCHAR szClassName[]=_T("Debugging Windows Programs");
 WNDCLASSEX wc;
 MSG messages;
 HWND hWnd;

 wc.lpszClassName=szClassName;                wc.lpfnWndProc=fnWndProc;
 wc.cbSize=sizeof (WNDCLASSEX);               wc.style=CS_DBLCLKS;
 wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);     wc.hInstance=hIns;
 wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION);  wc.hCursor=LoadCursor(NULL,IDC_ARROW);
 wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW;    wc.cbWndExtra=0;
 wc.lpszMenuName=NULL;                        wc.cbClsExtra=0;
 RegisterClassEx(&wc);
 fp=fopen("Output.txt","w");
 fprintf(fp,"Output.txt Opened In WinMain()\n");
 hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,75,75,320,305,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
 }
 fprintf(fp,"Output.txt Closed In WinMain()\n");
 fclose(fp);

 return messages.wParam;
}


This is what shows up in the debug output file...

1
2
3
4
5
6
7
8
9
Output.txt Opened In WinMain()
  Entering fnWndProc_OnCreate()
    hWnd = 3802640
  Leaving fnWndProc_OnCreate()

    Entering fnWndProc_OnDestroy()
      hWnd = 3802640
    Leaving fnWndProc_OnDestroy()
Output.txt Closed In WinMain()

Last edited on Jan 19, 2012 at 7:45pm
Jan 20, 2012 at 7:32am
closed account (DSLq5Di1)
A little confusingly.. MSDN states WM_CREATE should return 0 to continue and -1 for failure, but the HANDLE_WM_CREATE macro translates return values greater than zero to 0, and the rest to -1.

I'd like to believe they made this change as a result of returning FALSE to continue being counter intuitive, though it would've been nice if this were documented in the remarks section!
Jan 20, 2012 at 1:47pm
Hah!!! That was it! I returned TRUE (as you say, the reverse of what I'd think) and it worked...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Output.txt Opened In WinMain()
  Entering fnWndProc_OnCreate()
    hWnd = 1901208
  Leaving fnWndProc_OnCreate()

  Entering fnWndProc_OnSize()
    Width = 304	Height = 267
  Leaving fnWndProc_OnSize()

  Entering fnWndProc_OnPaint()
    Painting Window!
    ps.rcPaint.right  = 304
    ps.rcPaint.bottom = 267
  Leaving fnWndProc_OnPaint()

  Entering fnWndProc_OnClose()
    hWnd = 1901208
    Entering fnWndProc_OnDestroy()
      hWnd = 1901208
    Leaving fnWndProc_OnDestroy()
  Leaving fnWndProc_OnClose()
Output.txt Closed In WinMain()


Thanks Sloppy 9. I was beginning to think nobody would help! I suppose I should have more faith in my fellow man.
Topic archived. No new replies allowed.