_stdcallthunk
// c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\atlmfc\include\atlstdthunk.h
// This is a part of the Active Template Library.
// Copyright (C) Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Active Template Library Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Active Template Library product.
#ifndef __ATLSTDTHUNK_H__
#define __ATLSTDTHUNK_H__
#pragma once
#pragma push_macro("new")
#undef new
#include <atldef.h>
#if !defined(_ATL_USE_WINAPI_FAMILY_DESKTOP_APP)
#error This file is not compatible with the current WINAPI_FAMILY
#endif
#if !defined(USE_ATL_THUNK2) && (defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64))
#define USE_ATL_THUNK2
#endif
#include <atlthunk.h>
namespace ATL
{
/////////////////////////////////////////////////////////////////////////////
// Thunks for __stdcall member functions
#if defined(_M_IX86)
PVOID __stdcall __AllocStdCallThunk(VOID);
VOID __stdcall __FreeStdCallThunk(_In_opt_ PVOID);
#pragma pack(push,1)
struct _stdcallthunk
{
DWORD m_mov; // mov dword ptr [esp+0x4], pThis (esp+0x4 is hWnd)
DWORD m_this; //
BYTE m_jmp; // jmp WndProc
DWORD m_relproc; // relative jmp
BOOL Init(
_In_ DWORD_PTR proc,
_In_opt_ void* pThis)
{
m_mov = 0x042444C7; //C7 44 24 0C
m_this = PtrToUlong(pThis);
m_jmp = 0xe9;
m_relproc = DWORD((INT_PTR)proc - ((INT_PTR)this+sizeof(_stdcallthunk)));
// write block from data cache and
// flush from instruction cache
FlushInstructionCache(GetCurrentProcess(), this, sizeof(_stdcallthunk));
return TRUE;
}
//some thunks will dynamically allocate the memory for the code
void* GetCodeAddress()
{
return this;
}
_Ret_maybenull_ _Post_writable_byte_size_(sizeof(_stdcallthunk)) void* operator new(_In_ size_t)
{
return __AllocStdCallThunk();
}
void operator delete(_In_opt_ void* pThunk)
{
__FreeStdCallThunk(pThunk);
}
};
#pragma pack(pop)
#elif defined(_M_X64)
PVOID __stdcall __AllocStdCallThunk(VOID);
VOID __stdcall __FreeStdCallThunk(PVOID);
#pragma pack(push,2)
struct _stdcallthunk
{
USHORT RcxMov; // mov rcx, pThis
ULONG64 RcxImm; //
USHORT RaxMov; // mov rax, target
ULONG64 RaxImm; //
USHORT RaxJmp; // jmp target
BOOL Init(
_In_ DWORD_PTR proc,
_In_opt_ void *pThis)
{
RcxMov = 0xb948; // mov rcx, pThis
RcxImm = (ULONG64)pThis; //
RaxMov = 0xb848; // mov rax, target
RaxImm = (ULONG64)proc; //
RaxJmp = 0xe0ff; // jmp rax
FlushInstructionCache(GetCurrentProcess(), this, sizeof(_stdcallthunk));
return TRUE;
}
//some thunks will dynamically allocate the memory for the code
void* GetCodeAddress()
{
return this;
}
_Ret_maybenull_ _Post_writable_byte_size_(sizeof(_stdcallthunk)) void* operator new(_In_ size_t)
{
return __AllocStdCallThunk();
}
void operator delete(_In_opt_ void* pThunk)
{
__FreeStdCallThunk(pThunk);
}
};
#pragma pack(pop)
#elif defined (_M_THUMB)
// note this case must be before _M_ARM because _M_ARM is also defined
PVOID __stdcall __AllocStdCallThunk(VOID);
VOID __stdcall __FreeStdCallThunk(PVOID);
#pragma pack(push,2)
struct _stdcallthunk
{
USHORT m_mov_r0[2]; // mov r0, pThis
USHORT m_mov_pc[2]; // mov pc, pFunc
DWORD m_pThis;
DWORD m_pFunc;
BOOL Init(DWORD_PTR proc, void* pThis)
{
m_mov_r0[0] = 0xF8DF;
m_mov_r0[1] = 0x0004;
m_mov_pc[0] = 0xF8DF;
m_mov_pc[1] = 0xF004;
m_pThis = (DWORD)pThis;
m_pFunc = (DWORD)proc;
// write block from data cache and
// flush from instruction cache
FlushInstructionCache(GetCurrentProcess(), this, sizeof(_stdcallthunk));
return TRUE;
}
void* GetCodeAddress()
{
return (void *)((ULONG_PTR)this | 1);
}
void* operator new(size_t)
{
return __AllocStdCallThunk();
}
void operator delete(void* pThunk)
{
__FreeStdCallThunk(pThunk);
}
};
#pragma pack(pop)
#elif defined(_M_ARM64)
PVOID __stdcall __AllocStdCallThunk(VOID);
VOID __stdcall __FreeStdCallThunk(PVOID);
#pragma pack(push,4)
struct _stdcallthunk {
ULONG m_ldr_r16; // ldr x16, [pc, #24]
ULONG m_ldr_r0; // ldr x0, [pc, #12]
ULONG m_br; // br x16
ULONG m_pad;
ULONG64 m_pThis;
ULONG64 m_pFunc;
BOOL Init(DWORD_PTR proc, void* pThis) {
m_ldr_r16 = 0x580000D0;
m_ldr_r0 = 0x58000060;
m_br = 0xd61f0200;
m_pThis = (ULONG64)pThis;
m_pFunc = (ULONG64)proc;
// write block from data cache and
// flush from instruction cache
FlushInstructionCache(GetCurrentProcess(), this, sizeof(_stdcallthunk));
return TRUE;
}
void* GetCodeAddress() {
return (void *)((ULONG_PTR)this | 1);
}
void* operator new(size_t)
{
return __AllocStdCallThunk();
}
void operator delete(void* pThunk) {
__FreeStdCallThunk(pThunk);
}
};
#pragma pack(pop)
#elif defined(_ARM_)
#pragma pack(push,4)
struct _stdcallthunk // this should come out to 16 bytes
{
DWORD m_mov_r0; // mov r0, pThis
DWORD m_mov_pc; // mov pc, pFunc
DWORD m_pThis;
DWORD m_pFunc;
BOOL Init(
_In_ DWORD_PTR proc,
_In_opt_ void* pThis)
{
m_mov_r0 = 0xE59F0000;
m_mov_pc = 0xE59FF000;
m_pThis = (DWORD)pThis;
m_pFunc = (DWORD)proc;
// write block from data cache and
// flush from instruction cache
FlushInstructionCache(GetCurrentProcess(), this, sizeof(_stdcallthunk));
return TRUE;
}
void* GetCodeAddress()
{
return this;
}
};
#pragma pack(pop)
#else
#error Only X86, X64, ARM, and ARM64 supported
#endif
#if defined(_M_IX86) || defined (_M_X64) || defined(_M_ARM)
#pragma pack(push,8)
// avoid ODR violations
#ifdef USE_ATL_THUNK2
typedef class CDynamicStdCallThunk2 CDynamicStdCallThunk; class CDynamicStdCallThunk2
#else
class CDynamicStdCallThunk
#endif
{
public:
#ifdef USE_ATL_THUNK2
AtlThunkData_t* pThunk;
#else
_stdcallthunk *pThunk;
#endif
#ifdef USE_ATL_THUNK2
CDynamicStdCallThunk2()
#else
CDynamicStdCallThunk()
#endif
{
pThunk = NULL;
}
#ifdef USE_ATL_THUNK2
~CDynamicStdCallThunk2()
#else
~CDynamicStdCallThunk()
#endif
{
if (pThunk)
{
#ifdef USE_ATL_THUNK2
AtlThunk_FreeData(pThunk);
#else
delete pThunk;
#endif
}
}
BOOL Init(
_In_ DWORD_PTR proc,
_In_opt_ void *pThis)
{
if (pThunk == NULL)
{
#ifdef USE_ATL_THUNK2
pThunk = AtlThunk_AllocateData();
#else
pThunk = new _stdcallthunk;
#endif
if (pThunk == NULL)
{
return FALSE;
}
}
#ifdef USE_ATL_THUNK2
AtlThunk_InitData(pThunk, (void*)proc, (size_t)pThis);
return TRUE;
#else
return pThunk->Init(proc, pThis);
#endif
}
void* GetCodeAddress()
{
#ifdef USE_ATL_THUNK2
return AtlThunk_DataToCode(pThunk);
#else
return pThunk->GetCodeAddress();
#endif
}
};
#pragma pack(pop)
typedef CDynamicStdCallThunk CStdCallThunk;
#else
typedef _stdcallthunk CStdCallThunk;
#endif // _M_IX86 || _M_X64 || _M_ARM
} // namespace ATL
#pragma pop_macro("new")
#endif // __ATLSTDTHUNK_H__
_stdcallthunk的更多相关文章
- ATL中窗口句柄与窗口过程的关联方法
ATL中采用了一种动态生成机器指令的方式进行窗口句柄与窗口对象进行关联,以是详细分析: CWindowImpl会在第一次调用Create时注册窗口类,该窗口类是的信息是在CWindowImpl的子类中 ...
- [IE编程] 多页面基于IE内核浏览器的代码示例
有不少人发信问这个问题,我把答案贴在这里: 建议参考 WTL (Windows Template Library) 的代码示例工程TabBrowser (在WTL目录/Samples/TabBrow ...
- ATL Thunk机制深入分析
如果你有SDK的编程经验,就一定应该知道在创建窗口时需要指定窗口类,窗口类中的一种重要的参数就是窗口过程.任何窗口接收到的消息,都是由该窗口过程来处理. 在面向对象编程中,如果还需要开发人员来使用原始 ...
- wtl学习总结
在windows平台,相比MFC,我更喜欢WTL,因其简洁漂亮.所以陆续花了一年的时间学习之,这里总结一下(在学习Wtl/Atl之前,最好是对WinApi编程有一定的了解). 安装 Wtl主页 htt ...
- ATL7窗口类详细剖析
前言: ATL是微软继MFC之后提供的一套C++模板类库,小巧.精妙.效率极高.它的主要作用是为我们编写COM/DOM/COM+程序提供了丰富的支持.但是ATL只能写COM么?我以前只是MFC程序员的 ...
- 深入解析Windows窗体创建和消息分发
Windows GUI採用基于事件驱动的编程模型,其实差点儿全部的界面库都是这样做的.在纯粹的Window32 SDK编程时代.人们还能够搞懂整个Windows窗口创建和消息的流通过程.可是在如今各种 ...
- 深入解析Windows窗口创建和消息分发(三个核心问题:怎么将不同的窗口过程勾到一起,将不同的hwnd消息分发给对应的CWnd类去处理,CWnd如何简单有效的去处理消息,由浅入深,非常清楚) good
笔记:争取不用看下面的内容,只看自己的笔记,就能记住这个流程,就算明白了: _tWinMain-->AfxWinMain,它调用四个函数: -->AfxWinInit用于做一些框架的初始化 ...
随机推荐
- 【python】udp 数据的发送和接收
import socket def send_message(): # 创建一个udp套接字 udp_socker = socket.socket(socket.AF_INET,socket.SOCK ...
- <choose><when><if>
--说明:choose类似于switch,其中的when如果不符合则进入otherwise(类似于default),可以结合if标签使用. -- <select> -- SELECT * ...
- Linux文件恢复利器 ext3grep与extundelete
介绍两款Linux文件恢复工具,ext3grep与extundelete,可能在关键时刻会有所帮助.ext3grep仅对ext3文件系统有效,extundelete对ext3与ext4文件系统都有效 ...
- AD中怎么将字体stroke全部改为truetype?
shift+F 智能查找功能,然后鼠标会变成十字状,你点一下你的这个字体会弹出如下: 一开始如果你没有改动过字体那应该都是同一总字体 在1的地方选择same,你点apply再点OK就可以把全部stro ...
- 聊聊ThreadLocal源码(基于JDK1.8)
原文:https://cloud.tencent.com/developer/article/1333298 聊聊JDK源码中ThreadLocal的实现 主要方法: ThreadLocal的get方 ...
- beta版本——第四次冲刺
第四次冲刺 (1)SCRUM部分☁️成员描述: 姓名 李星晨 完成了哪个任务 进行注册的时候若不输入手机号,提醒用户的是未输入登录名,进行更改 花了多少时间 1.2h 还剩余多少时间 1.8h 遇到什 ...
- keepalived+nginx+lnmp 网站架构
<网站架构演变技术研究> 项目实施手册 2019年8月2日 第一章: 实验环境确认 4 1.1-1.系统版本 4 1.1-2.内核参数 4 1.1-3.主机网络参数设置 4 1-1-4 ...
- Linux/Raspbian 每个目录用途说明
本文转自无聊小博,很多刚接触树莓派/Linux 的同学会在给树莓派安装.卸载.配置软件时,软件和配置文件等存放在哪儿产生疑惑.也会遇到诸如“磁盘分区”.U盘挂载等涉及到的目录路径问题.Linux 的目 ...
- python无法导入自己的模块的解决办法
- 六.深浅copy
先问问大家,什么是拷贝?拷贝是音译的词,其实他是从copy这个英文单词音译过来的,那什么是copy? copy其实就是复制一份,也就是所谓的抄一份.深浅copy其实就是完全复制一份,和部分复制一份的意 ...