The LoadLibrary() function for loading and unloading a library through the DLL
// Create a new empty Win32 console mode application project, the source file name: testmydllruntime.cpp
// Using Run-Time Dynamic Linking which is demonstrated using a simple program that uses LoadLibrary() and GetProcAddress() to access mydll() in mydllpro.dll.
// For WinXp, don't forget to add
#define _WIN32_WINNT 0x0501
#include <stdio.h>
#include <windows.h>
typedef void (*MYPROC)(LPTSTR);
int main(void)
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to our DLL module created in the previous example. Make sure you already copied the mydllpro.lib and mydllpro.dll to the appropriate folders...
hinstLib = LoadLibrary(L"mydllpro");
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
printf("The LoadLibrary(L\"mydllpro\") dll handle is valid...\n");
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "mydll");
// If the function address is valid, call the function.
if (ProcAdd != NULL)
{
printf("The function address is valid: %p\n\n", ProcAdd);
fRunTimeLinkSuccess = TRUE;
// Pass some text, mydll() will display it on the standard output...
(ProcAdd) (L"\nThis message is via DLL function...\n");
}
else
printf("\nThe function address is NOT valid, error: %d.\n", GetLastError());
// Free up the DLL module.
fFreeResult = FreeLibrary(hinstLib);
if (fFreeResult != 0)
printf("FreeLibrary() is OK.\n");
else
printf("FreeLibrary() is not OK, error: %d.\n", GetLastError());
}
else
printf("The LoadLibrary(L\"mydllpro\") dll handle is not valid, error: %d.\n", GetLastError());
// If unable to call the DLL function, use an alternative.
if (!fRunTimeLinkSuccess)
printf("This message via alternative method...\n");
return 0;
}
Output example:
The LoadLibrary(L"mydllpro") dll handle is valid...
The function address is valid: 1001108C
-This is mydll.dll file lol!-
GetStdHandle(), standard handle is OK.
This message is via DLL function...
FreeLibrary() is OK.
Press any key to continue . . .
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows Xp Pro SP2
Target platform: none, just for learning and fun
Header file: Standard and Windows
Additional library: Windows Platform SDK
Additional project setting: Set project to be compiled as C.
Project->your_project_name Properties->Configuration Properties->C/C++->Advanced->Compiled As: Compiled as C Code (/TC)
Other info: non-CLR or unmanaged. Need to add Advapi32.lib (Advapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'Advapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.
To do: Loading a library through the DLL in Windows system programmatically
To show: The Windows process, thread and DLL C program example