了解一下Windows Cracker

时间:2021-06-11 01:02:34

  Windows Cracker
  消息拆析宏
  可以为消息进行参数分解
  无需记住或查阅资料来了解WParam和lParam的意义
  可以忘记旧的消息处理方式:switch/case
  不适合于大型复杂的需要处理大量消息的应用: coding is slow and program debugging and maintenance become slow as the application grows.
  

  WindowsX.h
  包含三种类型的宏: Macro APIs, window message crackers, Control APIs
  优点:
  1)减少强制类型转换数量,避免强制类型转换出错;
  2)程序可读性增强;
  3)16位和Win32 API之间的转换;
  4)简便易懂(宏);
  5)容易与现存代码结合;
  6)可用于C语言和C++语言;
  7)可效仿编制自己的宏;
  8) 使用这些宏,你不必关心和了解隐含的Windows结构。

   

  代码Demo: 

#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");

    /*  MSDN states WM_CREATE should return 0 to continue and -1 for failure */
    //return FALSE;
    
    /*  but the HANDLE_WM_CREATE macro translates return values greater than zero to 0, and the rest to -1. */
    return TRUE;
}

/*  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 nShow)
{
    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,nShow);
    while(GetMessage(&messages,NULL,0,0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    fprintf(fp,"Output.txt Closed In WinMain()\n");
    fclose(fp);

    return messages.wParam;
}

   输出Output.txt

Output.txt Opened In WinMain()
  Entering fnWndProc_OnCreate()
    hWnd = 658154
  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 = 658154
    Entering fnWndProc_OnDestroy()
      hWnd = 658154
    Leaving fnWndProc_OnDestroy()
  Leaving fnWndProc_OnClose()
Output.txt Closed In WinMain()

   参考:http://www.cplusplus.com/forum/windows/59737/

      http://www.softpedia.com/get/Programming/SDK-DDK/Message-Cracker-Wizard.shtml (Message Cracker Wizard)

       An innovative tool makes every Windows developer forget about the old way of handling messages!

     百度云:http://pan.baidu.com/s/1dFkTds9%20 密码:8q7o