This article is due to the answer to a question in the csdn forum. I will sort it out to help my friends who have the same requirements.
If you have dynamically loaded the DLL, you should be clear about the getprocaddress API. Its function is to obtain the address of this function from the DLL module by using a function name, then convert it into the corresponding function pointer for our call. This is a typical example of using a function name to call a function. Next we will simulate this function. The main idea is to create a function ing table.
# Include <iostream>
Using namespace STD;
// Define the function entry Structure
Typedef void (* fun_ptr) (void );
Struct fun_entry
{
Const char * fun_name; // function name
Fun_ptr; // function pointer. In fact, the data type here can also be an integer.
};
// Defines two functions with different prototypes
Void foo1 () {cout <"foo1" <Endl ;}
Int foo2 (int I) {cout <"foo2:" <I <Endl; return 0 ;}
// Define the function ing table
Fun_entry fun_entry_table [] =
{
{"Foo1", (fun_ptr) foo1 },
{"Foo2", (fun_ptr) foo2}
};
// Simulate getprocaddress
Fun_ptr get_proc_address (const char * fun_name)
{
For (INT I = 0; I <sizeof (fun_entry_table)/sizeof (fun_entry_table [0]); I ++)
{
If (strcmp (fun_name, fun_entry_table [I]. fun_name) = 0)
Return fun_entry_table [I]. fun_ptr;
}
Return NULL;
}
Int main ()
{
Typedef void (* foo1_ptr) (void );
Typedef int (* foo2_ptr) (INT );
Foo1_ptr pfoo1 = (foo1_ptr) get_proc_address ("foo1"); // obtain the function entry address and convert it to a function pointer.
If (pfoo1) pfoo1 (); // call a function through a function pointer
Foo2_ptr pfoo2 = (foo2_ptr) get_proc_address ("foo2 ");
If (pfoo2) pfoo2 (100 );
System ("pause ");
Return 0;
}
Is it easy? How is getprocaddress implemented? I am not quite sure about this yet, but we know that the dll contains the function symbol information, so we can easily locate the function entry address. If the commonProgramBinaryCodeThere are also function names, which is not very nice! However, this exposes the internal implementation of the program, which can be very generous to hackers ,:).
Today is 9.18. Do not forget the national shame and revitalize China!
(Freefalcon at 9.18)