WinHttpGetIEProxyConfigForCurrentUser 的文档说:
The caller must free the lpszProxy, lpszProxyBypass and lpszAutoConfigUrl strings in the WINHTTP_CURRENT_USER_IE_PROXY_CONFIG structure if they are non-NULL. Use GlobalFree to free the strings.
我编写了以下代码(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 Incompatible types: 'NativeUInt' and 'PWideChar'
问题:
我应该将
PWideChar
类型转换为NativeUInt
吗?我可以使用
GlobafFreePtr
代替GlobafFree
(它接受PWideChar
并且在我的测试中工作正常)?
最佳答案
当 MSDN 告诉您免费使用特定功能时,那么这样做就是您最好的选择。
Windows API 的部分内容是用 C 语言编写的(有些部分甚至没有定义 STRICT
?),而具有更好类型检查的其他语言在某些地方将需要强制转换。
对于 HGLOBAL,您可以使用 GlobalFlags
函数来帮助您。在您的情况下,标志的低字节为零,表示没有锁。如果字符串已被分配为可移动,则文档必须告诉您在访问内存之前锁定,但事实并非如此。
棺材上的最后一颗钉子是调试该函数,如果您这样做,您将看到它调用 GlobalAlloc
并将标志设置为 0x40 (GPTR
),并且应该因此无需解锁即可传递给 GlobalFree。如果您的编译器提示,那么您必须转换为适当的类型:
GlobalFree(HGLOBAL(VConfig.lpszAutoConfigUrl));
关于delphi - GlobalFree - 不兼容的类型 : 'NativeUInt' and 'PWideChar' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58707564/