コード例 #1
0
        private static void ServiceFill(object o)
        {
            var ww            = (UIPerformWorkWindow)o;
            var sb            = new StringBuilder();
            var services      = ServiceController.GetServices();
            var managerHandle = Win32Native.OpenSCManager(null, null, 0x20004);

            for (var i = 0; i < services.Length; i++)
            {
                var service = services[i];
                try
                {
                    var localSb = new StringBuilder();
                    localSb.AppendLine($"{service.DisplayName} ({service.ServiceName})");
                    var  serviceHandle = Win32Native.OpenService(managerHandle, service.ServiceName, 0x20000);
                    uint sdSize;
                    var  result = Win32Native.QueryServiceObjectSecurity(serviceHandle, 7, null, 0, out sdSize);
                    var  gle    = Marshal.GetLastWin32Error();
                    if (result || (gle != 122))
                    {
                        Win32Native.CloseServiceHandle(serviceHandle);
                        throw new System.ComponentModel.Win32Exception(gle);
                    }
                    var binarySd = new byte[sdSize];
                    result = Win32Native.QueryServiceObjectSecurity(serviceHandle, 7, binarySd, binarySd.Length, out sdSize);
                    gle    = Marshal.GetLastWin32Error();
                    if (!result)
                    {
                        Win32Native.CloseServiceHandle(serviceHandle);
                        throw new System.ComponentModel.Win32Exception(gle);
                    }
                    var cd = new CommonSecurityDescriptor(false, false, binarySd, 0);
                    localSb.AppendLine(cd.GetSddlForm(AccessControlSections.All));
                    sb.AppendLine(localSb.ToString());
                    Win32Native.CloseServiceHandle(serviceHandle);
                    ww.Percentage = (int)((i + 0.0) * 100.0 / (services.Length + 0.0));
                    if (ww.AbortEvent.WaitOne(0))
                    {
                        break;
                    }
                }
                catch
                {
                    // continue
                }
            }
            Win32Native.CloseServiceHandle(managerHandle);
            _fillData = sb.ToString();
            ww.AbortEvent.Set();
        }