Lazarus

Programming => Operating Systems => Windows => Topic started by: Borneq on January 29, 2020, 08:07:43 pm

Title: How to get file attributes using unit shlobj ?
Post by: Borneq on January 29, 2020, 08:07:43 pm
I can walk through folders starting at SHGetDesktopFolder, next I do SHGetSpecialFolderLocation, next FIDesktopFolder.BindToObject and ShellFolder.EnumObjects. I can get path and display name by ShellFolder.GetDisplayNameOf.
But
how can file size and file modification time?
Is struct FILEDESCRIPTOR but i can't find any function that uses it.
Title: Re: How to get file attributes using unit shlobj ?
Post by: Remy Lebeau on January 29, 2020, 08:27:00 pm
But how can file size and file modification time?

You can use IShellFolder.BindToStorage() (https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellfolder-bindtostorage) and then IStorage.Stat() (https://docs.microsoft.com/en-us/windows/win32/api/objidl/nf-objidl-istorage-stat) or IStream.Stat() (https://docs.microsoft.com/en-us/windows/win32/api/objidl/nf-objidl-istream-stat).

Or, have a look at IShellFolder2 (https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ishellfolder2), which has GetDetailsOf() (https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellfolder2-getdetailsof) and GetDetailsEx() (https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellfolder2-getdetailsex) methods.

Or, consider using IShellItem (https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ishelliteml) instead of IShellFolder.  IShellFolder is an old API.  IShellItem2 (https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ishellitem2) has GetFileTime() (https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellitem2-getfiletime) and GetUInt64(PKEY_Size) (https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellitem2-getuint64) methods.

Otherwise, you can just use the filesystem API instead, using the path you retrieve from each shell item to call GetFileSize() (https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfilesize) and GetFileTime() (https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfiletime), or GetFileAttributesEx(GetFileExInfoStandard) (https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileattributesexw).
Title: Re: How to get file attributes using unit shlobj ?
Post by: Borneq on January 29, 2020, 08:33:10 pm
In ShellApi unit are SHGetFileInfo functions (is needed path).
I try use newer IShellItem but I don't see IShellItem2, only
Code: Pascal  [Select][+][-]
  1.     PIShellItem= ^IShellItem;
  2.     IShellItem = interface(IUnknown)
  3.                ['{43826d1e-e718-42ee-bc55-a1e261c37bfe}']
  4.                function BindToHandler(pbc:IBindCTX;bhid:PGUID;RIID:PIID;ppv:ppointer):HResult;Stdcall;          
  5.                function GetParent(ppsi:PISHellItem):HResult;Stdcall;
  6.                function GetDisplayName(sigdnname:SIGDN;ppszName:LPWSTR):HResult; Stdcall;
  7.                function GetAttributes(sfgaomask:SFGAOF;psfgaoAttribs:PSFGAOF):HResult;Stdcall;
  8.                function Compare(psi:IShellItem;hint:SICHINTF;piorder:PINT):HResult; Stdcall;
  9.                end;    
Title: Re: How to get file attributes using unit shlobj ?
Post by: Borneq on January 29, 2020, 08:52:51 pm
I get it from https://raw.githubusercontent.com/tpn/winsdk-10/master/Include/10.0.16299.0/um/ShObjIdl_core.h (https://raw.githubusercontent.com/tpn/winsdk-10/master/Include/10.0.16299.0/um/ShObjIdl_core.h)
if Windows 7 and Windows 8 handle it?
Title: Re: How to get file attributes using unit shlobj ?
Post by: Remy Lebeau on February 01, 2020, 03:38:28 am
In ShellApi unit are SHGetFileInfo functions (is needed path).

That function (https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shgetfileinfoa) does not provide the information you are asking for (file size, modification time, etc).

I try use newer IShellItem but I don't see IShellItem2

Then you will have to define it manually in your own code, based on the definition provided on MSDN, or I'm sure you can find a Pascal translation online if you look around.
TinyPortal © 2005-2018