In MS Windos, there is a difference between implicit linking and explicit linking.
Implicit Linking
The executable is linked against the accompanying library (.lib file) which provides the symbols exported from DLL. (The functions for import are remarked with __declspec(dllimport).) Implicit linked DLLs are loaded with executable.
Explicit Linking
The program loads a DLL explicitly calling LoadLibrary(). To call functions of the DLL their address has to be determined using GetProcAddress().
However, GetProcAddress() can be used as well for functions which come from implicit linked DLLs. This can be helpful, if accidentally the same symbol is available in multiple DLLs (e.g. if DLLs have been used which are linked against different runtime DLLs).
Sometimes, DLLs are provided without import libraries. A wellknown example is OpenGL for which MS stopped support with version 1.2. However, with sufficient H/W and up-to-date drivers, all functions of the current OpenGL version might be availabe (and can be loaded with GetProcAdress() at run-time).
A piece of sample code for OpenGL binding MyGL.cc:
where MY_GL_API is defined as __declspec(dllexport) when MyGL.dll is compiled and __declspec(dllimport) otherwise. (So, actually __declspec(dllimport)andGetProcAddress() instead of vs. as function pointers themselves are dllexported but initialized at run-time unsing GetProcAddress().)
(The PFNGL macros expand to function pointer types with the appropriate signature. They are included from a header provided by kronos.org.)
Another important usage of GetProcAddress() is for functions which may exist not before a certain version of Windows (or other functions which may or may not be available in DLLs). Thus, the application can be written backwards compatible providing an alternative fall-back when GetProcAddress() fails for the intended function.
GetProcAddress() lets you find functions in runtime. It's useful for optional imports or for things like plug-ins that can be loaded dynamically. Another use for it is for undocumented functions that are exported but don't have a header file or library file.
Using __declspec( dllimport ) adds the function to your import table so it's loaded automatically with your executable.
In MS Windos, there is a difference between implicit linking and explicit linking.
Implicit Linking
The executable is linked against the accompanying library (
.lib
file) which provides the symbols exported from DLL. (The functions for import are remarked with__declspec(dllimport)
.) Implicit linked DLLs are loaded with executable.Explicit Linking
The program loads a DLL explicitly calling
LoadLibrary()
. To call functions of the DLL their address has to be determined usingGetProcAddress()
.However,
GetProcAddress()
can be used as well for functions which come from implicit linked DLLs. This can be helpful, if accidentally the same symbol is available in multiple DLLs (e.g. if DLLs have been used which are linked against different runtime DLLs).Sometimes, DLLs are provided without import libraries. A wellknown example is OpenGL for which MS stopped support with version 1.2. However, with sufficient H/W and up-to-date drivers, all functions of the current OpenGL version might be availabe (and can be loaded with
GetProcAdress()
at run-time).A piece of sample code for OpenGL binding
MyGL.cc
:with
MyGL.h
:where
MY_GL_API
is defined as__declspec(dllexport)
whenMyGL.dll
is compiled and__declspec(dllimport)
otherwise. (So, actually__declspec(dllimport)
andGetProcAddress()
instead of vs. as function pointers themselves aredllexport
ed but initialized at run-time unsingGetProcAddress()
.)(The
PFNGL
macros expand to function pointer types with the appropriate signature. They are included from a header provided by kronos.org.)Another important usage of
GetProcAddress()
is for functions which may exist not before a certain version of Windows (or other functions which may or may not be available in DLLs). Thus, the application can be written backwards compatible providing an alternative fall-back whenGetProcAddress()
fails for the intended function.The sample provided on MSDN for
GetProcAddress()
:Further reading: MSDN: Link an executable to a DLL
GetProcAddress()
lets you find functions in runtime. It's useful for optional imports or for things like plug-ins that can be loaded dynamically. Another use for it is for undocumented functions that are exported but don't have a header file or library file.Using
__declspec( dllimport )
adds the function to your import table so it's loaded automatically with your executable.