Ejemplo n.º 1
0
        public int OnCustomDebugEvent(ref Guid ProcessId, VsComponentMessage message)
        {
            try {
                VsPackageMessage code = (VsPackageMessage)message.MessageCode;
                switch (code)
                {
                case VsPackageMessage.AttachToChild:
                    int     parentId = (int)message.Parameter1;
                    int     childId  = (int)message.Parameter2;
                    Process proc     = Process.GetProcessById(childId);
                    if (proc != null)
                    {
                        DebugAttach.AttachToProcess(new Process[] { proc }, ChildDebuggingMode.AlwaysAttach);
                    }
                    break;

                case VsPackageMessage.IsChildDebuggingEnabled:
                    Guid processGuid    = (Guid)message.Parameter1;
                    Guid connectionGuid = (Guid)message.Parameter2;
                    DkmTransportConnection connection = DkmTransportConnection.FindConnection(connectionGuid);
                    if (connection != null)
                    {
                        if (IsChildDebuggingEnabledByDefault)
                        {
                            DkmCustomMessage response = DkmCustomMessage.Create(
                                connection,
                                null,
                                PackageServices.VsDebuggerMessageGuid,
                                (int)VsDebuggerMessage.EnableChildProcessDebugging,
                                processGuid,
                                null
                                );
                            response.SendLower();
                        }
                        else
                        {
                            Logger.Log(
                                "Not enabling child process debugging for process {0}.  " +
                                "Child debugging is disabled by default.",
                                processGuid);
                        }
                    }
                    break;
                }
            } catch (Exception exception) {
                Logger.LogException(
                    exception,
                    "An error occured while handling a VsPackage message.  HR=0x{0:X}",
                    exception.HResult);
            }
            return(0);
        }
        public void Execute(object sender, EventArgs e)
        {
            var dte = (EnvDTE.DTE)_visualStudioPackageProvider.Package.DTE; //GetService(typeof(EnvDTE.DTE));

            var uiShell    = _visualStudioPackageProvider.Package.VsUIShell;
            var parentHwnd = IntPtr.Zero;

            uiShell.GetDialogOwnerHwnd(out parentHwnd);

            var parentShim = new NativeWindow();

            parentShim.AssignHandle(parentHwnd);
            var dialog = new AttachDialog();
            var result = dialog.ShowDialog(parentShim);

            if (result == DialogResult.OK)
            {
                HashSet <Process> processes = new HashSet <Process>();
                foreach (int pid in dialog.SelectedItems)
                {
                    Process p = Process.GetProcessById(pid);
                    if (!p.IsBeingDebugged())
                    {
                        processes.Add(p);
                    }

                    if (dialog.AutoAttachToCurrentChildren)
                    {
                        foreach (Process child in p.GetChildren())
                        {
                            if (!child.IsBeingDebugged())
                            {
                                processes.Add(child);
                            }
                        }
                    }
                }
                List <Process>     processList = new List <Process>(processes);
                ChildDebuggingMode mode        = (dialog.AutoAttachToFutureChildren)
            ? ChildDebuggingMode.AlwaysAttach
            : ChildDebuggingMode.UseDefault;
                DebugAttach.AttachToProcess(processList.ToArray(), mode);
            }
        }