挂钩 winapi 函数显示我 dllimport 错误

Hooking winapi function show me dllimport error

本文关键字:dllimport 错误 显示 winapi 函数 挂钩      更新时间:2023-10-16

我正在尝试写一篇关于api hookingWinApi函数的文章。就像每个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导入函数相关的声明略有不同。

只需删除您的声明,错误就会消失...