Search code examples
c#winapiiconslnk

SHGetFileInfo for lnk file without lnk overlay arrow


I'm trying to get the icon out of a .lnk file without the .lnk overlay appearing. The documentation has information about a flag SHGFI_ADDOVERLAYS which can be set to add an overlay, but I am looking to remove the overlay.

I have read this question, as well as the links inside of it, but I am still unable to get it working in c#.

Here is the code I have tried:

SHFILEINFO shFileInfo = new SHFILEINFO();

SHGetFileInfo(
    pathToLnk,
    0,
    ref shFileInfo,
    (uint)Marshal.SizeOf(shFileInfo),
    SHGFI_ICON & ~SHGFI_LINKOVERLAY);

As well as some other configurations of the flags.

Thanks in advance for the help.


Solution

  • The solution is as follows:

    [DllImport("Comctl32.dll")]
    public static extern IntPtr ImageList_GetIcon(IntPtr himl, int i, uint flags);
    
    
    SHFILEINFO fileInfo = new SHFILEINFO();
            IntPtr list = SHGetFileInfo(
                pathToLnk,
                FileAttributes,
                ref fileInfo,
                (uint)Marshal.SizeOf(fileInfo),
                SHGFI_SYSICONINDEX);
            var iconHandle = ImageList_GetIcon(list, fileInfo.iIcon.ToInt32(), FileFlags);
            Icon icn = Icon.FromHandle(iconHandle);