System.Security.Principal.Win32.ConvertIntPtrSidToByteArraySid C# (CSharp) Method

ConvertIntPtrSidToByteArraySid() static private method

static private ConvertIntPtrSidToByteArraySid ( IntPtr binaryForm ) : byte[]
binaryForm System.IntPtr
return byte[]
        internal static byte[] ConvertIntPtrSidToByteArraySid(IntPtr binaryForm)
        {
            byte[] ResultSid;

            //
            // Verify the revision (just sanity, should never fail to be 1)
            //

            byte Revision = Marshal.ReadByte(binaryForm, 0);

            if (Revision != SecurityIdentifier.Revision)
            {
                throw new ArgumentException(SR.IdentityReference_InvalidSidRevision, nameof(binaryForm));
            }

            //
            // Need the subauthority count in order to figure out how many bytes to read
            //

            byte SubAuthorityCount = Marshal.ReadByte(binaryForm, 1);

            if (SubAuthorityCount < 0 ||
                SubAuthorityCount > SecurityIdentifier.MaxSubAuthorities)
            {
                throw new ArgumentException(SR.Format(SR.IdentityReference_InvalidNumberOfSubauthorities, SecurityIdentifier.MaxSubAuthorities), nameof(binaryForm));
            }

            //
            // Compute the size of the binary form of this SID and allocate the memory
            //

            int BinaryLength = 1 + 1 + 6 + SubAuthorityCount * 4;
            ResultSid = new byte[BinaryLength];

            //
            // Extract the data from the returned pointer
            //

            Marshal.Copy(binaryForm, ResultSid, 0, BinaryLength);

            return ResultSid;
        }

Usage Example

        internal static int CreateSidFromString(string stringSid, out byte[] resultSid)
        {
            IntPtr zero = IntPtr.Zero;
            int    lastWin32Error;

            try
            {
                if (1 != Win32Native.ConvertStringSidToSid(stringSid, out zero))
                {
                    lastWin32Error = Marshal.GetLastWin32Error();
                    goto IL_2D;
                }
                resultSid = Win32.ConvertIntPtrSidToByteArraySid(zero);
            }
            finally
            {
                Win32Native.LocalFree(zero);
            }
            return(0);

IL_2D:
            resultSid = null;
            return(lastWin32Error);
        }
All Usage Examples Of System.Security.Principal.Win32::ConvertIntPtrSidToByteArraySid