internal static SecurityIdentifier GetWindowsServiceSid(string name)
        {
            short  num3;
            string accountName = string.Format(CultureInfo.InvariantCulture, @"NT Service\{0}", new object[] { name });

            byte[] sid   = null;
            uint   cbSid = 0;
            uint   cchReferencedDomainName = 0;
            int    error = 0;

            if (!ListenerUnsafeNativeMethods.LookupAccountName(null, accountName, sid, ref cbSid, null, ref cchReferencedDomainName, out num3))
            {
                error = Marshal.GetLastWin32Error();
                if (error != 0x7a)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error));
                }
            }
            sid = new byte[cbSid];
            StringBuilder referencedDomainName = new StringBuilder((int)cchReferencedDomainName);

            if (!ListenerUnsafeNativeMethods.LookupAccountName(null, accountName, sid, ref cbSid, referencedDomainName, ref cchReferencedDomainName, out num3))
            {
                error = Marshal.GetLastWin32Error();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error));
            }
            return(new SecurityIdentifier(sid, 0));
        }
Example #2
0
        internal static SecurityIdentifier GetWindowsServiceSid(string name)
        {
            Fx.Assert(OSEnvironmentHelper.IsVistaOrGreater, "This method can be called only on Vista or greater.");
            string accountName = string.Format(CultureInfo.InvariantCulture, WindowsServiceAccountFormat, name);

            byte[] sid   = null;
            uint   cbSid = 0;
            uint   cchReferencedDomainName = 0;
            short  peUse;
            int    error = UnsafeNativeMethods.ERROR_SUCCESS;

            if (!ListenerUnsafeNativeMethods.LookupAccountName(null, accountName, sid, ref cbSid,
                                                               null, ref cchReferencedDomainName, out peUse))
            {
                error = Marshal.GetLastWin32Error();
                if (error != ListenerUnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error));
                }
            }

            sid = new byte[cbSid];
            StringBuilder referencedDomainName = new StringBuilder((int)cchReferencedDomainName);

            if (!ListenerUnsafeNativeMethods.LookupAccountName(null, accountName, sid, ref cbSid,
                                                               referencedDomainName, ref cchReferencedDomainName, out peUse))
            {
                error = Marshal.GetLastWin32Error();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error));
            }

            return(new SecurityIdentifier(sid, 0));
        }