コード例 #1
0
ファイル: ModernAppInfo.cs プロジェクト: w7yuu/EarTrumpet
        public ModernAppInfo(int processId, bool trackProcess)
        {
            this.processId = processId;

            var appUserModelId = GetAppUserModelIdByPid(processId);
            var shellItem      = GetShellItemForAppByAumid(appUserModelId);

            BackgroundColor    = shellItem.GetUInt32(ref PropertyKeys.PKEY_AppUserModel_Background);
            PackageInstallPath = shellItem.GetString(ref PropertyKeys.PKEY_AppUserModel_PackageInstallPath);
            ExeName            = PackageInstallPath;
            DisplayName        = AppsFolder.ReadDisplayName(appUserModelId);

            try
            {
                var rawSmallLogoPath = shellItem.GetString(ref PropertyKeys.PKEY_Tile_SmallLogoPath);
                var smallLogoPath    = Path.Combine(PackageInstallPath, rawSmallLogoPath);
                if (File.Exists(smallLogoPath))
                {
                    SmallLogoPath = smallLogoPath;
                }
                else
                {
                    var mrtResourceManager = (IMrtResourceManager) new MrtResourceManager();
                    mrtResourceManager.InitializeForPackage(shellItem.GetString(ref PropertyKeys.PKEY_AppUserModel_PackageFullName));

                    var map = mrtResourceManager.GetMainResourceMap();
                    SmallLogoPath = Path.Combine(PackageInstallPath, map.GetFilePath(rawSmallLogoPath));

                    Marshal.ReleaseComObject(map);
                    Marshal.ReleaseComObject(mrtResourceManager);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"{ex}");
            }

            if (trackProcess)
            {
                ProcessWatcherService.WatchProcess(processId, (pid) => Stopped?.Invoke(this));
            }
        }
コード例 #2
0
        public DesktopAppInfo(int processId, bool trackProcess)
        {
            _processId = processId;

            var handle = Kernel32.OpenProcess(Kernel32.ProcessFlags.PROCESS_QUERY_LIMITED_INFORMATION | Kernel32.ProcessFlags.SYNCHRONIZE, false, processId);

            if (handle != IntPtr.Zero)
            {
                try
                {
                    ZombieProcessException.ThrowIfZombie(processId, handle);

                    var  fileNameBuilder = new StringBuilder(260);
                    uint bufferLength    = (uint)fileNameBuilder.Capacity;
                    if (Kernel32.QueryFullProcessImageName(handle, 0, fileNameBuilder, ref bufferLength) != 0)
                    {
                        if (fileNameBuilder.Length > 0)
                        {
                            var processFullPath = fileNameBuilder.ToString();
                            ExeName            = Path.GetFileNameWithoutExtension(processFullPath);
                            SmallLogoPath      = processFullPath;
                            PackageInstallPath = processFullPath;
                        }
                    }
                }
                finally
                {
                    Kernel32.CloseHandle(handle);
                }
            }
            else
            {
                trackProcess = false;

                if (TryGetExecutableNameViaNtByPid(processId, out var imageName))
                {
                    ExeName            = Path.GetFileNameWithoutExtension(imageName);
                    SmallLogoPath      = imageName;
                    PackageInstallPath = imageName;
                }
                else
                {
                    throw new ZombieProcessException(processId);
                }
            }

            // Display Name priority:
            // - AppsFolder
            // - A window caption
            // - Exe Name

            try
            {
                var appResolver = (IApplicationResolver) new ApplicationResolver();
                appResolver.GetAppIDForProcess((uint)processId, out string appId, out _, out _, out _);
                DisplayName = AppsFolder.ReadDisplayName(appId);
                Marshal.ReleaseComObject(appResolver);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }

            if (string.IsNullOrWhiteSpace(DisplayName))
            {
                try
                {
                    using (var proc = Process.GetProcessById(_processId))
                    {
                        DisplayName = proc.MainWindowTitle;
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                }
            }

            if (trackProcess)
            {
                ProcessWatcherService.WatchProcess(processId, (pid) => Stopped?.Invoke(this));
            }
        }