Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My DLL stop to working after MH_Initialize #89

Open
luca2125 opened this issue Sep 26, 2020 · 7 comments
Open

My DLL stop to working after MH_Initialize #89

luca2125 opened this issue Sep 26, 2020 · 7 comments

Comments

@luca2125
Copy link

luca2125 commented Sep 26, 2020

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
Copy link

Nucleoprotein commented Sep 26, 2020

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
Copy link
Author

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
Copy link

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

@luca2125
Copy link
Author

luca2125 commented Jun 4, 2021

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

@lambwheit
Copy link

lambwheit commented Jun 4, 2021

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
Copy link

fligger commented Jul 23, 2021

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants