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 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// Main.cpp
// cl GetMsg.cpp /O1 /Os kernel32.lib user32.lib
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include "GetMsg.h"
FILE* fp = NULL;
LRESULT fnWndProc_OnCreate(WndEventArgs& Wea)
{
fprintf(fp," Entering fnWndProc_OnCreate()\n");
fprintf(fp," Wea.hWnd = 0x%p\n",Wea.hWnd);
fprintf(fp," Leaving fnWndProc_OnCreate()\n\n");
return 0;
}
LRESULT fnWndProc_OnBoobyTrap(WndEventArgs& Wea)
{
fprintf(fp," Entering fnWndProc_OnBoobyTrap()\n");
fprintf(fp," Wea.hWnd = 0x%p\n",Wea.hWnd);
fprintf(fp," Leaving fnWndProc_OnBoobyTrap()\n\n");
return 0;
}
LRESULT fnWndProc_OnClose(WndEventArgs& Wea)
{
fprintf(fp," Entering fnWndProc_OnClose()\n");
fprintf(fp," Wea.hWnd = 0x%p\n",Wea.hWnd);
//DestroyWindow(Wea.hWnd);
PostMessage(Wea.hWnd,WM_DESTROY,0,0);
BOOL blnReturn=PostMessage(Wea.hWnd,BOOBY_TRAP,0,0);
if(blnReturn)
fprintf(fp," PostMessage(BOOBY_TRAP) Succeeded!\n");
else
fprintf(fp," PostMessage(BOOBY_TRAP) Failed!\n");
fprintf(fp," Leaving fnWndProc_OnClose()\n\n");
return 0;
}
LRESULT fnWndProc_OnDestroy(WndEventArgs& Wea)
{
fprintf(fp," Entering fnWndProc_OnDestroy()\n");
fprintf(fp," Wea.hWnd = 0x%p\n",Wea.hWnd);
//BOOL blnReturn=PostMessage(Wea.hWnd,BOOBY_TRAP,0,0);
PostQuitMessage(0);
//fprintf(fp," blnReturn = %d\n",blnReturn);
fprintf(fp," Leaving fnWndProc_OnDestroy()\n\n");
return 0;
}
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam,LPARAM lParam)
{
WndEventArgs Wea; // This procedure loops through the EVENTHANDER struct array
// to try to make a match with the msg parameter of the WndProc.
for(size_t i=0; i<dim(EventHandler); i++) // If a match is made the event handling procedure is called
{ // through a function pointer - (EventHandler[i].fnPtr). If no
if(EventHandler[i].Msg==msg) // match is found the msg is passed onto DefWindowProc().
{
Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
return (*EventHandler[i].fnPtr)(Wea);
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
wchar_t szClassName[]=L"GetMsg";
int blnGetMsg=0;
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
fp=fopen("Output.txt","w");
if(!fp)
return 0;
fprintf(fp,"Entering WinMain()\n");
wc.lpszClassName = szClassName;
wc.lpfnWndProc = fnWndProc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.hIcon = (HICON)LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
wc.hIconSm = (HICON)LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
wc.hCursor = (HCURSOR)LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.cbWndExtra = 0;
wc.cbClsExtra = 0;
wc.lpszMenuName = NULL;
wc.hInstance = hIns;
RegisterClassEx(&wc);
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,600,400,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
while(true)
{
blnGetMsg=GetMessage(&messages,NULL,0,0);
switch(blnGetMsg)
{
case -1:
fprintf(fp," blnGetMsg = -1!!!!\n");
fprintf(fp,"Leaving WinMain()\n");
fclose(fp);
return messages.wParam;
case 0:
fprintf(fp," blnGetMsg = WM_QUIT!\n");
fprintf(fp,"Leaving WinMain()\n");
fclose(fp);
return messages.wParam;
default:
TranslateMessage(&messages);
DispatchMessage(&messages);
}
}
}
| |