TsudaKageyu / minhook

The Minimalistic x86/x64 API Hooking Library for Windows
http://www.codeproject.com/KB/winsdk/LibMinHook.aspx
Other
4.32k stars 886 forks source link

My DLL stop to working after MH_Initialize #89

Open luca2125 opened 4 years ago

luca2125 commented 4 years ago

Hi,

I have a full working project with ms detours and becouse I like to switch to MinHook I have replace detours code with MinHook equivalent.

I use VS2017 v141 SDK 8.1. To do the test I used "MinHook.x86.lib" v 1.3.3 in this repository (not compiled by me).

This is my reduced test code:

#include <windows.h>
 #include "MinHook.h"
#pragma comment(lib, "MinHook.x86.lib")

bool WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
    int FFBInitDelayInMillliseconds;

    switch (dwReason)
    {
    case DLL_PROCESS_ATTACH:
        // Load dll

        MessageBoxA(NULL, "Test1", "Test2", 0);

         if (MH_Initialize() != MH_OK)
         {
            LF::Log_Update(LOG_FLAG_ERROR, "Hook initialize failed.");
            return false;
         }

        break;

    case DLL_PROCESS_DETACH:

        // Close the DLL

        LF::Log_Update("Closing session ...");

        // unhook
        /*if (MH_DisableHook(MH_ALL_HOOKS) != MH_OK)
            LF::Log_Update(LOG_FLAG_ERROR, "Warning fail to disable hooks");

        if (MH_RemoveHook(MH_ALL_HOOKS) != MH_OK)
            LF::Log_Update(LOG_FLAG_ERROR, "Warning fail to remove hooks");

        if (MH_Uninitialize() != MH_OK)
            LF::Log_Update(LOG_FLAG_ERROR, "Warning fail to uninitialize hooks");*/

        if (Config.ForceOff == false && Local_InitFFBComplete) FFB->ClearAll();

        LF::Log_Update("Session closed.");

        break;
    }

    return true;

For some strange reason my project don't do nothing if I add this line:

     if (MH_Initialize() != MH_OK)
         {
            LF::Log_Update(LOG_FLAG_ERROR, "Hook initialize failed.");
            return false;
         }

if I remove this work normally.

I attach my complete project.

BattleZone Redux (MinHook).zip

and my setting:

minhook

Can you help me please ?

Thank you !!

Nucleoprotein commented 4 years ago

I think this is not related to MinHook, it's just deadlock in DllMain because it's useless for anything than simple variables initialization: https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices

luca2125 commented 4 years ago

Thank you for share !!

Do you have an easy example of source code in DllMain with MinHook that work ?

My source code originally work fine with ms detours, but not with MinHook.

Thanks !

lambwheit commented 3 years ago

@luca2125 did you manage to find a way to initialize minhook in your dll? having the same issues as you

luca2125 commented 3 years ago

For the moment I use ms detours that probably is not best solution, but In my case work.

lambwheit commented 3 years ago

i got mine to work by adding libMinHook-x86-v140-mtd.lib to the additional dependencies in the linker option and not using #pragma comment(lib, "MinHook.x86.lib")

image

fligger commented 3 years ago

Check if your project has generated another DLL file. (minhook.x64d.dll)

image

When i use MH_Initialize im my code it will require that minhook.x64d.dll will be injected first so my original ClientBaseWindow.dll is able to load.

Sadly i don't know how to fix it. send help

Nucleoprotein commented 3 years ago

You have two project files: MinHook and libMinHook - use the second one in your solution and add reference to libMinHook in your project then Visual Studio will automatically build and link MinHook when you build your project.

libMinHook should be configured to output static library, so your project should not need any other dll files