Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Statically linking to EasyHook

4.86/5 (5 votes)
31 Jul 2014CPOL 38.8K  
Instructions about linking EasyHook library statically

Easyhook is a powerful open source library from http://easyhook.codeplex.com/.

Compile EasyHook as static library 

To link with EasyHook statically, first open the Properties popup, change the Configuration Type to "Static library (.lib)"   

Image 1

Then, remove the "EASYHOOK_EXPORTS" preprocesser definition as below.   Image 2

Find DllMain, rename it to "EasyHookDllMain
Image 3

Open easyhook.h,  line 51, find the following code.

C++
#ifdef EASYHOOK_EXPORTS
    #define EASYHOOK_API						__declspec(dllexport) __stdcall
	#define DRIVER_SHARED_API(type, decl)		EXTERN_C type EASYHOOK_API decl
#else
    #ifndef DRIVER
        #define EASYHOOK_API					__declspec(dllimport) __stdcall
		#define DRIVER_SHARED_API(type, decl)	EXTERN_C type EASYHOOK_API decl
    #else
        #define EASYHOOK_API					__stdcall
		#define DRIVER_SHARED_API(type, decl)	typedef type EASYHOOK_API PROC_##decl; EXTERN_C type EASYHOOK_API decl
    #endif
#endif  

Replace them with

C++
#define EASYHOOK_API                    __stdcall
#define DRIVER_SHARED_API(type, decl)   typedef type EASYHOOK_API PROC_##decl; EXTERN_C type EASYHOOK_API decl 

And now you can compile the library.  

Use the static library  

Open the project which will use the generated static library above. 

Call EasyHookDllMain in your DllMain

C++
EASYHOOK_BOOL_EXPORT EasyHookDllMain( HMODULE hModule,
	DWORD  ul_reason_for_call,
	LPVOID lpReserved
	);


BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	EasyHookDllMain( hModule, ul_reason_for_call, lpReserved);

	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;

Link the libraries.

C++
#pragma comment( lib, "EasyHook32.lib") // or EasyHook64.lib, depending on your project
#pragma comment( lib, "Aux_ulib.lib")
#pragma comment( lib, "psapi.lib") 

Now try to compile your project, if you get some complication error like

 error LNK2026: module unsafe for SAFESEH image 

 You can try to switch the SAFESEH to NO as bellowImage 4 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)