Exemplo n.º 1
0
        public static Win32AppInfo Get()
        {
            var process = Process.GetCurrentProcess();

            // First get the app ID
            IApplicationResolver appResolver = (IApplicationResolver) new CAppResolver();

            appResolver.GetAppIDForProcess(Convert.ToUInt32(process.Id), out string appId, out _, out _, out _);

            // Use app ID (or hashed app ID) as AUMID
            string aumid = appId.Length > AUMID_MAX_LENGTH?HashAppId(appId) : appId;

            // Then try to get the shortcut (for display name and icon)
            IShellItem shortcutItem = null;

            try
            {
                appResolver.GetBestShortcutForAppID(appId, out shortcutItem);
            }
            catch
            {
            }

            string displayName = null;
            string iconPath    = null;

            // First we attempt to use display assets from the shortcut itself
            if (shortcutItem != null)
            {
                try
                {
                    shortcutItem.GetDisplayName(0, out displayName);

                    ((IShellItemImageFactory)shortcutItem).GetImage(new SIZE(48, 48), SIIGBF.IconOnly | SIIGBF.BiggerSizeOk, out IntPtr nativeHBitmap);

                    if (nativeHBitmap != IntPtr.Zero)
                    {
                        try
                        {
                            Bitmap bmp = Bitmap.FromHbitmap(nativeHBitmap);

                            if (IsAlphaBitmap(bmp, out var bmpData))
                            {
                                var alphaBitmap = GetAlphaBitmapFromBitmapData(bmpData);
                                iconPath = SaveIconToAppPath(alphaBitmap, appId);
                            }
                            else
                            {
                                iconPath = SaveIconToAppPath(bmp, appId);
                            }
                        }
                        catch
                        {
                        }

                        NativeMethods.DeleteObject(nativeHBitmap);
                    }
                }
                catch
                {
                }
            }

            // If we didn't get a display name from shortcut
            if (string.IsNullOrWhiteSpace(displayName))
            {
                // We use the one from the process
                displayName = GetDisplayNameFromCurrentProcess(process);
            }

            // If we didn't get an icon from shortcut
            if (string.IsNullOrWhiteSpace(iconPath))
            {
                // We use the one from the process
                iconPath = ExtractAndObtainIconFromCurrentProcess(process, appId);
            }

            return(new Win32AppInfo()
            {
                Aumid = aumid,
                DisplayName = displayName,
                IconPath = iconPath
            });
        }
Exemplo n.º 2
0
        public static Win32AppInfo Get()
        {
            var process = Process.GetCurrentProcess();

            // First get the app ID
            IApplicationResolver appResolver = (IApplicationResolver) new CAppResolver();

            appResolver.GetAppIDForProcess(Convert.ToUInt32(process.Id), out string appId, out _, out _, out _);

            string aumid;
            string pre7_0_1Aumid = null;

            // If the app ID is too long
            if (appId.Length > AUMID_MAX_LENGTH)
            {
                // Hash the AUMID
                aumid = HashAppId(appId);
            }

            // Else if it contains a backslash
            else if (appId.Contains('\\'))
            {
                // For versions 19042 and older of Windows 10, we can't use backslashes - Issue #3870
                // So we change it to not include those
                aumid         = appId.Replace('\\', '/');
                pre7_0_1Aumid = appId;
            }
            else
            {
                // Use as-is
                aumid = appId;
            }

            // Then try to get the shortcut (for display name and icon)
            IShellItem shortcutItem = null;

            try
            {
                appResolver.GetBestShortcutForAppID(appId, out shortcutItem);
            }
            catch
            {
            }

            string displayName = null;
            string iconPath    = null;

            // First we attempt to use display assets from the shortcut itself
            if (shortcutItem != null)
            {
                try
                {
                    shortcutItem.GetDisplayName(0, out displayName);

                    ((IShellItemImageFactory)shortcutItem).GetImage(new SIZE(48, 48), SIIGBF.IconOnly | SIIGBF.BiggerSizeOk, out IntPtr nativeHBitmap);

                    if (nativeHBitmap != IntPtr.Zero)
                    {
                        try
                        {
                            Bitmap bmp = Bitmap.FromHbitmap(nativeHBitmap);

                            if (IsAlphaBitmap(bmp, out var bmpData))
                            {
                                var alphaBitmap = GetAlphaBitmapFromBitmapData(bmpData);
                                iconPath = SaveIconToAppPath(alphaBitmap, aumid);
                            }
                            else
                            {
                                iconPath = SaveIconToAppPath(bmp, aumid);
                            }
                        }
                        catch
                        {
                        }

                        NativeMethods.DeleteObject(nativeHBitmap);
                    }
                }
                catch
                {
                }
            }

            // If we didn't get a display name from shortcut
            if (string.IsNullOrWhiteSpace(displayName))
            {
                // We use the one from the process
                displayName = GetDisplayNameFromCurrentProcess(process);
            }

            // If we didn't get an icon from shortcut
            if (string.IsNullOrWhiteSpace(iconPath))
            {
                // We use the one from the process
                iconPath = ExtractAndObtainIconFromCurrentProcess(process, aumid);
            }

            return(new Win32AppInfo()
            {
                Aumid = aumid,
                Pre7_0_1Aumid = pre7_0_1Aumid,
                DisplayName = displayName,
                IconPath = iconPath
            });
        }