WTL 之trunk技术学习
相比于MFC的消息机制,WTL/ATL的实现更加优雅。后者将win32 API与面向对象技术完美地结合起来,去掉了庞杂的MFC依赖,生成的软件体积更小,运行速度更快。在其中,如何将窗口函数转变为对窗口对象成员函数的调用,是WTL/ATL消息机制的核心。以下利用trunk技术模拟了这一过程,仅供参考:
#include <Windows.h>
#include <assert.h>
#include <tchar.h>
thread_local void* pWnd = nullptr;#pragma pack(push,1)
struct _StdCallThunk32//x86
{DWORD m_mov;ULONG_PTR m_this;BYTE m_jmp;ULONG_PTR m_relproc;void Init(ULONG_PTR wPtr, ULONG_PTR proc){m_mov = 0x042444C7;//mov dword ptr [esp+4], pThism_this = wPtr;m_jmp = 0xe9;//jmp pRelFuncm_relproc = proc;}
};struct _StdCallThunk64//x64
{unsigned char mov_rax_1[2];unsigned char object[sizeof(ULONG_PTR)];unsigned char mov_rax_to_rcx[3];unsigned char mov_rax_2[2];unsigned char procedure[sizeof(ULONG_PTR)];unsigned char jump_rax[3];void Init(ULONG_PTR wPtr, ULONG_PTR proc){mov_rax_1[0] = 0x48;//mov rax, pThismov_rax_1[1] = 0xb8;memcpy(object, &wPtr, sizeof(ULONG_PTR));mov_rax_to_rcx[0] = 0x48;//mov rcx,raxmov_rax_to_rcx[1] = 0x89;mov_rax_to_rcx[2] = 0xc1;mov_rax_2[0] = 0x48;//mov rax, pFuncmov_rax_2[1] = 0xb8;memcpy(procedure, &proc, sizeof(ULONG_PTR));jump_rax[0] = 0x48;//jmp raxjump_rax[1] = 0xff;jump_rax[2] = 0xe0;}
};#pragma pack(pop)#if defined(_WIN64)
typedef struct _StdCallThunk64 StdCallThunk;
#elif defined(_WIN32)
typedef struct _StdCallThunk32 StdCallThunk;
#endif
class Window
{
public:Window();~Window();public:BOOL Create();protected:LRESULT WndProc(UINT message, WPARAM wParam, LPARAM lParam);protected:HWND m_hWnd;StdCallThunk* m_pThunk;protected:static LRESULT CALLBACK TempWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
};Window::Window():m_hWnd(NULL),m_pThunk(nullptr)
{}Window::~Window()
{if(m_pThunk != nullptr)VirtualFree(m_pThunk, sizeof(StdCallThunk), MEM_RELEASE);
}BOOL Window::Create()
{LPCTSTR lpszClassName = _T("ClassName");HINSTANCE hInstance = GetModuleHandle(NULL);WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };wcex.lpfnWndProc = TempWndProc;wcex.hInstance = hInstance;wcex.lpszClassName = lpszClassName;wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);ATOM regRes = RegisterClassEx(&wcex);pWnd = this;m_pThunk = (StdCallThunk*)VirtualAlloc(NULL, sizeof(StdCallThunk), MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);m_hWnd = CreateWindow(lpszClassName, NULL, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);if (m_hWnd == NULL){return FALSE;}ShowWindow(m_hWnd, SW_SHOW);UpdateWindow(m_hWnd);return TRUE;
}LRESULT Window::WndProc(UINT message, WPARAM wParam, LPARAM lParam)
{switch (message){case WM_LBUTTONUP:MessageBox(m_hWnd, _T("LButtonUp"), _T("Message"), MB_OK | MB_ICONINFORMATION);break;case WM_RBUTTONUP:MessageBox(m_hWnd, _T("RButtonUp"), _T("Message"), MB_OK | MB_ICONINFORMATION);break;case WM_DESTROY:PostQuitMessage(0);break;default:break;}return DefWindowProc(m_hWnd, message, wParam, lParam);
}LRESULT CALLBACK Window::TempWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{Window* pThis = (Window*)pWnd;assert(pThis != nullptr);WNDPROC pWndProc = (WNDPROC)pThis->m_pThunk;
#if defined(_WIN64)ULONG_PTR rel = (LONG_PTR)&Window::StaticWndProc;auto ret = SetWindowLongPtr(hWnd, GWLP_WNDPROC, (ULONG_PTR)pWndProc);
#elif defined(_WIN32)ULONG_PTR rel = (DWORD)&Window::StaticWndProc - ((DWORD)pThis->m_pThunk + sizeof(StdCallThunk));auto ret = SetWindowLong(hWnd, GWLP_WNDPROC, (ULONG)pWndProc);
#endifpThis->m_pThunk->Init((ULONG_PTR)pThis, rel);//FlushInstructionCache(GetCurrentProcess(), pThis->m_pThunk, sizeof(StdCallThunk));pThis->m_hWnd = hWnd;return pWndProc(hWnd, message, wParam, lParam);
}LRESULT CALLBACK Window::StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{return ((Window*)hWnd)->WndProc(message, wParam, lParam);
}int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{Window wnd;wnd.Create();MSG msg;while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}return (int)msg.wParam;
}