build mhook using mingw

mhook is a good library to set up Windows API hook. However it is developed under VC. You can Download the source code of mhook   and uncompress the zip package, you will find the VC project files:mhook-test.sln and mhook-test.vcxproj. You can open it in visual studio and build the test program easily.

But how to build mhook with Mingw? There need a little transplant work. You may come up with the following compiling command:

g++ mhook-lib\mhook.cpp disasm-lib\disasm.c disasm-lib\disasm_x86.c  disasm-lib\cpu.c mhook-test.cpp  -o mhooktest -lGdi32  -w -Wfatal-errors

But it brings you the following compiling error:

mhook-lib\mhook.cpp: In function ‘void odprintf(PCSTR, …)’:
mhook-lib\mhook.cpp:59:43: error: ‘vsprintf_s’ was not declared in this scope
len = vsprintf_s(buf, len, format, args);

vsprintf_s is VC stuff, and there is no such a function in mingw. You should add the following lines in mhook.cpp:

#ifdef __GNUC__
# define vsprintf_s vsnprintf
# define vswprintf_s vsnwprintf
# ifndef SecureZeroMemory
#  define SecureZeroMemory(p,s) RtlFillMemory((p),(s),0)
# endif
#endif

Then you will get the following error:

mhook-lib\mhook.cpp: In function ‘void odprintf(PCSTR, …)’:
mhook-lib\mhook.cpp:69:37: error: ‘isspace’ was not declared in this scope
while (len && isspace(buf[len-1])) len–;

isspace function is declared in cctype header file which you should include in mhook.cpp.

Now the compiler complains:

mhook-lib\mhook.cpp:171:126: error: cannot convert ‘const wchar_t*’ to ‘LPCSTR {aka const char*}’ for argument ‘1’ to ‘HINSTANCE__* GetModuleHandleA(LPCSTR)’
_CreateToolhelp32Snapshot fnCreateToolhelp32Snapshot = (_CreateToolhelp32Snapsh
ot) GetProcAddress(GetModuleHandle(L”kernel32″), “CreateToolhelp32Snapshot”);

Since you did not define UNICODE, you should use the wide char version of GetModuleHandle. Now change all GetModuleHandle to GetModuleHandleW.

Now comes the following error:

mhook-lib\mhook.cpp: In function ‘void* SuspendOneThread(DWORD, PBYTE, DWORD)’:
mhook-lib\mhook.cpp:375:66: error: ‘OpenThread’ was not declared in this scope
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, dwThreadId);

This error is a little weird. If you search msdn for OpenThread, you are told to include windows.h. But you actually have already included that header file in mhook.cpp. Why does it complain OpenThread is not declared? This is because in the header file, OpenThread is conditionally declared according to a defined constant _WIN32_WINNT. Only if the value of the _WIN32_WINNT>=0x0500 should OpenThread be declared. So change the build command to:

g++ mhook-lib\mhook.cpp disasm-lib\disasm.c disasm-lib\disasm_x86.c  disasm-lib\cpu.c mhook-test.cpp  -o mhooktest -lGdi32  -D_WIN32_WINNT=0x0502 -w -Wfatal-errors

Then comes the following error:

disasm-lib\disasm.c: In function ‘ARCHITECTURE_FORMAT* GetArchitectureFormat(ARCHITECTURE_TYPE)’:
disasm-lib\disasm.c:111:73: error: ‘ARCHITECTURE_FORMAT* GetArchitectureFormat(ARCHITECTURE_TYPE)’ was declared ‘extern’ and later ‘static’ [-fpermissive] static ARCHITECTURE_FORMAT *GetArchitectureFormat(ARCHITECTURE_TYPE Type)

Just adding “static” before the declaration of GetArchitectureFormat can resolve this problem.

The next error is:

disasm-lib\disasm_x86.c: In function ‘BOOL X86_GetInstruction(INSTRUCTION*, U8*,
U32)’:
disasm-lib\disasm_x86.c:1631:20: error: invalid conversion from ‘long unsigned i
nt’ to ‘INSTRUCTION_TYPE {aka _INSTRUCTION_TYPE}’ [-fpermissive]
Instruction->Type |= X86_GET_TYPE(X86Opcode);

You should either explicitly do the type conversions in  disasm_x86.c, or use the g++ option -fpermissive to enable implicit type conversion for g++. Since there are too many places to modify, we use the -fpermissive option to permit implicit type conversion.

The next error is:

mhook-test.cpp:178: undefined reference to `WSAStartup@8′

Adding -lws2_32 to the build command line will resolve this problem.

The last error is:

libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16′
collect2.exe: error: ld returned 1 exit status

Changing wmain(…) in mhook-test.cpp to main(…) will get rid of this error.

Now we have successfully ported the mhook from VC to mingw.

 

Comments are closed, but trackbacks and pingbacks are open.