Files
mirror-processhacker/trunk/ProcessHacker/UI/Actions/SessionActions.cs
T
wj32 898fd8b5e2 added elevation for session disconnect and logoff actions
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1392 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2009-06-08 01:10:16 +00:00

120 lines
4.6 KiB
C#

using System.Windows.Forms;
using ProcessHacker.Common;
using ProcessHacker.Components;
using ProcessHacker.Native;
using ProcessHacker.Native.Objects;
namespace ProcessHacker.UI.Actions
{
public class SessionActions
{
private static bool Prompt(IWin32Window window, string name,
string action, string content)
{
DialogResult result = DialogResult.No;
if (OSVersion.HasTaskDialogs)
{
TaskDialog td = new TaskDialog();
td.WindowTitle = "Process Hacker";
td.MainInstruction = "Do you want to " + action + " " + name + "?";
td.Content = content;
td.Buttons = new TaskDialogButton[]
{
new TaskDialogButton((int)DialogResult.Yes, char.ToUpper(action[0]) + action.Substring(1)),
new TaskDialogButton((int)DialogResult.No, "Cancel")
};
td.DefaultButton = (int)DialogResult.No;
result = (DialogResult)td.Show(window);
}
else
{
result = MessageBox.Show("Are you sure you want to " + action + " " + name + "?",
"Process Hacker", MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
}
return result == DialogResult.Yes;
}
private static void ElevateIfRequired(IWin32Window window, int session, string actionName, Action action)
{
try
{
action();
}
catch (WindowsException ex)
{
if (ex.ErrorCode == 5 &&
OSVersion.HasUac &&
Program.ElevationType == ProcessHacker.Native.Api.TokenElevationType.Limited)
{
TaskDialog td = new TaskDialog();
td.WindowTitle = "Process Hacker";
td.MainIcon = TaskDialogIcon.Warning;
td.MainInstruction = "Do you want to elevate the action?";
td.Content = "The action could not be performed in the current security context. " +
"Do you want Process Hacker to prompt for the appropriate credentials and elevate the action?";
td.ExpandedInformation = "Error: " + ex.Message + " (0x" + ex.ErrorCode.ToString("x") + ")";
td.ExpandFooterArea = true;
td.Buttons = new TaskDialogButton[]
{
new TaskDialogButton((int)DialogResult.Yes, "Elevate\nPrompt for credentials and elevate the action.")
};
td.CommonButtons = TaskDialogCommonButtons.Cancel;
td.UseCommandLinks = true;
td.Callback = (taskDialog, args, userData) =>
{
if (args.Notification == TaskDialogNotification.Created)
{
taskDialog.SetButtonElevationRequiredState((int)DialogResult.Yes, true);
}
return false;
};
DialogResult result = (DialogResult)td.Show(window);
if (result == DialogResult.Yes)
{
Program.StartProcessHackerAdmin("-e -type session -action " + actionName + " -obj \"" +
session.ToString() + "\" -hwnd " + window.Handle.ToString(), null, window.Handle);
}
}
else
{
PhUtils.ShowMessage("Error", ex);
}
}
}
public static void Disconnect(IWin32Window window, int session, bool prompt)
{
if (prompt && !Prompt(window, "the session", "disconnect", ""))
return;
ElevateIfRequired(window, session, "disconnect", () =>
{
TerminalServerHandle.GetCurrent().GetSession(session).Disconnect();
});
}
public static void Logoff(IWin32Window window, int session, bool prompt)
{
if (prompt && !Prompt(window, "the session", "logoff", ""))
return;
ElevateIfRequired(window, session, "logoff", () =>
{
TerminalServerHandle.GetCurrent().GetSession(session).Logoff();
});
}
}
}