打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Thunk 封装 Windows Timer Api - Jerson Ju - 博客园
/*
描叙:Thunk技术学习
作者:朱剑

时间:2008/8/20

 

环境:VC8

*/
#include 
"stdafx.h"

#include 
<Windows.h>

#include 
<conio.h>

#pragma pack(push, 1)


//由于没有虚方法所以this地址就是m_mov成员的地址
class Thunk
{
    unsigned 
char    m_mov;         
    unsigned int    m_this;         
    unsigned int    m_xchg_push;      
    unsigned char   m_jmp;         
    unsigned int    m_relproc;      
public:
    typedef 
void (_stdcall Thunk::*TMFP)();

    
void start()
    {
        Init((TMFP)
&Thunk::TimerProc,this);
        ::SetTimer(
0,0,500,(TIMERPROC )this);//用构造的假数据欺骗回调函数
    }
    
void Init(TMFP method, const Thunk *pThis)
    {
        
//用union的特点巧取成员地址
        union {
            unsigned 
int func;
            TMFP method;
        } addr;

        addr.method 
= method;
        m_jmp       
= 0xE9;
        m_mov       
= 0xB8;
        m_this       
= (unsigned int)(void *) pThis;
        m_xchg_push    
= 0x50240487;
        m_relproc    
= addr.func - (unsigned int)(void *)(this + 1);
        
//this指针是Thunk类型的,所以this+1就
        
//对象地址+所有成员大小= 对象数据结束的地址
        
//e9进行跳转的距离 = 成员函数的地址-对象数据结束的地址
    }

    VOID CALLBACK TimerProc(
        HWND hwnd,         
// handle to window
        UINT uMsg,         // WM_TIMER message
        UINT_PTR idEvent,  // timer identifier
        DWORD dwTime       // current system time
        )
    {
        printf(
"test timer!\n");
    }

};

#pragma pack(pop)

int _tmain(int argc, _TCHAR* argv[])
{
    Thunk myThunk;
    MSG msg;
    myThunk.start();
    
while (GetMessage(&msg, NULL, 00)) {
        
if (kbhit()) {
            
break;
        }
        DispatchMessage(
&msg);
    }
    
return 0;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
基于Thunk技术的Windows Timer的封装
RTC实时时钟驱动
51单片机数码管交通灯程序
STC89C52-秒表(精确10ms)同时LED二进制计数
运行控制API接口
API函数有send,sendto和sendmsg
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服