Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C++
Tip/Trick

Statically linking to EasyHook

Rate me:
Please Sign up or sign in to vote.
4.86/5 (5 votes)
31 Jul 2014CPOL 38K   9   8
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)


Written By
Team Leader
China China
Jerry is from China. He was captivated by computer programming since 13 years old when first time played with Q-Basic.



  • Windows / Linux & C++
  • iOS & Obj-C
  • .Net & C#
  • Flex/Flash & ActionScript
  • HTML / CSS / Javascript
  • Gaming Server programming / video, audio processing / image & graphics


Contact: vcer(at)qq.com
Chinese Blog: http://blog.csdn.net/wangjia184

Comments and Discussions

 
QuestionWhen i try to compile i always get some error Pin
tiktaktoe28-Nov-14 21:26
tiktaktoe28-Nov-14 21:26 
QuestionLicense Pin
bling1-Aug-14 10:05
bling1-Aug-14 10:05 
NewsApplication crash when calling LhUninstallAllHooks() Pin
Elmue13-Nov-12 17:38
Elmue13-Nov-12 17:38 
GeneralRe: Application crash when calling LhUninstallAllHooks() Pin
Jerry.Wang13-Nov-12 17:47
Jerry.Wang13-Nov-12 17:47 
QuestionStrange Pin
Elmue6-Nov-12 11:17
Elmue6-Nov-12 11:17 
AnswerRe: Strange Pin
Jerry.Wang6-Nov-12 13:02
Jerry.Wang6-Nov-12 13:02 
GeneralRe: Strange Pin
Elmue8-Nov-12 2:17
Elmue8-Nov-12 2:17 
GeneralRe: Strange Pin
Jerry.Wang8-Nov-12 2:53
Jerry.Wang8-Nov-12 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.