added uac buttons

This commit is contained in:
STAM 2020-02-17 23:49:46 +03:00
parent c444457919
commit 0d234d54f8
5 changed files with 179 additions and 16 deletions

View File

@ -30,7 +30,7 @@
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.menuStripItemAbout = new System.Windows.Forms.ToolStripMenuItem();
this.groupBox = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.buttonSetNormal = new System.Windows.Forms.Button();
this.buttonSetPortable = new System.Windows.Forms.Button();
this.labelState = new System.Windows.Forms.Label();
this.menuStrip.SuspendLayout();
@ -79,7 +79,7 @@
this.groupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox.Controls.Add(this.button2);
this.groupBox.Controls.Add(this.buttonSetNormal);
this.groupBox.Controls.Add(this.buttonSetPortable);
this.groupBox.Controls.Add(this.labelState);
this.groupBox.Location = new System.Drawing.Point(12, 27);
@ -89,15 +89,16 @@
this.groupBox.TabStop = false;
this.groupBox.Text = "Current state:";
//
// button2
// buttonSetNormal
//
this.button2.Enabled = false;
this.button2.Location = new System.Drawing.Point(260, 124);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(197, 41);
this.button2.TabIndex = 1;
this.button2.Text = "Set Normal";
this.button2.UseVisualStyleBackColor = true;
this.buttonSetNormal.Enabled = false;
this.buttonSetNormal.Location = new System.Drawing.Point(260, 124);
this.buttonSetNormal.Name = "buttonSetNormal";
this.buttonSetNormal.Size = new System.Drawing.Size(197, 41);
this.buttonSetNormal.TabIndex = 1;
this.buttonSetNormal.Text = "Set Normal";
this.buttonSetNormal.UseVisualStyleBackColor = true;
this.buttonSetNormal.Click += new System.EventHandler(this.buttonSetNormal_Click);
//
// buttonSetPortable
//
@ -108,6 +109,7 @@
this.buttonSetPortable.TabIndex = 1;
this.buttonSetPortable.Text = "Set Portable (windows to go)";
this.buttonSetPortable.UseVisualStyleBackColor = true;
this.buttonSetPortable.Click += new System.EventHandler(this.buttonSetPortable_Click);
//
// labelState
//
@ -153,7 +155,7 @@
private System.Windows.Forms.ToolStripMenuItem menuStripItemAbout;
private System.Windows.Forms.GroupBox groupBox;
private System.Windows.Forms.Label labelState;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button buttonSetNormal;
private System.Windows.Forms.Button buttonSetPortable;
}
}

View File

@ -1,4 +1,5 @@
using System;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -12,12 +13,115 @@ namespace windows2go.switcher {
public partial class FormMain : Form {
public FormMain() {
InitializeComponent();
if (!UACSecurity.IsAdmin()) {
UACSecurity.AddShieldToButton(buttonSetNormal);
UACSecurity.AddShieldToButton(buttonSetPortable);
} else
this.Text += " (Administrator)";
}
bool PortableState = false;
bool KeyExists = false;
bool unExpected=false;
private void FormMain_Load(object sender, EventArgs e) {
FullRegistryCheck();
}
private void FullRegistryCheck() {
GetState();
if (!KeyExists) {
inUnexpectedlState();
} else {
switch (PortableState) {
case true:
inPortableState();
break;
case false:
inNormalState();
break;
default:
inUnexpectedlState();
break;
}
}
}
private void GetState() {
RegistryKey key = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
key = key.OpenSubKey(@"SYSTEM\CurrentControlSet\Control");
//check for exists "PortableOperatingSystem"
if (key != null) {
switch (key.GetValue("PortableOperatingSystem")) {
case 0:
PortableState = true;
KeyExists = true;
break;
case 1:
PortableState = false;
KeyExists = true;
break;
default:
PortableState = false;
KeyExists = false;
break;
}
}
}
private void inNormalState() {
buttonSetNormal.Enabled = false;
buttonSetPortable.Enabled = true;
labelState.Text = @"You are in normal mode";
labelState.ForeColor = Color.LimeGreen;
unExpected = false;
}
private void inPortableState() {
buttonSetNormal.Enabled = true;
buttonSetPortable.Enabled = false;
labelState.Text = @"You are in portable (Windows To Go) mode";
labelState.ForeColor = Color.Crimson;
unExpected = false;
}
private void inUnexpectedlState() {
buttonSetNormal.Enabled = true;
buttonSetPortable.Enabled = false;
buttonSetNormal.Text = @"Forced set to normal?";
labelState.Text = "Maby DWORD \"PortableOperatingSystem\" not found in registry or have wrong value or type." +Environment.NewLine+
"sIt equals normal mode.";
labelState.ForeColor = Color.OrangeRed;
unExpected = true;
}
private void buttonSetNormal_Click(object sender, EventArgs e) {
if (UACSecurity.IsAdmin()) {
switch (unExpected) {
case true:
MessageBox.Show("oh no.", "");
FullRegistryCheck();
break;
case false:
MessageBox.Show("yay.", "");
FullRegistryCheck();
break;
}
} else {
UACSecurity.RestartElevated();
}
}
private void buttonSetPortable_Click(object sender, EventArgs e) {
if (UACSecurity.IsAdmin()) {
MessageBox.Show("okay.", "");
FullRegistryCheck();
} else {
UACSecurity.RestartElevated();
}
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Security.Principal;
namespace windows2go.switcher {
public static class UACSecurity {
[DllImport("user32")]
public static extern UInt32 SendMessage(IntPtr hWnd, UInt32 msg, UInt32 wParam, UInt32 lParam);
internal const int BCM_FIRST = 0x1600;
internal const int BCM_SETSHIELD = (BCM_FIRST + 0x000C);
static internal bool IsVistaOrHigher() {
return Environment.OSVersion.Version.Major < 6;
}
/// <summary>
/// Checks if the process is elevated
/// </summary>
/// <returns>If is elevated</returns>
static internal bool IsAdmin() {
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal p = new WindowsPrincipal(id);
return p.IsInRole(WindowsBuiltInRole.Administrator);
}
/// <summary>
/// Add a shield icon to a button
/// </summary>
/// <param name="b">The button</param>
static internal void AddShieldToButton(Button b) {
b.FlatStyle = FlatStyle.System;
SendMessage(b.Handle, BCM_SETSHIELD, 0, 0xFFFFFFFF);
}
/// <summary>
/// Restart the current process with administrator credentials
/// </summary>
internal static void RestartElevated() {
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Verb = "runas";
try {
Process p = Process.Start(startInfo);
} catch (System.ComponentModel.Win32Exception ex) {
return; //If cancelled, do nothing
}
Application.Exit();
}
}
}

View File

Before

Width:  |  Height:  |  Size: 101 KiB

After

Width:  |  Height:  |  Size: 101 KiB

View File

@ -33,7 +33,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>iconfinder_microsoft_246000.ico</ApplicationIcon>
<ApplicationIcon>windows.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@ -57,6 +57,7 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UACSecurity.cs" />
<EmbeddedResource Include="FormMain.resx">
<DependentUpon>FormMain.cs</DependentUpon>
</EmbeddedResource>
@ -83,7 +84,7 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Content Include="iconfinder_microsoft_246000.ico" />
<Content Include="windows.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>