コード例 #1
0
ファイル: RegistryHelper.cs プロジェクト: bartoszduk/pebakery
        /*
         * public const UInt32 HKCR = 0x80000000; // HKEY_CLASSES_ROOT
         * public const UInt32 HKCU = 0x80000001; // HKEY_CURRENT_USER
         * public const UInt32 HKLM = 0x80000002; // HKEY_LOCAL_MACHINE
         * public const UInt32 HKU = 0x80000003; // HKEY_USERS
         * public const UInt32 HKPD = 0x80000004; // HKEY_PERFORMANCE_DATA
         * public const UInt32 HKCC = 0x80000005; // HKEY_CURRENT_CONFIG
         */

        public static void GetAdminPrivileges()
        {
            TOKEN_PRIVILEGES pRestoreToken = new TOKEN_PRIVILEGES();
            TOKEN_PRIVILEGES pBackupToken  = new TOKEN_PRIVILEGES();

            if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out IntPtr hToken))
            {
                throw new BetterWin32Errors.Win32Exception("OpenProcessToken failed");
            }

            if (!LookupPrivilegeValue(null, "SeRestorePrivilege", out LUID restoreLUID))
            {
                throw new BetterWin32Errors.Win32Exception("LookupPrivilegeValue failed");
            }

            if (!LookupPrivilegeValue(null, "SeBackupPrivilege", out LUID backupLUID))
            {
                throw new BetterWin32Errors.Win32Exception("LookupPrivilegeValue failed");
            }

            pRestoreToken.Count = 1;
            pRestoreToken.Luid  = restoreLUID;
            pRestoreToken.Attr  = SE_PRIVILEGE_ENABLED;

            pBackupToken.Count = 1;
            pBackupToken.Luid  = backupLUID;
            pBackupToken.Attr  = SE_PRIVILEGE_ENABLED;

            if (!AdjustTokenPrivileges(hToken, false, ref pRestoreToken, 0, IntPtr.Zero, IntPtr.Zero))
            {
                BetterWin32Errors.Win32Error error = BetterWin32Errors.Win32Exception.GetLastWin32Error();
                if (error == BetterWin32Errors.Win32Error.ERROR_NOT_ALL_ASSIGNED)
                {
                    throw new BetterWin32Errors.Win32Exception("AdjustTokenPrivileges failed, try running this program with Administrator privilege.");
                }
                else
                {
                    throw new BetterWin32Errors.Win32Exception("AdjustTokenPrivileges failed");
                }
            }

            if (!AdjustTokenPrivileges(hToken, false, ref pBackupToken, 0, IntPtr.Zero, IntPtr.Zero))
            {
                BetterWin32Errors.Win32Error error = BetterWin32Errors.Win32Exception.GetLastWin32Error();
                if (error == BetterWin32Errors.Win32Error.ERROR_NOT_ALL_ASSIGNED)
                {
                    throw new BetterWin32Errors.Win32Exception("AdjustTokenPrivileges failed, try running this program with Administrator privilege.");
                }
                else
                {
                    throw new BetterWin32Errors.Win32Exception("AdjustTokenPrivileges failed");
                }
            }
            CloseHandle(hToken);
        }
コード例 #2
0
        public static void GetAdminPrivileges()
        {
            NativeMethods.TOKEN_PRIVILEGES pRestoreToken = new NativeMethods.TOKEN_PRIVILEGES();
            NativeMethods.TOKEN_PRIVILEGES pBackupToken  = new NativeMethods.TOKEN_PRIVILEGES();

            // Because procHandle is a pseudo handle, it does not need to be closed.
            // https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess#remarks
            IntPtr procHandle = NativeMethods.GetCurrentProcess();

            const uint desiredAccess = NativeMethods.TOKEN_ADJUST_PRIVILEGES | NativeMethods.TOKEN_QUERY;

            if (!NativeMethods.OpenProcessToken(procHandle, desiredAccess, out IntPtr hToken))
            {
                throw new Win32Exception("OpenProcessToken failed");
            }

            try
            {
                if (!NativeMethods.LookupPrivilegeValue(null, "SeRestorePrivilege", out NativeMethods.LUID restoreLuid))
                {
                    throw new Win32Exception("LookupPrivilegeValue failed");
                }

                if (!NativeMethods.LookupPrivilegeValue(null, "SeBackupPrivilege", out NativeMethods.LUID backupLuid))
                {
                    throw new Win32Exception("LookupPrivilegeValue failed");
                }

                pRestoreToken.Count = 1;
                pRestoreToken.Luid  = restoreLuid;
                pRestoreToken.Attr  = NativeMethods.SE_PRIVILEGE_ENABLED;

                pBackupToken.Count = 1;
                pBackupToken.Luid  = backupLuid;
                pBackupToken.Attr  = NativeMethods.SE_PRIVILEGE_ENABLED;

                if (!NativeMethods.AdjustTokenPrivileges(hToken, false, ref pRestoreToken, 0, IntPtr.Zero, IntPtr.Zero))
                {
                    BetterWin32Errors.Win32Error ret = BetterWin32Errors.Win32Exception.GetLastWin32Error();
                    if (ret == BetterWin32Errors.Win32Error.ERROR_NOT_ALL_ASSIGNED)
                    {
                        throw new Win32Exception((int)ret, "AdjustTokenPrivileges failed, try running this program with Administrator privilege.");
                    }
                    else
                    {
                        throw new Win32Exception((int)ret, "AdjustTokenPrivileges failed");
                    }
                }

                if (!NativeMethods.AdjustTokenPrivileges(hToken, false, ref pBackupToken, 0, IntPtr.Zero, IntPtr.Zero))
                {
                    BetterWin32Errors.Win32Error ret = BetterWin32Errors.Win32Exception.GetLastWin32Error();
                    if (ret == BetterWin32Errors.Win32Error.ERROR_NOT_ALL_ASSIGNED)
                    {
                        throw new Win32Exception((int)ret, "AdjustTokenPrivileges failed, try running this program with Administrator privilege.");
                    }
                    else
                    {
                        throw new Win32Exception((int)ret, "AdjustTokenPrivileges failed");
                    }
                }
            }
            finally
            {
                NativeMethods.CloseHandle(hToken);
            }
        }