function hook problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

function hook problem

 
0
  #1
Jul 18th, 2009
I am playing with function hooking so I wrote simple program that calls
DisplayMessage Function which prints out Hello..
  1. void DisplayMessage()
  2. {
  3. cout << "Hello";
  4. }

After this one i found the DisplayMessage offset its (0x00131000)
now my target is write a dll to change DisplayMessage function to say bye instead of hello....

and here comes the problem...
DLL code
dll.h'
  1. #ifndef _DLL_H_
  2. #define _DLL_H_
  3.  
  4. #if BUILDING_DLL
  5. # define DLLIMPORT __declspec (dllexport)
  6. #else /* Not BUILDING_DLL */
  7. # define DLLIMPORT __declspec (dllimport)
  8. #endif /* Not BUILDING_DLL */
  9.  
  10. DLLIMPORT void Hook(void);
  11.  
  12. #endif /* _DLL_H_ *
dll.cpp
  1.  
  2. void (*PHook)(void);
  3. PHook = (void*)(0x00131000);
  4. PHook(void)
  5. {
  6. cout << "Bye";
  7. }
  8.  
  9. void Hook()
  10. {
  11. PHook();
  12.  
  13. }
  14.  
  15.  
  16. BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
  17. DWORD reason /* Reason this function is being called. */ ,
  18. LPVOID reserved /* Not used. */ )
  19. {
  20. switch (reason)
  21. {
  22. case DLL_PROCESS_ATTACH:
  23. Hook();
  24. break;
  25.  
  26. case DLL_PROCESS_DETACH:
  27.  
  28. break;
  29.  
  30. case DLL_THREAD_ATTACH:
  31.  
  32. break;
  33.  
  34. case DLL_THREAD_DETACH:
  35.  
  36. break;
  37.  
  38. }
  39. /* Returns TRUE on success, FALSE on failure */
  40. return TRUE;
  41. }

its wont compile no idea why
heres the errors
  1. mpiling...
  2. 1>dllmain.cpp
  3. 1>.\dllmain.cpp(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  4. 1>.\dllmain.cpp(11) : error C2373: 'PHook' : redefinition; different type modifiers
  5. 1> .\dllmain.cpp(10) : see declaration of 'PHook'
  6. 1>.\dllmain.cpp(11) : error C2440: 'initializing' : cannot convert from 'void *' to 'int'
  7. 1> There is no context in which this conversion is possible
  8. 1>.\dllmain.cpp(13) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  9. 1>.\dllmain.cpp(13) : error C2365: 'PHook' : redefinition; previous definition was 'data variable'
  10. 1> .\dllmain.cpp(10) : see declaration of 'PHook'
  11. 1>.\dllmain.cpp(15) : warning C4508: 'PHook' : function should return a value; 'void' return type assumed
  12. 1>.\dllmain.cpp(18) : warning C4273: 'Hook' : inconsistent dll linkage
  13. 1> c:\users\zippo\documents\visual studio 2008\projects\dynamic link libary\dynamic link libary\dll.h(10) : see previous definition of 'Hook'
  14. 1>.\dllmain.cpp(19) : error C3861: 'PHook': identifier not found


it give compile errors
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 37
Reputation: thelamb is on a distinguished road 
Solved Threads: 7
thelamb thelamb is offline Offline
Light Poster

Re: function hook problem

 
0
  #2
Jul 18th, 2009
  1. void (*PHook)(void); // 1
  2. PHook = (void*)(0x00131000); // 2
  3. PHook(void) // 3
  4. {
  5. cout << "Bye";
  6. }

The first error is on line //3, you forget the return type of the PHook function.

But all-together what you are doing isn't going to work out, if you're looking at some easy way to hook have a look at Microsoft's Detours library, it provides an easy way for you to hook your functions.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: function hook problem

 
0
  #3
Jul 18th, 2009
can you explain why it wont work?because i already hooked regular variables this way
im totaly new to it..
and yes i heard about detours libary but is there anyway to do it without it? because i really want to understand how it works
Last edited by laconstantine; Jul 18th, 2009 at 12:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,440
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: function hook problem

 
0
  #4
Jul 18th, 2009
>>can you explain why it wont work?

It won't work because the addresses will change every time the program is run. Windows hook functions work because you pass it a pointer to the function, not the actually address value.

This should work
  1. void PHook(void)
  2. {
  3. cout << "Bye";
  4. }
  5. void (*fn)(void) = PHook;
  6.  
  7. void Hook()
  8. {
  9. fn();
  10.  
  11. }

or this
  1. void PHook(void)
  2. {
  3. cout << "Bye";
  4. }
  5.  
  6. void Hook(void (*fn)() )
  7. {
  8. fn();
  9.  
  10. }
  11.  
  12. int main()
  13. {
  14. Hook( PHook );
  15. }
Last edited by Ancient Dragon; Jul 18th, 2009 at 12:59 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: function hook problem

 
0
  #5
Jul 18th, 2009
You didnt get my point right I wana change function of a program by injecting a dll into it.

for example we have 2 progs.
1 is .exe which has function DisplayMessage and main funcs call it..
2.a dynamic link libary

now i want that my dll will have a function pointer which points to .exe function DisplayMessage and then change it.

and the final step inject the dll into .exe

is it possible? without detours libary.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,440
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: function hook problem

 
0
  #6
Jul 18th, 2009
you might search these links
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC