Ejemplo n.º 1
0
        private static NativeMethods.TOKEN_ELEVATION_TYPE GetProcessElevationType()
        {
            SafeTokenHandle tokenHandle;

            var success = NativeMethods.OpenProcessToken(Process.GetCurrentProcess().Handle, NativeMethods.TOKEN.TOKEN_READ, out tokenHandle);

            var lastError = Marshal.GetLastWin32Error();

            if (!success)
            {
                throw new Win32Exception(lastError, string.Format(CultureInfo.CurrentCulture, "{0}: OpenProcessToken failed with error: {1}", MethodBase.GetCurrentMethod().Name, lastError.ToString(CultureInfo.CurrentCulture)));
            }


            using (tokenHandle)
                using (var safeBuffer = new SafeGlobalMemoryBufferHandle(Marshal.SizeOf(Enum.GetUnderlyingType(typeof(NativeMethods.TOKEN_ELEVATION_TYPE)))))
                {
                    uint bytesReturned;
                    success = NativeMethods.GetTokenInformation(tokenHandle, NativeMethods.TOKEN_INFORMATION_CLASS.TokenElevationType, safeBuffer, (uint)safeBuffer.Capacity, out bytesReturned);

                    lastError = Marshal.GetLastWin32Error();

                    if (!success)
                    {
                        throw new Win32Exception(lastError, string.Format(CultureInfo.CurrentCulture, "{0}: GetTokenInformation failed with error: {1}", MethodBase.GetCurrentMethod().Name, lastError.ToString(CultureInfo.CurrentCulture)));
                    }


                    return((NativeMethods.TOKEN_ELEVATION_TYPE)safeBuffer.ReadInt32());
                }
        }