public GrantBackupAndRestorePrivilege() { this._ProcessHandle = Win32Interface.OpenProcess(ProcessAccessFlags.SetInformation | ProcessAccessFlags.QueryInformation, false, Win32Interface.GetCurrentProcessId()); Win32Interface.OpenProcessToken(this._ProcessHandle, TokenAccessFlags.Query | TokenAccessFlags.AdjustPrivileges, out this._TokenHandle); TOKEN_PRIVILEGES_SIMPLE NewState = new TOKEN_PRIVILEGES_SIMPLE(); TOKEN_PRIVILEGES_SIMPLE PreviousState = new TOKEN_PRIVILEGES_SIMPLE(); LUID lpLuid; if (!Win32Interface.LookupPrivilegeValue("", "SeRestorePrivilege", out lpLuid)) { return; } NewState.PrivilegeCount = 1U; NewState.Luid = lpLuid; NewState.Attributes = PrivilegeAttributes.Enabled; int ReturnLength1 = Marshal.SizeOf <TOKEN_PRIVILEGES_SIMPLE>(PreviousState); Win32Interface.AdjustTokenPrivileges(this._TokenHandle, false, ref NewState, Marshal.SizeOf <TOKEN_PRIVILEGES_SIMPLE>(NewState), ref PreviousState, ref ReturnLength1); Win32Interface.LookupPrivilegeValue("", "SeBackupPrivilege", out lpLuid); Win32Interface.LookupPrivilegeValue("", "SeSecurityPrivilege", out lpLuid); Win32Interface.LookupPrivilegeValue("", "SeTakeOwnershipPrivilege", out lpLuid); NewState.PrivilegeCount = 1U; NewState.Luid = lpLuid; NewState.Attributes = PrivilegeAttributes.Enabled; int ReturnLength2 = Marshal.SizeOf <TOKEN_PRIVILEGES_SIMPLE>(PreviousState); Win32Interface.AdjustTokenPrivileges(this._TokenHandle, false, ref NewState, Marshal.SizeOf <TOKEN_PRIVILEGES_SIMPLE>(NewState), ref PreviousState, ref ReturnLength2); }
public RegistryLoader(string hiveFileName, string registryRootPath) { this._HiveFileName = hiveFileName; this._RegistryRootPath = registryRootPath; this._Disposed = false; using (new GrantBackupAndRestorePrivilege()) Win32Interface.TestRegistryResult(0, Win32Interface.RegLoadKey(HKey.LocalMachine, registryRootPath, hiveFileName), string.Format("RegLoadKey failed to load the hive \"{0}\" into the key \"{1}\"", (object)hiveFileName, (object)registryRootPath)); }
private void Dispose(bool disposing) { if (this._Disposed) { return; } Win32Interface.RegUnloadKey(HKey.LocalMachine, this._RegistryRootPath); this._Disposed = true; }
public void Dispose() { Win32Interface.CloseHandle(this._TokenHandle); Win32Interface.CloseHandle(this._ProcessHandle); }