ReAttach.ReAttachDebugger.ReAttach C# (CSharp) Метод

ReAttach() публичный Метод

public ReAttach ( ReAttachTarget target ) : bool
target ReAttach.Data.ReAttachTarget
Результат bool
        public bool ReAttach(ReAttachTarget target)
        {
            if (target == null)
                return false;
            List<Process3> candidates;
            if (!target.IsLocal)
            {
                var transport = _dteDebugger.Transports.Item("Default");
                var processes = _dteDebugger.GetProcesses(transport, target.ServerName).OfType<Process3>();
                candidates = processes.Where(p => p.Name == target.ProcessPath).ToList();
            }
            else
            {
                var processes = _dteDebugger.LocalProcesses.OfType<Process3>();

                var tmp = processes.Select(p => new { Name = p.Name, UserName = p.UserName }).ToArray();
                Console.WriteLine(tmp);

                candidates = processes.Where(p =>
                    p.Name == target.ProcessPath &&
                    p.UserName == target.ProcessUser).ToList();

                if (!candidates.Any()) // Do matching on processes running in exclusive mode.
                {
                    candidates = processes.Where(p =>
                        p.Name == target.ProcessName &&
                        string.IsNullOrEmpty(p.UserName)).ToList();
                }
            }

            if (!candidates.Any())
                return false;

            Process3 process = null; // First try to use the pid.
            if (target.ProcessId > 0)
                process = candidates.FirstOrDefault(p => p.ProcessID == target.ProcessId);

            // If we don't have an exact match, just go for the highest PID matching.
            if (process == null)
            {
                var maxPid = candidates.Max(p => p.ProcessID);
                process = candidates.FirstOrDefault(p => p.ProcessID == maxPid);
            }

            if (process == null)
                return false;

            try
            {
                if (target.Engines != null && target.Engines.Any())
                {
                    var engines = target.Engines.Where(e => _engines.ContainsKey(e)).Select(e => _engines[e]).ToArray();
                    process.Attach2(engines);
                }
                else
                {
                    process.Attach();
                }
                return true;
            }
            catch (COMException e)
            {
                _package.Reporter.ReportError("Unable to ReAttach to process {0} ({1}) based on target {2}. Message: {3}.",
                    process.Name, process.ProcessID, target, e.Message);

                // It's either this or returning this HRESULT to shell with Shell.ReportError method, shows UAC box btw.
                const int E_ELEVATION_REQUIRED = unchecked((int)0x800702E4);
                Marshal.ThrowExceptionForHR(E_ELEVATION_REQUIRED);
                return false;
            }
            catch (Exception e)
            {
                _package.Reporter.ReportError("Unable to ReAttach to process {0} ({1}) based on target {2}. Message: {3}.",
                    process.Name, process.ProcessID, target, e.Message);
            }
            return false;
        }

Usage Example

Пример #1
0
        private void ReAttachCommandClicked(object sender, EventArgs e)
        {
            var command = sender as OleMenuCommand;

            if (command == null)
            {
                ReAttachUtils.ShowError("ReAttach failed.", "Click was sent from non-ole command.");
                return;
            }

            var index             = command.CommandID.ID - ReAttachConstants.ReAttachCommandId;
            var unAttachedTargets = _history.GetUnAttached();
            var target            = index < unAttachedTargets.Length ? unAttachedTargets[index] : null;

            if (target == null)
            {
                return;
            }

            if (_options.BuildBeforeReAttach)
            {
                TryBuildSolution();
            }

            if (!EnsureDebuggerService())
            {
                ReAttachUtils.ShowError("ReAttach failed.", "Unable to obtain ref to debugger service.");
                return;
            }

            var result = _debugger.ReAttach(target);

            if (result == ReAttachResult.NotStarted)
            {
                var dialog = new WaitingDialog(_debugger, target);
                dialog.ShowModal();
                result = dialog.Result;
            }

            switch (result)
            {
            case ReAttachResult.Success:
                break;

            case ReAttachResult.ElevationRequired:
                ReAttachUtils.ShowElevationDialog();
                break;

            case ReAttachResult.NotStarted:
                ReAttachUtils.ShowError("ReAttach failed.", "Process not started.");
                break;

            case ReAttachResult.Cancelled:
                break;

            case ReAttachResult.Failed:
                ReAttachUtils.ShowError("ReAttach failed.", "Failed reattaching to process.");
                break;
            }
        }