LoadLibrary method loads and runs DLL library.
The LoadLibrary function is a function in the Windows operating system that is used to load dynamic link libraries (DLLs) at runtime. Its function prototype is as follows:
Load a library module and return a handle to it.
The parameter lpFileName specifies the file name of the DLL library to be loaded, which can be an absolute or relative path. The return value is a module handle (HMODULE) used for further operations on the library.
Here is a simple example code demonstrating how to use the LoadLibrary function to load and run a DLL library.
#include <windows.h>
int main()
{
HMODULE hDll = LoadLibrary(L"mydll.dll");
if (hDll != NULL)
{
// DLL加载成功,继续执行其他操作
// ...
// 卸载DLL
FreeLibrary(hDll);
}
else
{
// DLL加载失败,处理错误
}
return 0;
}
In this example, the LoadLibrary function is first called to load a DLL library named “mydll.dll”. If the loading is successful, LoadLibrary will return a non-empty module handle. Then, through this module handle, the GetProcAddress function can be used to obtain the addresses of exported functions within the DLL and call them.
Finally, unload the DLL library by calling the FreeLibrary function.
It is important to note that when using the LoadLibrary function to load a DLL library, you need to make sure that the DLL library file is in the search path of the executable file, or specify the correct path. Otherwise, the LoadLibrary function will return NULL, indicating a failed load.
Additionally, the DLL libraries loaded using the LoadLibrary function need to be unloaded by calling the FreeLibrary function before the program ends to release the related resources.