gpt4 book ai didi

windows - 通过 GetModuleHandle/LoadLibrary 和使用 FreeLibrary 加载 DLL

转载 作者:可可西里 更新时间:2023-11-01 14:46:00 27 4
gpt4 key购买 nike

这是我的代码:

function GetProcedureAddress(var P: FARPROC; const ModuleName, ProcName: AnsiString): Boolean;
var
ModuleHandle: HMODULE;
begin
Result := False;
ModuleHandle := GetModuleHandle(PAnsiChar(AnsiString(ModuleName)));
if ModuleHandle = 0 then
ModuleHandle := LoadLibrary(PAnsiChar(ModuleName)); // DO WE NEED TO CALL FreeLibrary ?
if ModuleHandle <> 0 then
begin
P := Pointer(GetProcAddress(ModuleHandle, PAnsiChar(ProcName)));
if Assigned(P) then
Result := True;
end;
end;

function PathMakeSystemFolder(Path: AnsiString): Boolean;
var
_PathMakeSystemFolderA: function(pszPath: PAnsiChar): BOOL; stdcall;
begin
Result := False;
if GetProcedureAddress(@_PathMakeSystemFolderA, 'shlwapi.dll', 'PathMakeSystemFolderA') then
Result := _PathMakeSystemFolderA(PChar(Path));
end;

如果使用 LoadLibrary,是否需要调用 FreeLibrary?或者它的引用计数会在我的应用程序终止时自动递减?

最佳答案

我会引用 here .

The system maintains a per-process reference count on all loaded modules. Calling LoadLibrary increments the reference count. Calling the FreeLibrary or FreeLibraryAndExitThread function decrements the reference count. The system unloads a module when its reference count reaches zero or when the process terminates (regardless of the reference count).

所以基本上您不需要调用 FreeLibrary 但您应该考虑这样做。我个人认为当资源没有被正确处理时,这是一个错误。

关于windows - 通过 GetModuleHandle/LoadLibrary 和使用 FreeLibrary 加载 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7364846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com