943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 959
  • C++ RSS
Jul 18th, 2009
0

function hook problem

Expand Post »
I am playing with function hooking so I wrote simple program that calls
DisplayMessage Function which prints out Hello..
C++ Syntax (Toggle Plain Text)
  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'
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
Jul 18th, 2009
0

Re: function hook problem

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 193
Solved Threads: 75
Posting Pro in Training
thelamb is offline Offline
426 posts
since Aug 2008
Jul 18th, 2009
0

Re: function hook problem

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
Jul 18th, 2009
0

Re: function hook problem

>>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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jul 18th, 2009
0

Re: function hook problem

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
Jul 18th, 2009
0

Re: function hook problem

you might search these links
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: CreateFile on system volume
Next Thread in C++ Forum Timeline: C++ to Lan Cable/Port





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC