Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Get icon from filename on Windows 8



Permlink Replies: 3 - Last Post: Aug 3, 2015 6:49 AM Last Post By: Ana Rodrigues
Ana Rodrigues

Posts: 5
Registered: 2/26/05
Get icon from filename on Windows 8
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 30, 2015 1:04 AM
Hi,
Does anyone knows how the right way to get icon from filename on windows 8? I use the code below on windows 7, and it works. When using the same code on windows 8, some applications (filename), appear with a default icon, although the real icon is another in windows explorer:

function GetFileIcon(FileName: String): HICON;
var lFileInfo: SHFILEINFO;
begin
FillMemory(@lFileInfo,SizeOf(lFileInfo),0);
Result := 0;

if (SHGetFileInfo(PWideChar(FileName), FILE_ATTRIBUTE_DIRECTORY, lFileInfo, SizeOf(lFileInfo),
SHGFI_ICON OR SHGFI_SYSICONINDEX) > 0) then //SHGFI_LARGEICON OR SHGFI_SMALLICON
begin
Result := lFileInfo.hIcon;
end;
end;

Is there a different primitive to get the icon? SHGetFileInfoW, or SHGetFileInfoA? Or the place in registry where we should look for icon information changed from windows 7 to 8, to another place?
Thanks any help.
Best Regards,
Ana Rodrigues
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Get icon from filename on Windows 8
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 30, 2015 9:19 AM   in response to: Ana Rodrigues in response to: Ana Rodrigues
Ana wrote:

Does anyone knows how the right way to get icon from filename
on windows 8? I use the code below on windows 7, and it works.
When using the same code on windows 8, some applications
(filename), appear with a default icon, although the real icon is
another in windows explorer

What is the actual return value of SHGetFileInfo(), and what does the content
of the SHFILEINFO look like?

Microsoft is not in the habit of breaking existing APIs, especially one as
old as SHGetFileInfo().

On a side note, why are you specifying FILE_ATTRIBUTE_DIRECTORY for a file?
In any case, since you are not specifying the SHGFI_USEFILEATTRIBUTES flag,
that parameter is ignored anyway.

--
Remy Lebeau (TeamB)
Ana Rodrigues

Posts: 5
Registered: 2/26/05
Re: Get icon from filename on Windows 8
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 3, 2015 2:06 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Ana wrote:

Does anyone knows how the right way to get icon from filename
on windows 8? I use the code below on windows 7, and it works.
When using the same code on windows 8, some applications
(filename), appear with a default icon, although the real icon is
another in windows explorer

What is the actual return value of SHGetFileInfo(), and what does the content
of the SHFILEINFO look like?


Microsoft is not in the habit of breaking existing APIs, especially one as
old as SHGetFileInfo().

On a side note, why are you specifying FILE_ATTRIBUTE_DIRECTORY for a file?
In any case, since you are not specifying the SHGFI_USEFILEATTRIBUTES flag,
that parameter is ignored anyway.

--
Remy Lebeau (TeamB)

Hi,
Thank you for your reply. The value returned from SHGetFileInfo() is an integer non zero for the particular application.
If IconHandle return by that API, is non zero, i get it right. If it is zero, i put a default icon from an ImageList of mine. strangely, today, it worked in windows 8 as windows 7. Yes, microsoft doesnt have that habit, but i though something has changed in registry information, about applications icons, not the API.
Thanks!

if IconHandle <> 0 then begin
Icon.Handle := IconHandle;
end
else begin
ImagemSemIcon := TIcon.Create;
ImgListNoIcon.GetIcon(0,ImagemSemIcon);
Icon.Handle := ImagemSemIcon.Handle;
end;{Aplicacao não tem icon}

Ana Rodrigues

Posts: 5
Registered: 2/26/05
Re: Get icon from filename on Windows 8
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 3, 2015 6:49 AM   in response to: Ana Rodrigues in response to: Ana Rodrigues
Ana Rodrigues wrote:
Remy Lebeau (TeamB) wrote:
Ana wrote:

Does anyone knows how the right way to get icon from filename
on windows 8? I use the code below on windows 7, and it works.
When using the same code on windows 8, some applications
(filename), appear with a default icon, although the real icon is
another in windows explorer

What is the actual return value of SHGetFileInfo(), and what does the content
of the SHFILEINFO look like?


Microsoft is not in the habit of breaking existing APIs, especially one as
old as SHGetFileInfo().

On a side note, why are you specifying FILE_ATTRIBUTE_DIRECTORY for a file?
In any case, since you are not specifying the SHGFI_USEFILEATTRIBUTES flag,
that parameter is ignored anyway.

--
Remy Lebeau (TeamB)

Hi,
Thank you for your reply. The value returned from SHGetFileInfo() is an integer non zero for the particular application.
If IconHandle return by that API, is non zero, i get it right. If it is zero, i put a default icon from an ImageList of mine. strangely, today, it worked in windows 8 as windows 7. Yes, microsoft doesnt have that habit, but i though something has changed in registry information, about applications icons, not the API.
Thanks!

if IconHandle <> 0 then begin
Icon.Handle := IconHandle;
end
else begin
ImagemSemIcon := TIcon.Create;
ImgListNoIcon.GetIcon(0,ImagemSemIcon);
Icon.Handle := ImagemSemIcon.Handle;
end;{Aplicacao não tem icon}


Hi again,
Just found another way to get the correct icon of an application. Just used JclSysInfo from JEDI:
function GetWindowIcon(Wnd: THandle; LargeIcon: Boolean): HICON;
It worked just fine, on both scenarios win 7 and win 8.
Best Regards
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02