Typedef function pointer?

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:

 
fpLoadLibrary loadLibraryAddr = (fpLoadLibrary)GetProcAddress(hDLL, "LoadLibraryA");


Is there a way to avoid declaring that silly typedef?

Btw on a sidenote, why doesn't preview work on this forum?
Last edited on
Yes. Try writing it and you'll see why the typedef is a good idea :).

Seriously though, typedefs for function pointers are really helpful to make clear what something is.
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");
Last edited on
Apparently this does the same thing it seems
Now try calling it as a function through function pointer.
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. I think...
Last edited on
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.

Ask the compiler to figure out the type of the pointer returned by GetProcAddress().
auto: http://www.stroustrup.com/C++11FAQ.html#auto
decltype: http://www.stroustrup.com/C++11FAQ.html#decltype

Or use the type alias FARPROC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <windows.h>
#include <map>
#include <cstdint>

int main()
{
    const char* const lib_name = "kernel32.dll" ;
    const auto 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< const char*, decltype( GetProcAddress(nullptr,nullptr) )  > symbol_to_address_map ;
    // C++98: std::map< const char*, FARPROC > symbol_to_address_map ;

    const char* const close = "CloseHandle" ;
    const auto address = GetProcAddress( lib_base_address, close ) ;
    // C++98: FARPROC address = GetProcAddress( lib_base_address, close ) ;
    if(address) symbol_to_address_map[close] = address ;

    const char* 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( const auto& 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]
}

http://rextester.com/AYIA25579
Topic archived. No new replies allowed.