首页 > 代码库 > Windows程序中加载并使用动态链接库
Windows程序中加载并使用动态链接库
1 GetProcAddress()
1.1函数原型
GetProcAddress函数检索指定的动态链接库(DLL)中的输出库函数地址。
1.2 注释
1.3 参考代码
{
typedef DWORD (CALLBACK *LPFNREGISTER)(DWORD,DWORD);
LPFNREGISTER lpfnRegister;
lpfnRegister=(LPFNREGISTER)GetProcAddress(hModule,"RegisterServiceProcess");
if (lpfnRegister)
{
(*lpfnRegister)(NULL,1L);
}
}
2 LoadLibrary()
3 FreeLibrary()
Frees the loaded dynamic-link library (DLL) module and, if necessary, decrements its reference count. When the reference count reaches zero, the module is unloaded from the address space of the calling process and the handle is no longer valid.
3.1 函数原型
BOOL WINAPI FreeLibrary(
_In_ HMODULE hModule
);
A handle to the loaded library module. The LoadLibrary, LoadLibraryEx, GetModuleHandle, or GetModuleHandleEx function returns this handle.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call the GetLastError function.
3.2 注释
The system maintains a per-process reference count for each loaded module. A module that was loaded at process initialization due to load-time dynamic linking has a reference count of one. The reference count for a module is incremented each time the module is loaded by a call to LoadLibrary. The reference count is also incremented by a call to LoadLibraryEx unless the module is being loaded for the first time and is being loaded as a data or image file.
The reference count is decremented each time the FreeLibrary or FreeLibraryAndExitThread function is called for the module. When a module‘s reference count reaches zero or the process terminates, the system unloads the module from the address space of the process. Before unloading a library module, the system enables the module to detach from the process by calling the module‘s DllMain function, if it has one, with the DLL_PROCESS_DETACH value. Doing so gives the library module an opportunity to clean up resources allocated on behalf of the current process. After the entry-point function returns, the library module is removed from the address space of the current process.
It is not safe to call FreeLibrary from DllMain. For more information, see the Remarks section in DllMain.
Calling FreeLibrary does not affect other processes that are using the same module.
Use caution when calling FreeLibrary with a handle returned by GetModuleHandle. The GetModuleHandle function does not increment a module‘s reference count, so passing this handle to FreeLibrary can cause a module to be unloaded prematurely.
A thread that must unload the DLL in which it is executing and then terminate itself should call FreeLibraryAndExitThread instead of calling FreeLibrary and ExitThread separately. Otherwise, a race condition can occur. For details, see the Remarks section of FreeLibraryAndExitThread.
Windows程序中加载并使用动态链接库