Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GlobalFree - Incompatible types: 'NativeUInt' and 'PWideChar'

Tags:

winapi

delphi

Documentation of WinHttpGetIEProxyConfigForCurrentUser says:

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.

I wrote the following code (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;

and got an error:

[dcc32 Error] E2010 Incompatible types: 'NativeUInt' and 'PWideChar'

Questions:

  • should I type-cast PWideChar to NativeUInt?

  • can I use GlobafFreePtr instead of GlobafFree (it accepts PWideChar and works fine in my tests)?

like image 560
zed Avatar asked Nov 05 '19 08:11

zed


1 Answers

When MSDN tells you to free with a specific function then doing just that is your best bet.

Parts of the Windows API is written in C and (some parts even without STRICT defined?) and other languages with better type checking will require casts in some places.

In the case of HGLOBALs you have the GlobalFlags function that can help you out. In your case the low byte of flags is zero indicating that there are no locks. If the strings had been allocated as movable the documentation would have to tell you to lock before accessing the memory and it does not.

The final nail in the coffin is to debug the function and if you do that you will see that it calls GlobalAlloc with flags set to 0x40 (GPTR) and should therefore be passed to GlobalFree without unlocking. If your compiler complains then you must cast to the appropriate type:

GlobalFree(HGLOBAL(VConfig.lpszAutoConfigUrl)); 
like image 122
Anders Avatar answered Sep 18 '22 20:09

Anders