delphi - GlobalFree - 不兼容的类型 : 'NativeUInt' and 'PWideChar'

标签 delphi winapi

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/

相关文章:

windows - Windows api 程序错误 : cannot convert from 'LRESULT (__stdcall *)(HWND, UINT、LPARAM、WPARAM) 到 WNDPROC

c - 如何将 Windows 消息从一个线程传递到另一个线程?

c++ - 试图解决有关 SECURITY_FLAG_STRENGTH_* 标志的歧义

德尔福7 : an abstract class through VFI

android - 如何在Android/iOS中释放组件

delphi - 其他语言上的 ObjectPascal 标识符命名风格

delphi - 如何设置注册表项的值

c# - LIST_ENTRY 和 UNICODE_STRING pinvoke C#

c - FD_ACCEPT 如何接受多线程的连接请求?

delphi - 从delphi控制邮件客户端