What is the purpose of this? And how does it work? I am currently writing a DLL injection using the WINAPI, but I stumble across being told having to apply the following code:
typedef HINSTANCE (*fpLoadLibrary)(char*);
When using the: "GetProcAddress" function the typedef is meant for:
The only way to call an explicitly mapped function from a library is via a pointer.
I do presume that the return type of GetProcAddress() is void*. It has to be cast to the actual type of the function that is in the DLL.
The loadLibraryAddr is a variable. A pointer. A function pointer. Pointer to a function that takes one char pointer as parameter and returns HINSTANCE (whatever that is).
That "silly thing" makes the actual code much simpler.
This is just a guess (probably wrong): auto loadLibraryAddr = reinterpret_cast<HINSTANCE (*)(char*)>(GetProcAddress(hDLL, "LoadLibraryA"));
That's what I would have to write without the typedef? No simpler than that?
Ok so is this a function I myself create without a body (it basically does nothing except from taking in a a paramater type?). Or is it actually a hidden function that is a part of a library but can only be called through a pointer?
Nvm, that's odd. Replacing fpLoadLibrary with simply void* type seems to work too, then I don't need to typedef it.
Apparently this does the same thing it seems: void* loadLibraryAddr = (void*)GetProcAddress(hDLL, "LoadLibraryA");
Wrong. If you don't have a need to use a function from the library, then you have no need to get its address either. If you have no need for any functions of the library, then you don't need that library at all.
> Is there a way to avoid declaring that silly typedef?
>> I don't need to use it as a function, I just need loadLibraryAddr to be capable of
>> storing the return value of GetProcAddress function.
#include <iostream>
#include <windows.h>
#include <map>
#include <cstdint>
int main()
{
constchar* const lib_name = "kernel32.dll" ;
constauto lib_base_address = GetModuleHandleA(lib_name) ;
std::cout << "base address of library '" << lib_name << "' is: " << lib_base_address << '\n' ;
// store the address of exported symbols without a cast
std::map< constchar*, decltype( GetProcAddress(nullptr,nullptr) ) > symbol_to_address_map ;
// C++98: std::map< const char*, FARPROC > symbol_to_address_map ;
constchar* const close = "CloseHandle" ;
constauto address = GetProcAddress( lib_base_address, close ) ;
// C++98: FARPROC address = GetProcAddress( lib_base_address, close ) ;
if(address) symbol_to_address_map[close] = address ;
constchar* const dup = "DuplicateHandle" ;
symbol_to_address_map[dup] = GetProcAddress( lib_base_address, dup ) ;
// print out the values of the addresses through std::cout (a cast is required)
for( constauto& pair : symbol_to_address_map )
std::cout << "symbol '" << pair.first << "' is at address: "
<< std::hex << std::showbase << std::uintptr_t(pair.second) << '\n' ;
// note about printing out the value of the pointer through std::cout
// std::uintptr_t is optional
// A pointer can be explicitly converted to any integral type large enough to hold it.
// The mapping function is implementation-defined.
// We are relying on this note in the standard: [Note: It is intended to be unsurprising
// to those who know the addressing structure of the underlying machine]
}