This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VistaSecurity.cs
50 lines (47 loc) · 1.7 KB
/
VistaSecurity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
namespace VanSwitch
{
internal class VistaSecurity
{
[DllImport("user32")]
public static extern UInt32 SendMessage
(IntPtr hWnd, UInt32 msg, UInt32 wParam, UInt32 lParam);
internal const int BCM_FIRST = 0x1600; //Normal button
internal const int BCM_SETSHIELD = (BCM_FIRST + 0x000C); //Elevated button
static internal bool IsAdmin()
{
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal p = new WindowsPrincipal(id);
Debug.WriteLine($"VanSwitch (VistaSecurity) : IsAdmin? = {p.IsInRole(WindowsBuiltInRole.Administrator)}");
return p.IsInRole(WindowsBuiltInRole.Administrator);
}
internal static void RestartElevated(string args)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Verb = "runas";
startInfo.Arguments = args;
Debug.WriteLine($"VanSwitch (VistaSecurity) : Restaring as Elevated with args '{args}'");
try
{
Process p = Process.Start(startInfo);
}
catch (System.ComponentModel.Win32Exception)
{
return;
}
Application.Exit();
}
}
}