简体   繁体   中英

Make a windows Extension handler for custom icons in C#

I want to handle a file extension icon by dll and give icon according to file contents in Windows Explorer (something like thumbnail icons for PSD files. Photoshop handles .psd extension and generates thumbnail for each file)

I made a dll in C# that can handle Load and GetIconLocation functions well. It seems Windows should call the Extract function after GetIconLocation but it won't!

Interface:

   [ComVisible(true), ComImport, Guid("000214eb-0000-0000-c000-000000000046"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
   public interface IExtractIcon
   {
        [PreserveSig]
        uint GetIconLocation(int uFlags, IntPtr szIconFile, int cchMax, IntPtr piIndex, UIntPtr pwFlags);

        //[PreserveSig]
        //uint GetIconLocation(uint uFlags, IntPtr szIconFile, uint cchMax, IntPtr piIndex, UIntPtr pwFlags);
        [PreserveSig]
        uint Extract(string pszFile, uint nIconIndex, ref IntPtr phiconLarge,ref IntPtr phiconSmall, uint nIconSize);
    }

And functions:

    public uint Load(string pszFileName, uint dwMode)//Using IPersistFile
    {
        icon_File = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Icon.bmp");
        tip = "My tool-tip";
        Logger.WriteLog("Load :"+pszFileName+" , "+dwMode.ToString());

        return S_OK;

    }

    public uint GetIconLocation(int uFlags, IntPtr szIconFile, int cchMax, IntPtr piIndex, UIntPtr pwFlags)//Using IExtractIcon and IPersistFile.Load
    {
            try
            {
                IconHandlerReturnFlags Flags;

                Flags = IconHandlerReturnFlags.PerClass | IconHandlerReturnFlags.DontCache | IconHandlerReturnFlags.NotFilename;
                pwFlags = (UIntPtr)Flags;

                Logger.WriteLog("GetIconLocation...");
                return S_OK;
            }
            catch (Exception e)
            {
                Logger.WriteLog("GetIconLocation " + e.Message);
                return S_FALSE;
            }
        }


        public uint Extract(string pszFile, uint nIconIndex, ref IntPtr phiconLarge,ref IntPtr phiconSmall, uint nIconSize)//Using IExtractIcon 
        {

                Logger.WriteLog("Extract...");
    // other code...
}

Load function works correctly and gives correct filename. Also GetIconLocation works. But after return S_OK value Extract won't exec...:(

This is the log after view two .myf files icon in Windows Explorer

Load : C:\**********1.Myf , 0
GetIconLocation...
Load : C:\**********2.Myf , 0
GetIconLocation...
Load : C:\**********1.Myf , 0
GetIconLocation...
GetIconLocation...
Load : C:\**********1.Myf , 0
GetIconLocation..

(Sometimes two GetIconLocation call without Load )

Please someone help me....

it may help you answer my question:(from microsoft site...)

Implementing the IExtractIcon Interface After the interface is initialized, the Shell uses the handler's IExtractIcon interface to request the appropriate icon. The interface has two methods: IExtractIcon::GetIconLocation and IExtractIcon::Extract . Icons are identified by their location in the file system. The IExtractIcon::GetIconLocation method is called to request this information. Set the szIconFile parameter to the file name. If there is more than one icon in the file, set piIndex to the icon's index. Assign appropriate values to the two flag variables. If you do not want to specify a file name, or if you do not want the Shell to extract the icon, set the GIL_NOTFILENAME flag in the pwFlags parameter. You do not need to assign a value to szIconFile, but the handler must provide icon handles when the Shell calls IExtractIcon::Extract . If you return a file name, the Shell normally attempts to load the icon from its cache. To prevent the loading of a cached icon, set the GIL_DONTCACHE flag in the pwFlags parameter. If a cached icon is not loaded, the Shell then calls IExtractIcon::Extract to request the icon handle. If a file and index were specified by IExtractIcon::GetIconLocation , they are passed to IExtractIcon::Extract in the pszFile and nIconIndex parameters, respectively. If a file name is provided, your handler can return S_FALSE to have the Shell extract the icon. Otherwise, your handler must extract or otherwise produce large and small icons, and assign their HICON handles to the phiconLarge and phiconSmall parameters. The Shell adds the icons to its cache to expedite subsequent calls to the handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM