I am playing with function hooking so I wrote simple program that calls
DisplayMessage Function which prints out Hello..

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'

#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_ *

dll.cpp

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

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

Recommended Answers

All 5 Replies

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?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

>>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

void PHook(void)
{
	cout << "Bye";
}
void (*fn)(void) = PHook;

void Hook()
{ 
	fn();	       
          
}

or this

void PHook(void)
{
	cout << "Bye";
}

void Hook(void (*fn)() )
{ 
	fn();	       
          
}

int main()
{
    Hook( PHook );
}

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.

you might search these links

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.