C++ MinHook function hooking - error: invalid conversion

Refresh

April 2019

Views

257 time

1

I am trying to use the MinHook library, but when I try to compile the example code I get the following error:

E:\Dev\C++\detour\main.cpp:50:98: error: invalid conversion from 'int (__attribute__((__stdcall__)) *)(HWND, LPCWSTR, LPCWSTR, UINT) {aka int (__attribute__((__stdcall__)) *)(HWND__*, const wchar_t*, const wchar_t*, unsigned int)}' to 'LPVOID {aka void*}' [-fpermissive]
 if (MH_CreateHook(&MessageBoxW, &DetourMessageBoxW, reinterpret_cast<LPVOID*>(&fpMessageBoxW)) != MH_OK)

Line 50 is:

if (MH_CreateHook(&MessageBoxW, &DetourMessageBoxW, reinterpret_cast<LPVOID*>(&fpMessageBoxW)) != MH_OK)

Complete main.cpp

#include <Windows.h>
#include "MinHook.h"

#if defined _M_X64
#pragma comment(lib, "libMinHook.x64.lib")
#elif defined _M_IX86
#pragma comment(lib, "libMinHook.x86.lib")
#endif

typedef int (WINAPI *MESSAGEBOXW)(HWND, LPCWSTR, LPCWSTR, UINT);

// Pointer for calling original MessageBoxW.
MESSAGEBOXW fpMessageBoxW = NULL;

// Detour function which overrides MessageBoxW.
int WINAPI DetourMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
{
    return fpMessageBoxW(hWnd, L"Hooked!", lpCaption, uType);
}

int main()
{
    // Initialize MinHook.
    if (MH_Initialize() != MH_OK)
    {
        return 1;
    }

    // Create a hook for MessageBoxW, in disabled state.
    if (MH_CreateHook(&MessageBoxW, &DetourMessageBoxW, reinterpret_cast<LPVOID*>(&fpMessageBoxW)) != MH_OK)
    {
        return 1;
    }

    // or you can use the new helper function like this.
    //if (MH_CreateHookApiEx(
    //    L"user32", "MessageBoxW", &DetourMessageBoxW, &fpMessageBoxW) != MH_OK)
    //{
    //    return 1;
    //}

    // Enable the hook for MessageBoxW.
    if (MH_EnableHook(&MessageBoxW) != MH_OK)
    {
        return 1;
    }

    // Expected to tell "Hooked!".
    MessageBoxW(NULL, L"Not hooked...", L"MinHook Sample", MB_OK);

    // Disable the hook for MessageBoxW.
    if (MH_DisableHook(&MessageBoxW) != MH_OK)
    {
        return 1;
    }

    // Expected to tell "Not hooked...".
    MessageBoxW(NULL, L"Not hooked...", L"MinHook Sample", MB_OK);

    // Uninitialize MinHook.
    if (MH_Uninitialize() != MH_OK)
    {
        return 1;
    }

    return 0;
}

Here is my CMakeLists.txt file:

cmake_minimum_required(VERSION 3.9)
project(detour)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -static-libgcc -static-libstdc++")

include_directories(hde)

file(GLOB SOURCES
        ${detour_SOURCE_DIR}/*.cpp
        ${detour_SOURCE_DIR}/*.c
        ${detour_SOURCE_DIR}/*.h
        ${detour_SOURCE_DIR}/hde/*.c
        ${detour_SOURCE_DIR}/hde/*.h)

set(SOURCE_FILES ${SOURCES})

add_executable(detour ${SOURCE_FILES})

Do I miss something really simple? It would be cool if anyone could help me with this.

0 answers