Function pointers

In non-DLL code, the actual address of a nonimported function is assigned to a function pointer. In DLL code, the address of a function descriptor is assigned to a function pointer.

If you assign the address of an imported function to a pointer in non-DLL code, the link step will fail with an unresolved reference. In a complex DLL or DLL application, a pointer to a function descriptor may be passed to non-DLL code. A direct function pointer (pointer to a function entry point) may be passed to DLL code. A parameter, a return value, or an external variable can pass a function pointer or an external variable.

In a complex DLL or DLL application, a function pointer may point either to a function descriptor or to a function entry, depending on the origin of the code. The different ways of dereferencing a function pointer causes the compatibility problem in linking DLL code with non-DLL code.

In Figure 1,  1  assigns the address of the descriptor for the imported function f to fp.  2  assigns the address of the imported variable x to xp.  3  assigns the address of the descriptor for the nonimported function g to gp.  4  assigns the address of the non-imported variable y to yp.

Figure 1. Pointer Assignment in DLL code
graphic

In Figure 2,  1  causes a bind error because the assignment to fp is undefined.  2  causes a binder error because the assignment to xp is undefined.  3  assigns gp to the address of the nonimported function, g.  4  assigns the address of the nonimported variable y to yp.

Figure 2. Pointer assignment in non-DLL code
graphic