分享
 
 
 

学习ATL的一个习作 : )

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

#include "stdafx.h"

template<typename ObjectType>

class Delegate

{

// Type Definitions

public:

typedef LRESULT (ObjectType::*CallType)(HWND, UINT, WPARAM, LPARAM);

// Constructor

public:

Delegate(ObjectType* pObject, CallType pCallee) : m_pObject(pObject), m_pCallee(pCallee)

{

}

// Destrcutor

public:

~Delegate()

{

}

// Thunk Structure

public:

/* Adjust pack size */

#pragma pack(push,1)

struct Thunk

{

/* push ebp */

BYTE m_pushebp;

/* mov ebp, esp */

BYTE m_movebp;

BYTE m_esp;

/* mov eax, dword ptr [ebp + 8] */

BYTE m_moveax1;

BYTE m_ebpplus1;

BYTE m_offset1;

/* push eax */

BYTE m_pusheax1;

/* mov eax, dword ptr [ebp + 12] */

BYTE m_moveax2;

BYTE m_ebpplus2;

BYTE m_offset2;

/* push eax */

BYTE m_pusheax2;

/* mov eax, dword ptr [ebp + 16] */

BYTE m_moveax3;

BYTE m_ebpplus3;

BYTE m_offset3;

/* push eax */

BYTE m_pusheax3;

/* mov eax, dword ptr [ebp + 20] */

BYTE m_moveax4;

BYTE m_ebpplus4;

BYTE m_offset4;

/* push eax */

BYTE m_pusheax4;

/* mov eax, this*/

BYTE m_moveax5;

void* m_this;

/* push eax */

BYTE m_pusheax5;

/* mov eax, helper*/

BYTE m_moveax6;

void* m_helper;

/* call eax */

BYTE m_call;

BYTE m_calleax;

/* pop ebp */

BYTE m_popebp;

/* ret 10h */

BYTE m_ret;

DWORD m_10h;

};

/* Reset pack size */

#pragma pack(pop)

// Properties

protected:

Thunk m_Thunk;

ObjectType* m_pObject;

CallType m_pCallee;

// Methods

public:

operator WNDPROC(void)

{

void* pf;

__asm

{

mov eax, Delegate<ObjectType>::Helper

mov pf, eax

}

/* push ebp */

m_Thunk.m_pushebp = 0x55;

/* mov ebp, esp */

m_Thunk.m_movebp = 0x8B;

m_Thunk.m_esp = 0xEC;

/* mov eax dword ptr [ebp + 20] */

m_Thunk.m_moveax1 = 0x8B;

m_Thunk.m_ebpplus1 = 0x45;

m_Thunk.m_offset1 = 20;

/* push eax */

m_Thunk.m_pusheax1 = 0x50;

/* mov eax dword ptr [ebp + 16] */

m_Thunk.m_moveax2 = 0x8B;

m_Thunk.m_ebpplus2 = 0x45;

m_Thunk.m_offset2 = 16;

/* push eax */

m_Thunk.m_pusheax2 = 0x50;

/* mov eax dword ptr [ebp + 12] */

m_Thunk.m_moveax3 = 0x8B;

m_Thunk.m_ebpplus3 = 0x45;

m_Thunk.m_offset3 = 12;

/* push eax */

m_Thunk.m_pusheax3 = 0x50;

/* mov eax dword ptr [ebp + 8] */

m_Thunk.m_moveax4 = 0x8B;

m_Thunk.m_ebpplus4 = 0x45;

m_Thunk.m_offset4 = 8;

/* push eax */

m_Thunk.m_pusheax4 = 0x50;

/* mov eax, this */

m_Thunk.m_moveax5 = 0xB8;

m_Thunk.m_this = this;

/* push eax */

m_Thunk.m_pusheax5 = 0x50;

/* mov eax, Delegate::Helper */

m_Thunk.m_moveax6 = 0xB8;

m_Thunk.m_helper = pf;

/*call eax*/

m_Thunk.m_call = 0xFF;

m_Thunk.m_calleax = 0xD0;

/* pop ebp */

m_Thunk.m_popebp = 0x5D;

/* ret 10h */

m_Thunk.m_ret = 0xC2;

m_Thunk.m_10h = 0x10;

return (LRESULT(CALLBACK*)(HWND, UINT, WPARAM, LPARAM))&m_Thunk;

}

HRESULT CALLBACK Helper(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)

{

return (m_pObject->*m_pCallee)(hWnd, uMessage, wParam, lParam);

}

};

class CWindow

{

public:

virtual LRESULT WndProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)

{

if(uMessage==WM_DESTROY)

{

::PostQuitMessage(0);

}

return ::DefWindowProc(hWnd, uMessage, wParam, lParam);

}

};

class CMyWindow : public CWindow

{

public:

virtual LRESULT WndProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)

{

if(uMessage==WM_LBUTTONDOWN)

{

::MessageBox(hWnd, "ok", "ok", MB_OK);

}

return CWindow::WndProc(hWnd, uMessage, wParam, lParam);

}

};

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

CMyWindow theWindow;

CWindow* pWindow = &theWindow;

Delegate<CWindow> theDelegate(pWindow, CWindow::WndProc);

WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.cbClsExtra = 0;

wcex.cbWndExtra = 0;

wcex.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);

wcex.hCursor = ::LoadCursor(NULL, IDC_ARROW);

wcex.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);

wcex.hIconSm = wcex.hIcon;

wcex.hInstance = ::GetModuleHandle(NULL);

// Replaced with a member function delegation

wcex.lpfnWndProc = theDelegate;

wcex.lpszClassName = "TestClass";

wcex.lpszMenuName = NULL;

wcex.style = CS_VREDRAW | CS_HREDRAW;

::RegisterClassEx(&wcex);

HWND hWnd = ::CreateWindow("TestClass",

"TestWindow",

WS_OVERLAPPEDWINDOW | WS_VISIBLE,

0, 0,

320, 240,

NULL, NULL,

::GetModuleHandle(NULL), NULL);

::UpdateWindow(hWnd);

MSG msg;

while(::GetMessage(&msg, NULL, 0, 0))

{

::TranslateMessage(&msg);

::DispatchMessage(&msg);

}

::ExitProcess(0);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有