/// <summary> /// Event handler. Called by serialPortReservationsToolStripMenuItem for click events. /// </summary> /// /// <param name="sender"> . </param> /// <param name="e"> Event information. </param> private void serialPortReservationsToolStripMenuItem_Click(object sender, EventArgs e) { if (Buster.IsAdmin()) { new SerialPortReservations().Execute(); } else { //Must run with limited privileges in order to see the UAC window RestartElevated(Application.ExecutablePath); } }
/// <summary> /// Enumerate Devices and Delete Ghosts that are marked. /// </summary> /// /// <param name="sender"> . </param> /// <param name="e"> . </param> private void RemoveBtn_Click(object sender, EventArgs e) { if (Buster.IsAdmin()) { if (chkSysRestore.Checked) { try { label1.Text = "Creating a System Restore Point..."; label1.Update(); ManagementBaseObject result = WmiRestorePoint("GhostBuster Restore Point", RestoreType.ApplicationInstall, EventType.BeginSystemChange); if (Int32.Parse(result[ReturnValue].ToString()) == S_OK) { label1.Text = "System Restore Point created successfully."; } else { label1.Text = "Creation of System Restore Point failed!"; } label1.Update(); Thread.Sleep(1000); } catch (ManagementException err) { MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message); } } label1.Text = "Removing devices..."; label1.Update(); Enumerate(true); label1.Text = "Devices removed."; label1.Update(); Thread.Sleep(1000); label1.Text = String.Empty; label1.Update(); } else { //Must run with limited privileges in order to see the UAC window RestartElevated(Application.ExecutablePath); } }
/// <summary> /// Enumerate All Devices and Set the UAC shield on the button. /// </summary> /// /// <param name="sender"> . </param> /// <param name="e"> . </param> private void Form1_Load(object sender, EventArgs e) { WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator); chkSysRestore.Enabled = SysRestoreAvailable(); using (IniFile ini = new IniFile(Buster.IniFileName)) { //#error When Merged the InFile.AppIni is wrong (Encrypt.dll) //MessageBox.Show(ini.FileName); chkSysRestore.Checked = ini.ReadBool("Setup", "CreateCheckPoint", chkSysRestore.Checked); } if (chkSysRestore.Enabled) { Console.WriteLine("System Restore Available"); } else { Console.WriteLine("System Restore Unavailable"); } if (!Buster.IsAdmin()) { AddShieldToButton(RemoveBtn); AddShieldToMenuItem(serialPortReservationsToolStripMenuItem); this.UACToolTip.ToolTipTitle = "UAC"; this.UACToolTip.SetToolTip(RemoveBtn, "\r\nFor Vista and Windows 7 Users:\r\n\r\n" + "Ghostbuster requires admin rights for device removal.\r\n" + "If you click this button GhostBuster will restart and ask for these rights."); } Buster.HwEntries.CollectionChanged += new NotifyCollectionChangedEventHandler(HwEntries_CollectionChanged); Enumerate(false); this.InfoToolTip.ToolTipTitle = "Help on Usage"; this.InfoToolTip.SetToolTip(listView1, "\r\nUse the Right Click Context Menu to:\r\n\r\n" + "1) Add devices or classes to the removal list (if ghosted)\r\n" + "2) Removed devices or classes of the removal list."); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //! Add Support for CommandLine Options: //! //! /Remove //! /Register (Task) //! /Unregister (Task) //! IniFile //! //! Start logging to TaskManager Log, EventLog and Debug Console. switch (Environment.GetCommandLineArgs().Length) { case 3: if (Buster.IsAdmin()) { if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/NOGUI")) { String fn = Environment.ExpandEnvironmentVariables(Environment.GetCommandLineArgs()[2]); if (File.Exists(fn) && fn.ToUpper().EndsWith(".INI")) { using (Buster buster = new Buster(fn)) { Buster.Enumerate(); Buster.WriteToEventLog(String.Format("Found {0} Device(s)", Buster.HwEntries.Count), EventLogEntryType.Information); Buster.WriteToEventLog(String.Format("Loaded {0} Device Filter(s)", Buster.Devices.Count), EventLogEntryType.Information); Buster.WriteToEventLog(String.Format("Loaded {0} Class Filter(s)", Buster.Classes.Count), EventLogEntryType.Information); Buster.WriteToEventLog(String.Format("Loaded {0} Wildcard Filter(s)", Buster.Wildcards.Count), EventLogEntryType.Information); //! Count Filtered Devices (separate match from deletion in RemoveDevice_IF_Ghosted_AND_Marked). Int32 ghosted = 0; Int32 cnt = 0; foreach (HwEntry he in Buster.HwEntries) { //Debug.WriteLine(he.Description); if (he.Ghosted) { ghosted++; } if (he.RemoveDevice_IF_Ghosted_AND_Marked()) { cnt++; } } Buster.WriteToEventLog(String.Format("Removed {0} of {1} Ghosted Device(s).", cnt, ghosted), EventLogEntryType.Information); Application.Exit(); return; } } else { MessageBox.Show(String.Format("Configuration File not found at \r\n'{0}'.", fn), Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/REGISTER")) { // Get the service on the local machine using (TaskService ts = new TaskService()) { String user = System.Security.Principal.WindowsIdentity.GetCurrent().Name; String conf = Environment.ExpandEnvironmentVariables(Environment.GetCommandLineArgs()[2]); String appl = Application.ExecutablePath; Debug.WriteLine(appl); Debug.WriteLine(conf); Version ver = ts.HighestSupportedVersion; Boolean newVer = (ver >= new Version(1, 2)); TaskDefinition td = ts.NewTask(); td.Data = "GhostBuster"; td.Principal.UserId = user; td.Principal.LogonType = TaskLogonType.InteractiveToken; td.RegistrationInfo.Author = "Wim van der Vegt"; td.RegistrationInfo.Description = String.Format( "{0} removes selected Ghosted Devices. \r\n" + "\r\n" + "Default Settings for this scheduled task are:\r\n" + " - Running with the required Administrative Privileges,\r\n" + " - Running 1 minute after a user logs on,\r\n" + " - Can be started manually with 'schtasks /run /ts {0}'. \r\n" + "\r\n" + "See http://ghosutbuster.codeplex.com", Buster.S_TITLE); td.RegistrationInfo.Documentation = "See http://ghostbuster.codeplex.com"; td.Settings.DisallowStartIfOnBatteries = true; td.Settings.Hidden = false; if (newVer) { td.Principal.RunLevel = TaskRunLevel.Highest; td.RegistrationInfo.Source = Buster.S_TITLE; td.RegistrationInfo.URI = new Uri("http://ghostbuster.codeplex.com"); td.RegistrationInfo.Version = new Version(0, 9); td.Settings.AllowDemandStart = true; td.Settings.AllowHardTerminate = true; td.Settings.Compatibility = TaskCompatibility.V2; td.Settings.MultipleInstances = TaskInstancesPolicy.Queue; } LogonTrigger lTrigger = (LogonTrigger)td.Triggers.Add(new LogonTrigger()); if (newVer) { lTrigger.Delay = TimeSpan.FromMinutes(1); lTrigger.UserId = user; } td.Actions.Add(new ExecAction( appl, String.Format("/NOGUI {0}", conf), Path.GetDirectoryName(appl))); //if (ts.FindTask(Buster.S_TITLE) != null) //{ // ts.RootFolder.DeleteTask(Buster.S_TITLE); //} // Register the task in the root folder Task task = ts.RootFolder.RegisterTaskDefinition( Buster.S_TITLE, td, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken, null); task.ShowEditor(); } } } else { MessageBox.Show(S_CONSOLE, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } break; case 2: { if (Buster.IsAdmin()) { if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/UNREGISTER")) { using (TaskService ts = new TaskService()) { if (ts.FindTask(Buster.S_TITLE) != null) { ts.RootFolder.DeleteTask(Buster.S_TITLE); } } } else if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/EVENTLOG")) { if (!EventLog.SourceExists(Buster.S_TITLE)) { EventLog.CreateEventSource(Buster.S_TITLE, Buster.S_TITLE); } } else { MessageBox.Show(S_SYNTAX, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } Application.Exit(); return; } else { if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/REGISTER") || Environment.GetCommandLineArgs()[1].ToUpper().Equals("/UNREGISTER")) { MessageBox.Show(S_SCHEDULE, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/NOGUI")) { MessageBox.Show(S_CONSOLE, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/EVENTLOG")) { MessageBox.Show(S_EVENTLOG, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(S_SYNTAX, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } } } break; default: Application.Run(new Mainform()); break; } }