Best Vstest code snippet using Microsoft.TestPlatform.AttachVS.DebuggerUtility.GetVsFromPid
AttachVs.cs
Source:AttachVs.cs
...24 }25 var process = Process.GetProcessById(pid.Value);26 Trace($"Starting with pid '{pid}({process.ProcessName})', and vsPid '{vsPid}'");27 Trace($"Using pid: {pid} to get parent VS.");28 var vs = GetVsFromPid(Process.GetProcessById(vsPid ?? process.Id));29 if (vs != null)30 {31 Trace($"Parent VS is {vs.ProcessName} ({vs.Id}).");32 AttachTo(process, vs);33 return true;34 }35 Trace($"Parent VS not found, finding the first VS that started.");36 var firstVs = Process.GetProcesses()37 .Where(p => p.ProcessName == "devenv")38 .Select(p =>39 {40 try41 {42 return new { Process = p, p.StartTime, p.HasExited };43 }44 catch45 {46 return null;47 }48 })49 .Where(p => p != null && !p.HasExited)50 .OrderBy(p => p!.StartTime)51 .FirstOrDefault();52 if (firstVs != null)53 {54 Trace($"Found VS {firstVs.Process.Id}");55 AttachTo(process, firstVs.Process);56 return true;57 }58 Trace("Could not find any started VS.");59 }60 catch (Exception ex)61 {62 Trace($"ERROR: {ex}, {ex.StackTrace}");63 }64 return false;65 }66 private static void AttachTo(Process process, Process vs)67 {68 var attached = AttachVs(vs, process.Id);69 if (attached)70 {71 // You won't see this in DebugView++ because at this point VS is already attached and all the output goes into Debug window in VS.72 Trace($"SUCCESS: Attached process: {process.ProcessName} ({process.Id})");73 }74 else75 {76 Trace($"FAIL: Could not attach process: {process.ProcessName} ({process.Id})");77 }78 }79 private static bool AttachVs(Process vs, int pid)80 {81 IBindCtx? bindCtx = null;82 IRunningObjectTable? runninObjectTable = null;83 IEnumMoniker? enumMoniker = null;84 try85 {86 var r = CreateBindCtx(0, out bindCtx);87 Marshal.ThrowExceptionForHR(r);88 if (bindCtx == null)89 {90 Trace($"BindCtx is null. Cannot attach VS.");91 return false;92 }93 bindCtx.GetRunningObjectTable(out runninObjectTable);94 if (runninObjectTable == null)95 {96 Trace($"RunningObjectTable is null. Cannot attach VS.");97 return false;98 }99 runninObjectTable.EnumRunning(out enumMoniker);100 if (enumMoniker == null)101 {102 Trace($"EnumMoniker is null. Cannot attach VS.");103 return false;104 }105 var dteSuffix = ":" + vs.Id;106 var moniker = new IMoniker[1];107 while (enumMoniker.Next(1, moniker, IntPtr.Zero) == 0 && moniker[0] != null)108 {109 moniker[0].GetDisplayName(bindCtx, null, out string dn);110 if (dn.StartsWith("!VisualStudio.DTE.") && dn.EndsWith(dteSuffix))111 {112 object dbg, lps;113 runninObjectTable.GetObject(moniker[0], out object dte);114 // The COM object can be busy, we retry few times, hoping that it won't be busy next time.115 for (var i = 0; i < 10; i++)116 {117 try118 {119 dbg = dte.GetType().InvokeMember("Debugger", BindingFlags.GetProperty, null, dte, null, CultureInfo.InvariantCulture)!;120 lps = dbg.GetType().InvokeMember("LocalProcesses", BindingFlags.GetProperty, null, dbg, null, CultureInfo.InvariantCulture)!;121 var lpn = (System.Collections.IEnumerator)lps.GetType().InvokeMember("GetEnumerator", BindingFlags.InvokeMethod, null, lps, null, CultureInfo.InvariantCulture)!;122 while (lpn.MoveNext())123 {124 var pn = Convert.ToInt32(lpn.Current.GetType().InvokeMember("ProcessID", BindingFlags.GetProperty, null, lpn.Current, null, CultureInfo.InvariantCulture), CultureInfo.InvariantCulture);125 if (pn == pid)126 {127 lpn.Current.GetType().InvokeMember("Attach", BindingFlags.InvokeMethod, null, lpn.Current, null, CultureInfo.InvariantCulture);128 return true;129 }130 }131 }132 // Catch the exception if it is COMException coming directly, or coming from methodInvocation, otherwise just let it be.133 catch (Exception ex) when (ex is COMException || (ex is TargetInvocationException tie && tie.InnerException is COMException))134 {135 Trace($"ComException: Retrying in 250ms.\n{ex}");136 Thread.Sleep(250);137 }138 }139 Marshal.ReleaseComObject(moniker[0]);140 break;141 }142 Marshal.ReleaseComObject(moniker[0]);143 }144 return false;145 }146 finally147 {148 if (enumMoniker != null)149 {150 try151 {152 Marshal.ReleaseComObject(enumMoniker);153 }154 catch { }155 }156 if (runninObjectTable != null)157 {158 try159 {160 Marshal.ReleaseComObject(runninObjectTable);161 }162 catch { }163 }164 if (bindCtx != null)165 {166 try167 {168 Marshal.ReleaseComObject(bindCtx);169 }170 catch { }171 }172 }173 }174 private static Process? GetVsFromPid(Process process)175 {176 var parent = process;177 while (!IsVsOrNull(parent))178 {179 parent = GetParentProcess(parent);180 }181 return parent;182 }183 private static bool IsVsOrNull([NotNullWhen(false)] Process? process)184 {185 if (process == null)186 {187 Trace("Parent process is null..");188 return true;...
GetVsFromPid
Using AI Code Generation
1using System;2using System.Diagnostics;3using Microsoft.TestPlatform.AttachVS;4{5 {6 static void Main(string[] args)7 {8 DebuggerUtility debuggerUtility = new DebuggerUtility();9 debuggerUtility.GetVsFromPid(1234);10 }11 }12}13using System;14using System.Diagnostics;15using Microsoft.TestPlatform.AttachVS;16{17 {18 static void Main(string[] args)19 {20 DebuggerUtility debuggerUtility = new DebuggerUtility();21 debuggerUtility.GetVsFromPid(1234, "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\devenv.exe");22 }23 }24}25using System;26using System.Diagnostics;27using Microsoft.TestPlatform.AttachVS;28{29 {30 static void Main(string[] args)31 {32 DebuggerUtility debuggerUtility = new DebuggerUtility();33 debuggerUtility.GetVsFromPid(1234, "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\devenv.exe", "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\devenv.exe");34 }35 }36}37using System;38using System.Diagnostics;39using Microsoft.TestPlatform.AttachVS;40{41 {42 static void Main(string[] args)43 {44 DebuggerUtility debuggerUtility = new DebuggerUtility();45 debuggerUtility.GetVsFromPid(1234, "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\devenv.exe", "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\devenv.exe", "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\devenv.exe");46 }47 }48}
GetVsFromPid
Using AI Code Generation
1using Microsoft.TestPlatform.AttachVS;2using System;3using System.Diagnostics;4{5 {6 static void Main(string[] args)7 {8 int pid = Process.GetCurrentProcess().Id;9 var vs = DebuggerUtility.GetVsFromPid(pid);10 if (vs != null)11 {12 Console.WriteLine("The Visual Studio instance is not null");13 }14 {15 Console.WriteLine("The Visual Studio instance is null");16 }17 }18 }19}
GetVsFromPid
Using AI Code Generation
1using System;2using System.Diagnostics;3using System.Runtime.InteropServices;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine("Enter the process id of the process to be debugged.");9 int pid = Convert.ToInt32(Console.ReadLine());10 Process vsProcess = Microsoft.TestPlatform.AttachVS.DebuggerUtility.GetVsFromPid(pid);11 Console.WriteLine("Visual Studio process id: " + vsProcess.Id);12 Console.ReadLine();13 }14 }15}16Note: The GetVsFromPid method is available only in Microsoft.TestPlatform.AttachVS.dll assembly. The assembly is available in the following location: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\Microsoft.TestPlatform.AttachVS.dll17Note: The GetVsFromPid method is available only in Microsoft.TestPlatform.AttachVS.dll assembly. The assembly is available in the following location: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWind
GetVsFromPid
Using AI Code Generation
1{2 {3 static void Main(string[] args)4 {5 var vs = Microsoft.TestPlatform.AttachVS.DebuggerUtility.GetVsFromPid(1234);6 }7 }8}9{10 {11 static void Main(string[] args)12 {13 var vs = Microsoft.TestPlatform.AttachVS.DebuggerUtility.GetVsFromPid(1234);14 }15 }16}17{18 {19 static void Main(string[] args)20 {21 var vs = Microsoft.TestPlatform.AttachVS.DebuggerUtility.GetVsFromPid(1234);22 }23 }24}25{26 {27 static void Main(string[] args)28 {29 var vs = Microsoft.TestPlatform.AttachVS.DebuggerUtility.GetVsFromPid(1234);30 }31 }32}33{34 {35 static void Main(string[] args)36 {37 var vs = Microsoft.TestPlatform.AttachVS.DebuggerUtility.GetVsFromPid(1234);38 }39 }40}41{42 {43 static void Main(string[] args)44 {
GetVsFromPid
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.TestPlatform.AttachVS;7{8 {9 static void Main(string[] args)10 {11 var vs = DebuggerUtility.GetVsFromPid(12345);12 vs.LaunchDebugTarget("C:\\code\\test\\test.exe");13 }14 }15}16using System;17using System.Collections.Generic;18using System.Linq;19using System.Text;20using System.Threading.Tasks;21using Microsoft.TestPlatform.AttachVS;22{23 {24 static void Main(string[] args)25 {26 var vs = DebuggerUtility.GetVsFromPid(12345);27 vs.LaunchDebugTarget("C:\\code\\test\\test.exe");28 }29 }30}31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using System.Threading.Tasks;36using Microsoft.TestPlatform.AttachVS;37{38 {39 static void Main(string[] args)40 {41 var vs = DebuggerUtility.GetVsFromPid(12345);42 vs.LaunchDebugTarget("C:\\code\\test\\test.exe");43 }44 }45}
GetVsFromPid
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Diagnostics;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7using Microsoft.TestPlatform.AttachVS;8{9 {10 static void Main(string[] args)11 {12 int testhostPid = 0;13 int vsPid = 0;14 if (args.Length > 0)15 {16 testhostPid = int.Parse(args[0]);17 }18 if (testhostPid > 0)19 {20 vsPid = DebuggerUtility.GetVsFromPid(testhostPid);21 }22 Console.WriteLine("Testhost process id is {0}", testhostPid);23 Console.WriteLine("VS process id is {0}", vsPid);24 }25 }26}
GetVsFromPid
Using AI Code Generation
1using Microsoft.TestPlatform.AttachVS;2using System;3{4 {5 static void Main(string[] args)6 {7 int pid = 0;8 string vsVersion = string.Empty;9 Console.WriteLine("Enter process Id:");10 pid = Convert.ToInt32(Console.ReadLine());11 vsVersion = DebuggerUtility.GetVsFromPid(pid);12 Console.WriteLine("Visual Studio version from process Id is: " + vsVersion);13 Console.ReadLine();14 }15 }16}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!