Beispiel #1
0
        public static List <ServiceInfo> GetServicesByPID(int pid)
        {
            bool doUpdate = false;

            if (pid == -1) // -1 means get all and we always want a fresh list
            {
                doUpdate = true;
            }
            else
            {
                ServiceCacheLock.EnterReadLock();
                doUpdate = ServiceCacheTime <= DateTime.FromFileTimeUtc(ProcFunc.GetProcessCreationTime(pid)).ToLocalTime();
                ServiceCacheLock.ExitReadLock();
            }
            if (doUpdate)
            {
                RefreshServices();
            }

            ServiceCacheLock.EnterReadLock();
            CloneableList <ServiceInfo> values;

            if (pid == -1)
            {
                values = ServiceCacheByPID.GetAllValues();
            }
            else if (!ServiceCacheByPID.TryGetValue(pid, out values))
            {
                values = null;
            }
            ServiceCacheLock.ExitReadLock();
            return((values != null && values.Count == 0) ? null : values);
        }
Beispiel #2
0
            public ServiceInfo(ServiceController sc)
            {
                ServiceName = sc.ServiceName;
                var ImagePath = GetServiceImagePath(sc.ServiceName);

                ServicePath = ImagePath != null?ProcFunc.GetPathFromCmdLine(ImagePath) : "";

                DisplayName = sc.DisplayName;
                if (sc.Status == ServiceControllerStatus.Stopped)
                {
                    LastKnownPID = -1;
                }
                else
                {
                    var ssp = GetServiceStatus(sc.ServiceName);
                    LastKnownPID = ssp != null ? (int)ssp.dwProcessId : -1;
                }
            }
Beispiel #3
0
        public string GetAppPackageByPID_(int PID)
        {
            // WARNING: this is not consistent with the windows firewall, we need to go by the proces token

            //var process = System.Diagnostics.Process.GetProcessById(PID); // throws error if pid is not found
            var processHandle = ProcFunc.OpenProcess(0x1000 /*PROCESS_QUERY_LIMITED_INFORMATION*/, false, PID);

            if (processHandle == IntPtr.Zero)
            {
                return(null);
            }

            uint          cchLen  = 256;
            StringBuilder sbName  = new StringBuilder((int)cchLen);
            Int32         lResult = GetApplicationUserModelId(processHandle, ref cchLen, sbName);

            if (ERROR_INSUFFICIENT_BUFFER == lResult)
            {
                sbName  = new StringBuilder((int)cchLen);
                lResult = GetApplicationUserModelId(processHandle, ref cchLen, sbName);
            }
            ProcFunc.CloseHandle(processHandle);

            if (lResult != ERROR_SUCCESS)
            {
                return(null);
            }

            string sResult = sbName.ToString();
            int    pos     = sResult.LastIndexOf("!"); // bla!App

            if (pos != -1)
            {
                sResult = sResult.Substring(0, pos);
            }

            return(sResult);
        }