How to programmatically launch Debugger in a remote machine

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);

     scope.Connect();

      

        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)

        {

            if (oReturn["Name"].ToString().Equals("ConsoleApplication1.exe"))

            {

                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.