TypeDef with Function Pointer: Function does not Exist

Laurie Stearn

The problem is running code on an older machine where a requested function does not exist. To check for it one uses LoadLibrary and GetProcAddress as demonstrated here, but GetProcAddress requires the address of the functions in a TypeDef prior to its use.
For example, take these two on e.g, XP SP2 32 bit:

typedef BOOL (__stdcall *LPFN_Wow64RevertWow64FsRedirection) (PVOID OldValue);
typedef BOOL (__stdcall *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
...

...
LPFN_Wow64RevertWow64FsRedirection wowRevert = NULL;
LPFN_Wow64DisableWow64FsRedirection wowDisable = NULL;
HINSTANCE hLib;
if(GetProcAddresses( &hLib, "kernel32.dll", 2, &wowRevert,_ 
"Wow64RevertWow64FsRedirection", &wowDisable, Wow64DisableWow64FsRedirection" ))
{...

Code crashes here with:

The procedure entry point Wow64RevertWow64FsRedirection could not be located in the dynamic link library Kernel32.dll

It's easy enough to implement our own custom Wow64RevertWow64FsRedirection with non-WINAPI typedefs, but how can they be replaced with the base type when the function exists in kernel32.dll?

Cody Gray

I'm having a bit of trouble understanding your question. The Wow64RevertWow64FsRedirection function is obviously not going to exist on a 32-bit operating system, so it won't exist on 32-bit Windows XP. Therefore, attempting to retrieve a pointer to this function with GetProcAddress will fail. You get the sensible error that this entry point could not be found. If the entry point cannot be found, the function does not exist and you should not attempt to call it.

You claim that you can implement your own custom Wow64RevertWow64FsRedirection function, but I haven't the foggiest idea why you would want to do this. If the operating system supports WOW64 file-system redirection, then it will provide the Wow64RevertWow64FsRedirection function. If it does not, then it does not provide the function, but you do not need such a function, because there is no such thing as WOW64 file-system redirection. You don't need to enable, disable, or revert it.

It seems that you are making this far more complicated than it needs to be. You don't even need to first verify that the process is a 64-bit process. You can just attempt to locate the entry point to Wow64RevertWow64FsRedirection (or Wow64DisableWow64FsRedirection, as needed), call it if it exists, or ignore the failure if it does not exist.

It is as simple as:

BOOL RevertWOW64RedirectionIfNecessary(PVOID pOldValue)
{
    typedef BOOL (WINAPI * fnWow64RevertWow64FsRedirection)(PVOID);

    fnWow64RevertWow64FsRedirection pfn =
        reinterpret_cast<fnWow64RevertWow64FsRedirection>(
           reinterpret_cast<void*>(
           GetProcAddress(GetModuleHandle(L"kernel32"),
                          "Wow64RevertWow64FsRedirection")));

    if (pfn)
    {
        // The function exists, so call it through the pointer we obtained.
        return pfn(pOldValue);
    }
    else
    {
        // The function does not exist, so we can't call it.
        // But we don't ever need to call it in such cases,
        // so do nothing and feign success.
        return TRUE;
    }
}

Note that I am calling the GetModuleHandle function to retrieve a handle to the module kernel32.dll (the .dll extension is implied). I can use GetModuleHandle here instead of LoadModule because I know that kernel32.dll is guaranteed to always be loaded in any application's process. And since I've used GetModuleHandle, I don't need to free the module handle, either.

I pass the resulting handle to the GetProcAddress function, along with a string that contains the name of the function/procedure whose address is to be retrieved. This function attempts to retrieve the address of that function, and returns it if it exists; otherwise, it fails and returns NULL.

I check to see if it returned a valid pointer, and if so, I call the function dynamically through that pointer. Otherwise, it returned NULL, meaning that the function is not available, but in that case, we don't even need to worry about it, so the code just becomes a no-op.

As for the funny casting, see my answer here, which explains this trick.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

Typedef function pointer?

From Dev

typedef and pointer to function in C

From Dev

C function pointer and typedef

From Dev

Typedef function vs function pointer

From Dev

pointer to function to structure, pointer to function to typedef

From Dev

Is `typedef` of a function standard C syntax, and how does it differ from a `typedef` of a function pointer?

From Dev

Passing a typedef method as pointer function

From Dev

Function pointer IAR ( typedef void )

From Dev

How to typedef template function pointer?

From Dev

returning pointer to a function inline without typedef

From Dev

Using a pointer to function pointers without typedef?

From Dev

Typedef function pointer - differences between use & or not use it

From Dev

How to typedef a function pointer with template arguments

From Dev

Typedef for a pointer to a C function inside C++

From Dev

How to create a "typedef to a function pointer" in C#?

From Dev

Are infinite typedef function pointer inheritances possible?

From Dev

Passing typedef function pointer as an argument problem

From Dev

how to understand using typedef to define a function pointer?

From Dev

PostgreSQL: Function does not exist

From Dev

FUNCTION "SUM does not exist"

From Dev

typedef function pointer -> initialization from incompatible pointer type

From Dev

C++ typedef function pointer and declare a pointer in one statement

From Dev

Define a function pointer with typedef in a class to a parent class function

From Dev

How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

From Dev

SQL function HASHBYTES does not exist

From Dev

PSQL Error: function does not exist

From Java

Does a heaviside step function exist?

From Dev

function sum(boolean) does not exist

From Dev

jsonb function does not exist in greenplum

Related Related

  1. 1

    Typedef function pointer?

  2. 2

    typedef and pointer to function in C

  3. 3

    C function pointer and typedef

  4. 4

    Typedef function vs function pointer

  5. 5

    pointer to function to structure, pointer to function to typedef

  6. 6

    Is `typedef` of a function standard C syntax, and how does it differ from a `typedef` of a function pointer?

  7. 7

    Passing a typedef method as pointer function

  8. 8

    Function pointer IAR ( typedef void )

  9. 9

    How to typedef template function pointer?

  10. 10

    returning pointer to a function inline without typedef

  11. 11

    Using a pointer to function pointers without typedef?

  12. 12

    Typedef function pointer - differences between use & or not use it

  13. 13

    How to typedef a function pointer with template arguments

  14. 14

    Typedef for a pointer to a C function inside C++

  15. 15

    How to create a "typedef to a function pointer" in C#?

  16. 16

    Are infinite typedef function pointer inheritances possible?

  17. 17

    Passing typedef function pointer as an argument problem

  18. 18

    how to understand using typedef to define a function pointer?

  19. 19

    PostgreSQL: Function does not exist

  20. 20

    FUNCTION "SUM does not exist"

  21. 21

    typedef function pointer -> initialization from incompatible pointer type

  22. 22

    C++ typedef function pointer and declare a pointer in one statement

  23. 23

    Define a function pointer with typedef in a class to a parent class function

  24. 24

    How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

  25. 25

    SQL function HASHBYTES does not exist

  26. 26

    PSQL Error: function does not exist

  27. 27

    Does a heaviside step function exist?

  28. 28

    function sum(boolean) does not exist

  29. 29

    jsonb function does not exist in greenplum

HotTag

Archive