How to Link to a DLL
There are two ways to link to a DLL from a Visual C++ program:- Implicit linking - This is the most common way to link to a DLL. It assumes you have the DLL, its import library file (.lib) and its header file (.h).
- Explicit linking - This technique is used when a .lib and/or a .h file are not available for the DLL. It involves using the Win32 APIs LoadLibrary() and GetProcAddress().
Implicit Linking

Setting Compile Directories
First, we have to tell the compiler where to find the DLL's header file, MyDll.h, which we have used in a#include in MyApp. In the MyApp property pages, we add ..\include to the Additional Include Directories:

Setting Link Directories
Now that the compiler is happy, we have to tell the linker where to find the .lib file for the DLL. Again in the MyApp property pages, we add ..\lib to the Additional Library Directories:

Where is the DLL?
The above steps will compile and link MyApp. The only remaining question is, how does MyApp find the DLL when it is run? With the tightened security of Vista and Win7, you should not even consider putting an application DLL in the Windows or System32 directories. Application DLLs belong in the same directory as the application EXE. When you are developing a new application, you have two choices: put a copy of the DLL in your Debug directory, or make a bin directory where you copy the application EXE with a post-build command.It is important that you do not copy development DLLs anywhere to the Windows path, where other applications may pick them up by mistake, or where you might confuse an older DLL with the one you are trying to debug. Be very careful about where you copy DLLs.
Explicit Linking
When you explicitly load a DLL, you do not have to specify additional include or library directories. However, the DLL must still be accessible to the EXE. Once again, application DLLs belong in the same directory as the application EXE.
Even though you do not have a DLL header file, you still must know the function prototype of the function you want to call. As an example, suppose you want to call the function IsThemeActive() which you know is in the Windows DLL UxTheme.dll. After checking on MSDN, you see that the function prototype is
BOOL IsThemeActive(void);so you write the following typedef:
typedef BOOL (__stdcall *PFNISTHEMEACTIVE)();(Note: all Windows APIs use
__stdcall calling convention.)
Since this is a Windows DLL, it will be on the path, so loading the DLL is simple:
HMODULE hDLL = LoadLibrary(_T("UxTheme.dll"));
and now you can get a pointer to IsThemeActive():
PFNISTHEMEACTIVE pIsThemeActive = (PFNISTHEMEACTIVE)GetProcAddress(hDLL, "IsThemeActive");and you can call the function like this:
BOOL bThemed = pIsThemeActive();When you are finished with the DLL, you should unload it like this:
FreeLibrary(hDLL);