我想修改Windows API中可用的本机函数,例如CreateWindowEx或ShowWindow,以便在编译包含这些函数的应用程序时,它将调用我的函数,在那里执行任务,然后调用原始的本机函数。
换句话说,我想以某种方式代理函数,同时仍然使用相同的名称(因此,如果一个程序是用本机API编写的,那么简单地添加这些函数就会修改处理这些本机函数的方式)。
HWND WINAPI CreateWindowEx(
__in DWORD dwExStyle,
__in_opt LPCTSTR lpClassName,
__in_opt LPCTSTR lpWindowName,
__in DWORD dwStyle,
__in int x,
__in int y,
__in int nWidth,
__in int nHeight,
__in_opt HWND hWndParent,
__in_opt HMENU hMenu,
__in_opt HINSTANCE hInstance,
__in_opt LPVOID lpParam
) {
//my custom code here....
// done with my custom code... so now I want to run the native function
return CreateWindowEx(dwExStyle, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
}这(由于显而易见的原因)会导致堆栈溢出,因为它会一遍又一遍地调用自己。我想要它做的是,当它被调用时,它会运行我创建的自定义函数,然后运行Windows API中可用的本机函数。
我对c++非常陌生,但例如在许多其他语言中,我可以用另一个名称存储对本机函数的引用,然后我可以在我的自定义函数中调用它。在c++中有没有类似的东西?
发布于 2011-10-15 00:24:18
正如我在评论中所写的,许多钩子库的父函数可能是微软的Detours
现在它不再是免费的了,有各种各样的选择。这里有他们中的一些比较(链接删除。我不确定它是不是安全。试着在谷歌上搜索"Microsoft Detour是一个在特定拦截中使用的库“,并选择一个或更简单的代用资源。
嗯,看起来现在唯一免费的选择是http://easyhook.codeplex.com/和http://www.codeproject.com/KB/system/mini_hook_engine.aspx
这里有一个问题:Detours alternative for Registry interception,如果你感兴趣的话。
发布于 2011-10-15 00:30:14
对您的问题的一种解释是,您有一个包含源代码的项目,并且您希望更改该项目,以便它使用您自己的某些winapi函数版本。
以下是您可以为每个导入的API函数实现的解决方案。下面是针对ShowWindow的示例
#define ShowWindow Deleted_Winapi_ShowWindow // prevent windows.h from defining ShowWindow
#include <windows.h>
#undef ShowWindow
namespace HiddenWinapi
{
extern "C"
{
// Do what windows.h does, but hide it inside a namespace.
WINUSERAPI BOOL WINAPI ShowWindow( __in HWND hWnd, __in int nCmdShow);
}
}
// make your own function to be called instead of the API, and delegate to the actual API in the namespace.
BOOL WINAPI ShowWindow(HWND hwnd, int nCmdShow)
{
// ... do stuff ...
// call the original API
return HiddenWinapi::ShowWindow(hwnd, nCmdShow);
}要将此解决方案用于CreateWindowEx,您需要将实际导入的函数名(例如CreateWindowExW)存根,因为CreateWindowEx只是一个扩展为CreateWindowExW或CreateWindowExA的宏。
这里有一个解决方案,可以用你自己的宏替换,但我认为在所有情况下,使用上面的解决方案都会更好。
#include <windows.h>
#undef CreateWindowEx
// Note that this is a unicode-only version. If your app mixes A and W versions, see
// the solution below for non-macro APIs.
HWND WINAPI CreateWindowEx(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{
// ... do stuff ...
// call the REAL function.
return CreateWindowExW(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
}发布于 2011-10-15 00:22:38
如果您想自己这样做,最简单的方法是修改PE (可移植可执行文件)标头中的导入地址表。然而,这并不是微不足道的。
然而,我相信有一个标准库可以满足你的需要,叫做Detour。我自己从来没有用过它,因为当我开始做这件事的时候,它还没有出现,所以我有一个库--不是供公众使用的--当我需要的时候,它可以通过导入表来完成。
https://stackoverflow.com/questions/7770520
复制相似问题