WinHttpGetIEProxyConfigForCurrentUser的文档指出:
如果WINHTTP_CURRENT_USER_IE_PROXY_CONFIG结构体中的lpszProxy,lpszProxyBypass和lpszAutoConfigUrl字符串非空,则调用者必须释放它们。使用GlobalFree来释放这些字符串。
我编写了以下代码(Delphi 10.3.2):
var
VConfig: TWinHttpCurrentUserIEProxyConfig;
begin
FillChar(VConfig, SizeOf(VConfig), 0);
if not WinHttpGetIEProxyConfigForCurrentUser(VConfig) then begin
RaiseLastOSError;
end;
...
if VConfig.lpszAutoConfigUrl <> nil then begin
GlobalFree(VConfig.lpszAutoConfigUrl); // <-- Error
end;
出现错误:
[dcc32 Error] E2010 不兼容的类型:'NativeUInt' 和 'PWideChar'
问题:
我应该将
PWideChar
强制转换为NativeUInt
吗?我可以使用
GlobafFreePtr
替代GlobafFree
吗(它接受PWideChar
并在我的测试中正常工作)?
GlobalFreePtr
callsGlobalHandle
to get anHGLOBAL
from a pointer, thenGlobalUnlock
to unlock that handle, thenGlobalFree
. All the examples I see online callGlobalFree
directly on the pointer. My guess is that, these days at least,GlobalHandle
returns its input value directly, in other words theHGLOBAL
is the same thing as the pointer. My suggestion is that you callGlobalFree
directly, and hence cast the pointer toHGLOBAL
.GlobalFree(HGLOBAL(...))
- David HeffernanGlobalFreePtr
。因此,我认为在所有情况下使用GlobalFreePtr
会更安全。 - zed