The C# team posts answers to common questions and describes new language features
This blog post explains how to kick off a debugger in a remote machine, programmatically. We are going to use WMI interfaces to achieve this. Use WMI, to start the debuggee process and attach a registered debugger to it using WMI. I have given a sample code below.
This Code assumes that we have the ConsoleApplication1 process running in the Machine1(debuggee Machine). On executing this code , the registered Debugger instance will be attached to “ConsoleApplication1” process. If you have multiple debuggers registered in the debuggee machine, then “Select Debugger” dialog would come up. We can use this method to attach Debugger to any kind of processes/services.
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\machine1\\root\\cimv2", options);
ObjectQuery oQuery = new ObjectQuery("Select * from Win32_Process where Name Like 'ConsoleApp%'");
//Execute the query
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(scope,oQuery);
//Get the results
ManagementObjectCollection objReturnCollection = objSearcher.Get();
foreach (ManagementObject oReturn in objReturnCollection)
object outparams = oReturn.InvokeMethod("AttachDebugger", null);
This code reads through the active processes in the remote machine using WMI, and there is a build method of invoking the process while attaching a debugger to it. To learn more about using WMI, here.
So when you put this code in a program, do you only put it in temporarily or do you leave it there, but wrap it in #if DEBUG
just use the DEBUG thanks