Example #1
0
        private static IEnumerable <string> GetServiceRequiredPrivileges(SafeServiceHandle service)
        {
            using (var buf = new SafeHGlobalBuffer(8192))
            {
                if (!Win32NativeMethods.QueryServiceConfig2(service, SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO,
                                                            buf, buf.Length, out int needed))
                {
                    return(new string[0]);
                }

                IntPtr str_pointer = buf.Read <IntPtr>(0);
                if (str_pointer == IntPtr.Zero)
                {
                    return(new string[0]);
                }

                SafeHGlobalBuffer str_buffer = new SafeHGlobalBuffer(str_pointer, 8192 - 8, false);
                ulong             offset     = 0;
                List <string>     privs      = new List <string>();
                while (offset < str_buffer.ByteLength)
                {
                    string s = str_buffer.ReadNulTerminatedUnicodeString(offset);
                    if (s.Length == 0)
                    {
                        break;
                    }
                    privs.Add(s);
                    offset += (ulong)(s.Length + 1) * 2;
                }
                return(privs.AsReadOnly());
            }
        }