码迷,mamicode.com
首页 > 其他好文 > 详细

Detours的作用和实例

时间:2015-04-30 16:22:47      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:detours

Detours 可以用来拦截Win32的API函数,从而让程序按照我们自定义的方式进行处理,而不是Windows默认的。

Detours 也是通过Dll的方式,拦截Api函数。

例:拦截Win32的MessageBoxA()函数

1.先新建一个Dll工程

#include "detours.h"
#pragma comment(lib,"detours.lib") //导入detours.h和detours.lib文件


//static BOOL (WINAPI* Real_Messagebox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)=MessageBoxA;
int (WINAPI *Real_Messagebox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)= MessageBoxA; //注意是MessageBoxA函数
extern "C" _declspec(dllexport) BOOL WINAPI MessageBox_Mine(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
	CString temp= lpText;
	temp+="该MessageBox已被Detours截获!";
	return Real_Messagebox(hWnd,temp,lpCaption,uType);
}


//dll入口函数
BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
{
	if (DLL_PROCESS_ATTACH == fdwReason)
	{
		DetourTransactionBegin();
		DetourUpdateThread(GetCurrentThread()); //当前线程
		DetourAttach(&(PVOID&)Real_Messagebox,MessageBox_Mine); //设置detour
		DetourTransactionCommit();
	}
	else if (DLL_PROCESS_DETACH == fdwReason)
	{
		DetourTransactionBegin();
		DetourUpdateThread(GetCurrentThread());
		DetourDetach(&(PVOID&)Real_Messagebox,MessageBox_Mine);//卸载detour
		DetourTransactionCommit();
	}
	return TRUE;

}
特别要注意的就是MessageBox和MessageBoxA是不同的,我刚开始以为detours不会把它们区分,结果一直无法拦截成功。

2.新建一个MFC工程(在按扭响应函数中添加 MessageBoxA())

在其OnInitDialog()函数中 添加::LoadLibrary(L"messageDll.dll"); //注意路径问题

然后在按钮响应函数中添加 ::MessageBoxA(NULL,"4444443","23",0);

这样当成功后 点击按钮就可以拦截到MessageBox函数

效果如图:

技术分享



Detours的作用和实例

标签:detours

原文地址:http://blog.csdn.net/u013147600/article/details/45393847

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!