private static void AssertVisualStudioCanBeUsed(IVisualStudio visualStudio) { string version = null; visualStudio.Call(dte => version = dte.Version); Assert.IsNotNull(version); Assert.Throws <ArgumentNullException>(() => visualStudio.Call(null)); Assert.DoesNotThrow(() => visualStudio.BringToFront()); Assert.IsNotNull(visualStudio.GetDebugger(new DebuggerSetup())); Assert.Throws <ArgumentNullException>(() => visualStudio.GetDebugger(null)); }
/// <inheritdoc /> public bool IsAttachedToProcess(Process process) { if (process == null) { throw new ArgumentNullException("process"); } bool result = false; try { // TODO: Consider all instances of Visual Studio if not attached to a particular one. IVisualStudio visualStudio = GetVisualStudio(false, logger); if (visualStudio == null) { return(false); } visualStudio.Call(dte => { EnvDTE.Process dteProcess = FindProcess(dte.Debugger.DebuggedProcesses, process); if (dteProcess != null) { result = true; } }); } catch (VisualStudioException ex) { logger.Log(LogSeverity.Debug, "Failed to detect whether Visual Studio debugger was attached to process.", ex); } return(result); }
/// <inheritdoc /> public DetachDebuggerResult DetachFromProcess(Process process) { if (process == null) { throw new ArgumentNullException("process"); } DetachDebuggerResult result = DetachDebuggerResult.AlreadyDetached; try { IVisualStudio visualStudio = GetVisualStudio(false, logger); if (visualStudio != null) { visualStudio.Call(dte => { EnvDTE.Process dteProcess = FindProcess(dte.Debugger.DebuggedProcesses, process); if (dteProcess != null) { dteProcess.Detach(false); result = DetachDebuggerResult.Detached; } }); } } catch (VisualStudioException ex) { result = DetachDebuggerResult.CouldNotDetach; logger.Log(LogSeverity.Debug, "Failed to detach Visual Studio debugger from process.", ex); } return(result); }
/// <inheritdoc /> public AttachDebuggerResult AttachToProcess(Process process) { if (process == null) { throw new ArgumentNullException("process"); } AttachDebuggerResult result = AttachDebuggerResult.CouldNotAttach; try { IVisualStudio visualStudio = GetVisualStudio(true, logger); if (visualStudio != null) { visualStudio.Call(dte => { EnvDTE.Process dteProcess = FindProcess(dte.Debugger.DebuggedProcesses, process); if (dteProcess != null) { result = AttachDebuggerResult.AlreadyAttached; } else { dteProcess = FindProcess(dte.Debugger.LocalProcesses, process); if (dteProcess != null) { Process2 dteProcess2 = dteProcess as Process2; Debugger2 dteDebugger2 = dte.Debugger as Debugger2; if (dteProcess2 != null && dteDebugger2 != null) { IList <Guid> engineGuids = GetEngineGuids(); Engine[] engines = GetEngines(dteDebugger2, engineGuids); dteProcess2.Attach2(engines); } else { dteProcess.Attach(); } result = AttachDebuggerResult.Attached; } else { logger.Log(LogSeverity.Debug, "Failed to attach Visual Studio debugger to process because it was not found in the LocalProcesses list."); } } }); } } catch (VisualStudioException ex) { logger.Log(LogSeverity.Debug, "Failed to attach Visual Studio debugger to process.", ex); } return(result); }
public static Solution OpenSolutionIfNeeded() { string solutionPath = Path.Combine(Path.GetDirectoryName( AssemblyUtils.GetAssemblyLocalPath(typeof(VisualStudioTestHarness).Assembly)), @"..\TestSolution.sln"); Solution solution = null; visualStudio.Call(dte => { if (!dte.Solution.IsOpen) { dte.Solution.Open(solutionPath); } solution = dte.Solution; }); return(solution); }
private bool NavigateToFileInVisualStudio(string path, int lineNumber, int columnNumber) { path = Path.GetFullPath(path); var logger = NullLogger.Instance; IVisualStudio visualStudio = visualStudioManager.GetVisualStudio(VisualStudioVersion.Any, true, logger); if (visualStudio == null) { return(false); } visualStudio.Call(dte => { Window window = OpenFile(dte, path); if (window == null) { window = FindFileInSolution(dte, path); } TextSelection selection = window.Selection as TextSelection; if (lineNumber != 0) { if (selection != null) { selection.MoveToLineAndOffset(lineNumber, Math.Max(1, columnNumber), false); } } window.Activate(); window.Visible = true; }); visualStudio.BringToFront(); return(true); }
private static void AssertVisualStudioCanBeUsed(IVisualStudio visualStudio) { string version = null; visualStudio.Call(dte => version = dte.Version); Assert.IsNotNull(version); Assert.Throws<ArgumentNullException>(() => visualStudio.Call(null)); Assert.DoesNotThrow(() => visualStudio.BringToFront()); Assert.IsNotNull(visualStudio.GetDebugger(new DebuggerSetup())); Assert.Throws<ArgumentNullException>(() => visualStudio.GetDebugger(null)); }
/// <inheritdoc /> public Process LaunchProcess(ProcessStartInfo processStartInfo) { if (processStartInfo == null) { throw new ArgumentNullException("processStartInfo"); } Process foundProcess = null; try { IVisualStudio visualStudio = GetVisualStudio(true, logger); if (visualStudio != null) { visualStudio.Call(dte => { List <int> currentDebuggedProcesses = new List <int>(); if (dte.Debugger.DebuggedProcesses != null) { foreach (EnvDTE.Process dteProcess in dte.Debugger.DebuggedProcesses) { currentDebuggedProcesses.Add(dteProcess.ProcessID); } } var serviceProvider = new VisualStudioServiceProvider(dte); var debugger = serviceProvider.GetService <SVsShellDebugger, IVsDebugger2>(); var engineGuids = GetEngineGuids(); int debugTargetInfoSize = Marshal.SizeOf(typeof(VsDebugTargetInfo2)); int guidSize = Marshal.SizeOf(typeof(Guid)); IntPtr debugTargetInfoPtr = Marshal.AllocCoTaskMem(debugTargetInfoSize + guidSize * engineGuids.Count); try { IntPtr engineGuidsPtr = new IntPtr(debugTargetInfoPtr.ToInt64() + debugTargetInfoSize); var environment = new StringBuilder(); foreach (DictionaryEntry variable in processStartInfo.EnvironmentVariables) { environment.Append(variable.Key).Append('=').Append(variable.Value).Append('\0'); } var debugTargetInfo = new VsDebugTargetInfo2() { cbSize = (uint)debugTargetInfoSize, bstrExe = Path.GetFullPath(processStartInfo.FileName), bstrArg = processStartInfo.Arguments, bstrCurDir = string.IsNullOrEmpty(processStartInfo.WorkingDirectory) ? Environment.CurrentDirectory : Path.GetFullPath(processStartInfo.WorkingDirectory), bstrEnv = environment.Length != 0 ? environment.ToString() : null, dwDebugEngineCount = (uint)engineGuids.Count, pDebugEngines = engineGuidsPtr, fSendToOutputWindow = 1, guidLaunchDebugEngine = VisualStudioDebugEngines.ManagedAndNative, dlo = (int)DEBUG_LAUNCH_OPERATION.DLO_CreateProcess, LaunchFlags = (int)(__VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete | __VSDBGLAUNCHFLAGS.DBGLAUNCH_Silent) }; Marshal.StructureToPtr(debugTargetInfo, debugTargetInfoPtr, false); for (int i = 0; i < engineGuids.Count; i++) { Marshal.StructureToPtr(engineGuids[i], new IntPtr(engineGuidsPtr.ToInt64() + i * guidSize), false); } int hresult = debugger.LaunchDebugTargets2(1, debugTargetInfoPtr); if (hresult != S_OK) { Marshal.ThrowExceptionForHR(hresult); } } finally { Marshal.FreeCoTaskMem(debugTargetInfoPtr); } if (dte.Debugger.DebuggedProcesses != null) { foreach (EnvDTE.Process dteProcess in dte.Debugger.DebuggedProcesses) { if (!currentDebuggedProcesses.Contains(dteProcess.ProcessID)) { foundProcess = Process.GetProcessById(dteProcess.ProcessID); break; } } } if (foundProcess == null) { logger.Log(LogSeverity.Debug, "Could not find the newly launched process."); } }); } } catch (VisualStudioException ex) { logger.Log(LogSeverity.Debug, "Failed to attach Visual Studio debugger to process.", ex); } return(foundProcess); }