挂钩 winapi 函数显示我 dllimport 错误
Hooking winapi function show me dllimport error
我正在尝试写一篇关于api hooking
和WinApi
函数的文章。就像每个Windows图形界面的核心功能一样SendMessage
我试图使用minhook
来捕获这些消息并将其显示在我的图形界面中。我做了一个简单的函数的api钩子,以通过以下方式了解有关minhook的更多信息:
#include "C:UsersAndroideDesktopminhookDynamicMinHook_133_srcincludeMinHook.h"//MHook header
#include <iostream>
#include <Commctrl.h>
#include <conio.h>//For getch
using namespace std;
typedef void (*WRITEFOOBAR)();//Typedef for the hooked function
static WRITEFOOBAR Basewritefoobar;//Backup of the originak fonction
void Writefoobar();//Original function
void Hookedwritefoobar();//Original function's redirection
static bool Hook();
template <typename T>
inline MH_STATUS MH_CreateHookEx(void* target, void* const base, T** original)
{
return MH_CreateHook(target, base, reinterpret_cast<void**>(original));
}
int main()
{
if (!Hook())//Hook "Writefoobar"
{
cout << "Hook failed" << endl;
return 1;
}
Writefoobar();//Standard call to Writefoobar, but instead, Hookedwritefoobar will be executed
cout << "Press a key to exit" << endl;
_getch();
return 0;
}
bool Hook()
{
if (MH_Initialize() != MH_OK)
{
return false;
}
if (MH_CreateHookEx((void*)&Writefoobar, (void*)&Hookedwritefoobar, &Basewritefoobar) != MH_OK)
{
return FALSE;
}
return MH_EnableHook((void*)&Writefoobar) == MH_OK;
}
void Writefoobar()
{
cout << "foobar" << endl;
}
void Hookedwritefoobar()
{
cout << "BLUESCREEN Software" << endl;
}
这个例子很好用。但现在我试图捕获SendMessage
Winapi函数。所以我试图在我的工作示例中做一个类似的例子。示例如下:
#include "C:UsersAndroideDesktopminhookDynamicMinHook_133_srcincludeMinHook.h"//MHook header
#include <iostream>
#include <windows.h>
#include <Commctrl.h>
#include <conio.h>//For getch
using namespace std;
typedef void (*SENDMESSAGEW)();//Typedef for the hooked function
static SENDMESSAGEW Basewritefoobar;//Backup of the originak fonction
LRESULT WINAPI SendMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
static const wchar_t *hiddenprocess=L"tusitio";
LRESULT WINAPI BSSSendMessageW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if ( msg == LVM_INSERTITEMW || msg == LVM_SETITEMW)//Intercepts LVM_INSERTITEM and LVM_SETITEM messages
{
if (!lstrcmpW(((LVITEMW*)lparam)->pszText, hiddenprocess))//The lparam is a LVITEM* struct.
{
return 0;//If the item name is the same as process we want to hide, we simply return 0 (and we do not call the real SendMessage function.
}
return 0;
}
return SendMessage(hwnd, msg, wparam, lparam);//Calls the real SendMessage function.
}
static bool Hook();
template <typename T>
inline MH_STATUS MH_CreateHookEx(void* target, void* const base, T** original)
{
return MH_CreateHook(target, base, reinterpret_cast<void**>(original));
}
int main()
{
if (!Hook())//Hook "Writefoobar"
{
cout << "Hook failed" << endl;
return 1;
}
cout << "Press a key to exit" << endl;
_getch();
return 0;
}
bool Hook()
{
if (MH_Initialize() != MH_OK)
{
return false;
}
if (MH_CreateHookEx((void*)&SendMessageW, (void*)&BSSSendMessageW, &Basewritefoobar) != MH_OK)
{
return FALSE;
}
return MH_EnableHook((void*)&SendMessageW) == MH_OK;
}
但是似乎没有编译好。我收到以下错误:
$ gcc -o dllmain.o -c Dynamic.cpp Dynamic.cpp:11:16: warning: 'LRESULT SendMessageW(HWND, UINT, WPARAM, LPARAM)' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
LRESULT WINAPI SendMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
^~~~~~~~
我的dll试图钩住发送消息:
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "C:UsersAndroideDesktopminhookDynamicMinHook_133_srcincludeMinHook.h"//MHook header
#include <iostream>
#include <windows.h>
#include <Commctrl.h>
#include <conio.h>
using namespace std;
typedef void (*SENDMESSAGEW)();//Typedef for the hooked function
static SENDMESSAGEW Basewritefoobar;//Backup of the originak fonction
LRESULT WINAPI SendMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
static const wchar_t *hiddenprocess=L"tusitio";
LRESULT WINAPI BSSSendMessageW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if ( msg == LVM_INSERTITEMW || msg == LVM_SETITEMW)//Intercepts LVM_INSERTITEM and LVM_SETITEM messages
{
if (!lstrcmpW(((LVITEMW*)lparam)->pszText, hiddenprocess))//The lparam is a LVITEM* struct.
{
return 0;//If the item name is the same as process we want to hide, we simply return 0 (and we do not call the real SendMessage function.
}
return 0;
}
return SendMessage(hwnd, msg, wparam, lparam);//Calls the real SendMessage function.
}
static bool Hook();
template <typename T>
inline MH_STATUS MH_CreateHookEx(void* target, void* const base, T** original)
{
return MH_CreateHook(target, base, reinterpret_cast<void**>(original));
}
extern "C" __declspec (dllexport) void __cdecl SendWrite()
{
//SendWrite Call
}
BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
//Different behaviors depending on the reason why DllMain is called
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
if (!Hook())//Hook "Writefoobar"
{
cout << "Hook failed" << endl;
return 1;
}
//SendMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
bool Hook()
{
if (MH_Initialize() != MH_OK)
{
return false;
}
if (MH_CreateHookEx((void*)&SendMessageW, (void*)&BSSSendMessageW, &Basewritefoobar) != MH_OK)
{
return FALSE;
}
return MH_EnableHook((void*)&SendMessageW) == MH_OK;
}
目前我编译我的dll似乎不能很好地编译:
$ gcc -o dllmain.o -c dllmain.cpp
dllmain.cpp:14:16: warning: 'LRESULT SendMessageW(HWND, UINT, WPARAM, LPARAM)' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
LRESULT WINAPI SendMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
一般来说,我正在尝试制作一个 dll 挂钩sendmessage
winapi 函数的最小示例。我为什么要这样做?为了研究,我从那里得到数据:
挂钩发送消息
问题是这一行:
LRESULT WINAPI SendMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
此函数已由 #include <windows.h>
声明,但与如何从.DLL导入函数相关的声明略有不同。
只需删除您的声明,错误就会消失...
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- VC 链接器错误使用Dllimport/DLLEXPORT宏将标题包括在多个项目中
- 挂钩 winapi 函数显示我 dllimport 错误
- 错误 C2491:不允许定义 dllimport 函数
- 错误 LNK2001:未解析的外部符号"__declspec(dllimport) public: class QString & __thiscall QString::operator=(clas
- dllimport将一个c++方法移植到c#.当从C#调用时,程序关闭,没有任何错误消息