| | |
function hook problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2007
Posts: 69
Reputation:
Solved Threads: 1
I am playing with function hooking so I wrote simple program that calls
DisplayMessage Function which prints out Hello..
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'
dll.cpp
its wont compile no idea why
heres the errors
it give compile errors
DisplayMessage Function which prints out Hello..
C++ Syntax (Toggle Plain Text)
void DisplayMessage() { cout << "Hello"; }
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)
#ifndef _DLL_H_ #define _DLL_H_ #if BUILDING_DLL # define DLLIMPORT __declspec (dllexport) #else /* Not BUILDING_DLL */ # define DLLIMPORT __declspec (dllimport) #endif /* Not BUILDING_DLL */ DLLIMPORT void Hook(void); #endif /* _DLL_H_ *
C++ Syntax (Toggle Plain Text)
void (*PHook)(void); PHook = (void*)(0x00131000); PHook(void) { cout << "Bye"; } void Hook() { PHook(); } BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ , DWORD reason /* Reason this function is being called. */ , LPVOID reserved /* Not used. */ ) { switch (reason) { case DLL_PROCESS_ATTACH: Hook(); break; case DLL_PROCESS_DETACH: break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; } /* Returns TRUE on success, FALSE on failure */ return TRUE; }
its wont compile no idea why
heres the errors
C++ Syntax (Toggle Plain Text)
mpiling... 1>dllmain.cpp 1>.\dllmain.cpp(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>.\dllmain.cpp(11) : error C2373: 'PHook' : redefinition; different type modifiers 1> .\dllmain.cpp(10) : see declaration of 'PHook' 1>.\dllmain.cpp(11) : error C2440: 'initializing' : cannot convert from 'void *' to 'int' 1> There is no context in which this conversion is possible 1>.\dllmain.cpp(13) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>.\dllmain.cpp(13) : error C2365: 'PHook' : redefinition; previous definition was 'data variable' 1> .\dllmain.cpp(10) : see declaration of 'PHook' 1>.\dllmain.cpp(15) : warning C4508: 'PHook' : function should return a value; 'void' return type assumed 1>.\dllmain.cpp(18) : warning C4273: 'Hook' : inconsistent dll linkage 1> c:\users\zippo\documents\visual studio 2008\projects\dynamic link libary\dynamic link libary\dll.h(10) : see previous definition of 'Hook' 1>.\dllmain.cpp(19) : error C3861: 'PHook': identifier not found
it give compile errors
•
•
Join Date: Aug 2008
Posts: 37
Reputation:
Solved Threads: 7
C++ Syntax (Toggle Plain Text)
void (*PHook)(void); // 1 PHook = (void*)(0x00131000); // 2 PHook(void) // 3 { cout << "Bye"; }
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.
>>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
or this
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)
void PHook(void) { cout << "Bye"; } void (*fn)(void) = PHook; void Hook() { fn(); }
or this
C++ Syntax (Toggle Plain Text)
void PHook(void) { cout << "Bye"; } void Hook(void (*fn)() ) { fn(); } int main() { Hook( PHook ); }
Last edited by Ancient Dragon; Jul 18th, 2009 at 12:59 pm.
•
•
Join Date: May 2007
Posts: 69
Reputation:
Solved Threads: 1
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.
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.
![]() |
Similar Threads
- Problem while using function from loaded dll (C++)
- Function call problem (C++)
- Constructor function call problem (C++)
- function calling problem (C)
- Function call problem (HTML and CSS)
- repost, function alogorithm problem (C)
Other Threads in the C++ Forum
- Previous Thread: CreateFile on system volume
- Next Thread: C++ to Lan Cable/Port
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






