Beispiel #1
0
        private static IShellLink CreateShellLink(string title, string path, string arguments, string icon_path, int icon_pos)
        {
            try {
                IShellLink shell_link = (IShellLink)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID.ShellLink));
                shell_link.SetPath(path);

                if (!string.IsNullOrEmpty(arguments))
                {
                    shell_link.SetArguments(arguments);
                }

                if (!string.IsNullOrEmpty(icon_path))
                {
                    shell_link.SetIconLocation(icon_path, icon_pos);
                }

                IntPtr pps;
                Guid   ipsiid = CLSID.IPropertyStore;

                Marshal.QueryInterface(Marshal.GetIUnknownForObject(shell_link), ref ipsiid, out pps);
                IPropertyStore property_store = (IPropertyStore)Marshal.GetTypedObjectForIUnknown(pps, typeof(IPropertyStore));

                PROPVARIANT propvar = new PROPVARIANT();
                propvar.SetString(title);

                // PKEY_Title
                PROPERTYKEY PKEY_Title = new PROPERTYKEY();
                PKEY_Title.fmtid = new Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9");
                PKEY_Title.pid   = 2;

                property_store.SetValue(ref PKEY_Title, ref propvar);
                property_store.Commit();

                IntPtr psl;
                Guid   psliid = CLSID.IShellLinkW;

                Marshal.QueryInterface(Marshal.GetIUnknownForObject(shell_link), ref psliid, out psl);
                IShellLink link = (IShellLink)Marshal.GetTypedObjectForIUnknown(psl, typeof(IShellLink));

                propvar.Clear();

                Marshal.ReleaseComObject(property_store);
                property_store = null;

                Marshal.ReleaseComObject(shell_link);
                shell_link = null;

                return(link);
            } catch (COMException e) {
                Logger.Error("Error createing shell link: {0}\n{1}", e.Message, e.StackTrace);
            }

            return(null);
        }