Search code examples
delphiwinapidelphi-11-alexandria

Delphi 11.3 - SHGetFileInfo raises ERangeError


I have some code that calls SHGetFileInfo to retrieve the list of system icons:

FileIcons: TImageList;

procedure LoadFileIcons;
var
  listHandle: Integer;
  shFileInfo: TSHFileInfo;
begin
  listHandle:= SHGetFileInfo('', 0, shFileInfo, Sizeof(shFileInfo),
                             SHGFI_SYSICONINDEX or SHGFI_LARGEICON);

  if (listHandle <> 0) then
  begin
    FileIcons.Handle:=listHandle;
    FileIcons.ShareImages:=true;
  end;
end

On Delphi 10.4 this worked fine, and I was able to load icons and display files with proper icon. On Delphi 11.3, it raises the following exception:

Project Project.exe raised exception class ERangeError with message 'Range check error at address 00000000035C7A99'.

Has anything changed between Delphi 10.4 and Delphi 11.3? How can I fix it?


Solution

  • Solved declaring listHandle as DWORD_PTR:

    FileIcons: TImageList;
    
    procedure LoadFileIcons;
    var
      listHandle: DWORD_PTR;
      shFileInfo: TSHFileInfo;
    begin
      listHandle:= SHGetFileInfo('', 0, shFileInfo, Sizeof(shFileInfo),
                                 SHGFI_SYSICONINDEX or SHGFI_LARGEICON);
    
      if (listHandle <> 0) then
      begin
        FileIcons.Handle:=listHandle;
        FileIcons.ShareImages:=true;
      end;
    end