private void toggleGlobalHook_CheckedChanged(object sender, EventArgs e) { if (!toggleGlobalHook.Enabled) { return; } toggleGlobalHook.Enabled = false; if (toggleGlobalHook.Checked) { if (!Helpers.IsElevated) { DialogResult res = MessageBox.Show("RenderDoc needs to restart with admin privileges. Restart?", "Restart as admin", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (res == DialogResult.Yes) { string capfile = Path.GetTempFileName() + ".cap"; AutoStart.Checked = false; SaveSettings(capfile); var process = new Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo(Application.ExecutablePath, capfile); process.StartInfo.Verb = "runas"; try { process.Start(); } catch (Exception) { // don't restart if it failed for some reason (e.g. user clicked no to UAC) toggleGlobalHook.Checked = false; toggleGlobalHook.Enabled = true; return; } m_Core.Config.Serialize(Core.ConfigFilename); m_Core.Config.ReadOnly = true; m_Core.AppWindow.Close(); return; } else { toggleGlobalHook.Checked = false; toggleGlobalHook.Enabled = true; return; } } exePath.Enabled = exeBrowse.Enabled = workDirPath.Enabled = workDirBrowse.Enabled = cmdline.Enabled = capture.Enabled = save.Enabled = load.Enabled = false; foreach (Control c in capOptsFlow.Controls) { c.Enabled = false; } foreach (Control c in actionsFlow.Controls) { c.Enabled = false; } toggleGlobalHook.Text = "Disable Global Hook"; var path = Path.GetDirectoryName(Path.GetFullPath(Application.ExecutablePath)); var regfile = Path.Combine(Path.GetTempPath(), "RenderDoc_RestoreGlobalHook.reg"); try { if (Environment.Is64BitProcess) { EnableAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE").CreateSubKey("Wow6432Node"), path, "x86\\renderdocshim32.dll", out prevAppInitWoW64Enabled, out prevAppInitWoW64); EnableAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE"), path, "renderdocshim64.dll", out prevAppInitEnabled, out prevAppInit); using (FileStream s = File.OpenWrite(regfile)) { using (StreamWriter sw = new StreamWriter(s)) { sw.WriteLine("Windows Registry Editor Version 5.00"); sw.WriteLine(""); sw.WriteLine("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows NT\\CurrentVersion\\Windows]"); sw.WriteLine(String.Format("\"LoadAppInit_DLLs\"=dword:{0:X8}", prevAppInitWoW64Enabled)); sw.WriteLine(String.Format("\"AppInit_DLLs\"=\"{0}\"", prevAppInitWoW64)); sw.WriteLine(""); sw.WriteLine("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows]"); sw.WriteLine(String.Format("\"LoadAppInit_DLLs\"=dword:{0:X8}", prevAppInitEnabled)); sw.WriteLine(String.Format("\"AppInit_DLLs\"=\"{0}\"", prevAppInit)); sw.Flush(); } } } else { // if this is a 64-bit OS, it will re-direct our request to Wow6432Node anyway, so we // don't need to handle that manually EnableAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE"), path, "renderdocshim32.dll", out prevAppInitEnabled, out prevAppInit); using (FileStream s = File.OpenWrite(regfile)) { using (StreamWriter sw = new StreamWriter(s)) { sw.WriteLine("Windows Registry Editor Version 5.00"); sw.WriteLine(""); sw.WriteLine("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows]"); sw.WriteLine(String.Format("\"LoadAppInit_DLLs\"=dword:{0:X8}", prevAppInitEnabled)); sw.WriteLine(String.Format("\"AppInit_DLLs\"=\"{0}\"", prevAppInit)); sw.Flush(); } } } } catch (System.Exception ex) { MessageBox.Show("Aborting. Couldn't save backup .reg file to " + regfile + Environment.NewLine + ex.ToString(), "Cannot save registry backup", MessageBoxButtons.OK, MessageBoxIcon.Error); exePath.Enabled = exeBrowse.Enabled = workDirPath.Enabled = workDirBrowse.Enabled = cmdline.Enabled = capture.Enabled = save.Enabled = load.Enabled = true; foreach (Control c in capOptsFlow.Controls) { c.Enabled = true; } foreach (Control c in actionsFlow.Controls) { c.Enabled = true; } // won't recurse because it's not enabled yet toggleGlobalHook.Checked = false; toggleGlobalHook.Text = "Enable Global Hook"; toggleGlobalHook.Enabled = true; return; } ExitPipeThread(); pipeExit = false; try { pipe32 = new NamedPipeServerStream("RenderDoc.GlobalHookControl32"); pipe64 = new NamedPipeServerStream("RenderDoc.GlobalHookControl64"); } catch (System.IO.IOException ex) { // tidy up and exit MessageBox.Show("Aborting. Couldn't create named pipe:" + Environment.NewLine + ex.Message, "Cannot create named pipe", MessageBoxButtons.OK, MessageBoxIcon.Error); exePath.Enabled = exeBrowse.Enabled = workDirPath.Enabled = workDirBrowse.Enabled = cmdline.Enabled = capture.Enabled = save.Enabled = load.Enabled = true; foreach (Control c in capOptsFlow.Controls) { c.Enabled = true; } foreach (Control c in actionsFlow.Controls) { c.Enabled = true; } // need to revert registry entries too if (Environment.Is64BitProcess) { RestoreAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE").CreateSubKey("Wow6432Node"), prevAppInitWoW64Enabled, prevAppInitWoW64); RestoreAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE"), prevAppInitEnabled, prevAppInit); } else { // if this is a 64-bit OS, it will re-direct our request to Wow6432Node anyway, so we // don't need to handle that manually RestoreAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE"), prevAppInitEnabled, prevAppInit); } if (File.Exists(regfile)) { File.Delete(regfile); } // won't recurse because it's not enabled yet toggleGlobalHook.Checked = false; toggleGlobalHook.Text = "Enable Global Hook"; toggleGlobalHook.Enabled = true; return; } pipeThread = Helpers.NewThread(new ThreadStart(PipeTick)); pipeThread.Start(); string exe = exePath.Text; string logfile = exe; if (logfile.Contains("/")) { logfile = logfile.Substring(logfile.LastIndexOf('/') + 1); } if (logfile.Contains("\\")) { logfile = logfile.Substring(logfile.LastIndexOf('\\') + 1); } if (logfile.Contains(".")) { logfile = logfile.Substring(0, logfile.IndexOf('.')); } logfile = m_Core.TempLogFilename(logfile); StaticExports.StartGlobalHook(exe, logfile, GetSettings().Options); } else { ExitPipeThread(); exePath.Enabled = exeBrowse.Enabled = workDirPath.Enabled = workDirBrowse.Enabled = cmdline.Enabled = capture.Enabled = save.Enabled = load.Enabled = true; foreach (Control c in capOptsFlow.Controls) { c.Enabled = true; } foreach (Control c in actionsFlow.Controls) { c.Enabled = true; } toggleGlobalHook.Text = "Enable Global Hook"; if (Environment.Is64BitProcess) { RestoreAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE").CreateSubKey("Wow6432Node"), prevAppInitWoW64Enabled, prevAppInitWoW64); RestoreAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE"), prevAppInitEnabled, prevAppInit); } else { // if this is a 64-bit OS, it will re-direct our request to Wow6432Node anyway, so we // don't need to handle that manually RestoreAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE"), prevAppInitEnabled, prevAppInit); } var regfile = Path.Combine(Path.GetTempPath(), "RenderDoc_RestoreGlobalHook.reg"); if (File.Exists(regfile)) { File.Delete(regfile); } } toggleGlobalHook.Enabled = true; UpdateGlobalHook(); }
private void toggleGlobalHook_CheckedChanged(object sender, EventArgs e) { if (!toggleGlobalHook.Enabled) { return; } toggleGlobalHook.Enabled = false; if (toggleGlobalHook.Checked) { if (!Helpers.IsElevated) { DialogResult res = MessageBox.Show("RenderDoc needs to restart with admin privileges. Restart?", "Restart as admin", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (res == DialogResult.Yes) { string capfile = Path.GetTempFileName() + ".cap"; AutoStart.Checked = false; SaveSettings(capfile); var process = new Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo(Application.ExecutablePath, capfile); process.StartInfo.Verb = "runas"; try { process.Start(); } catch (Exception) { // don't restart if it failed for some reason (e.g. user clicked no to UAC) toggleGlobalHook.Checked = false; toggleGlobalHook.Enabled = true; return; } m_Core.Config.Serialize(Core.ConfigFilename); m_Core.Config.ReadOnly = true; m_Core.AppWindow.Close(); return; } else { toggleGlobalHook.Checked = false; toggleGlobalHook.Enabled = true; return; } } exePath.Enabled = exeBrowse.Enabled = workDirPath.Enabled = workDirBrowse.Enabled = cmdline.Enabled = launch.Enabled = save.Enabled = load.Enabled = false; foreach (Control c in capOptsFlow.Controls) { c.Enabled = false; } foreach (Control c in actionsFlow.Controls) { c.Enabled = false; } toggleGlobalHook.Text = "Disable Global Hook"; if (StaticExports.IsGlobalHookActive()) { StaticExports.StopGlobalHook(); } string exe = exePath.Text; string logfile = exe; if (logfile.Contains("/")) { logfile = logfile.Substring(logfile.LastIndexOf('/') + 1); } if (logfile.Contains("\\")) { logfile = logfile.Substring(logfile.LastIndexOf('\\') + 1); } if (logfile.Contains(".")) { logfile = logfile.Substring(0, logfile.IndexOf('.')); } logfile = m_Core.TempLogFilename(logfile); bool success = StaticExports.StartGlobalHook(exe, logfile, GetSettings().Options); if (!success) { // tidy up and exit MessageBox.Show("Aborting. Couldn't start global hook. Check diagnostic log in help menu for more information", "Couldn't start global hook", MessageBoxButtons.OK, MessageBoxIcon.Error); exePath.Enabled = exeBrowse.Enabled = workDirPath.Enabled = workDirBrowse.Enabled = cmdline.Enabled = launch.Enabled = save.Enabled = load.Enabled = true; foreach (Control c in capOptsFlow.Controls) { c.Enabled = true; } foreach (Control c in actionsFlow.Controls) { c.Enabled = true; } // won't recurse because it's not enabled yet toggleGlobalHook.Checked = false; toggleGlobalHook.Text = "Enable Global Hook"; toggleGlobalHook.Enabled = true; return; } } else { if (StaticExports.IsGlobalHookActive()) { StaticExports.StopGlobalHook(); } exePath.Enabled = exeBrowse.Enabled = workDirPath.Enabled = workDirBrowse.Enabled = cmdline.Enabled = launch.Enabled = save.Enabled = load.Enabled = true; foreach (Control c in capOptsFlow.Controls) { c.Enabled = true; } foreach (Control c in actionsFlow.Controls) { c.Enabled = true; } toggleGlobalHook.Text = "Enable Global Hook"; } toggleGlobalHook.Enabled = true; UpdateGlobalHook(); }