예제 #1
0
        public static VisualStudioDebugger GetAttached()
        {
            if (!System.Diagnostics.Debugger.IsAttached)
            {
                return(null);
            }

            var context = new STAContext();

            var instance = GetFirstOrDefaultDTE(context, x =>
            {
                // Try multiple time, as DTE might report it is busy
                var debugger = x.DTE.Debugger;
                if (debugger.DebuggedProcesses == null)
                {
                    return(false);
                }

                return(debugger.DebuggedProcesses.OfType <EnvDTE.Process>().Any(debuggedProcess => debuggedProcess.ProcessID == System.Diagnostics.Process.GetCurrentProcess().Id));
            });

            if (instance.DTE == null)
            {
                context.Dispose();
                return(null);
            }

            return(new VisualStudioDebugger(context, instance.DTE, instance.ProcessId));
        }
예제 #2
0
        public static VisualStudioDebugger GetByProcess(int processId)
        {
            var context = new STAContext();

            var instance = GetFirstOrDefaultDTE(context, x => x.ProcessId == processId);

            if (instance.DTE == null)
            {
                context.Dispose();
                return(null);
            }

            return(new VisualStudioDebugger(context, instance.DTE, instance.ProcessId));
        }
예제 #3
0
        private static VisualStudioDTE.Instance GetFirstOrDefaultDTE(STAContext context, Func <VisualStudioDTE.Instance, bool> predicate)
        {
            return(context.Execute(() =>
            {
                // Locate all Visual Studio DTE
                var dtes = VisualStudioDTE.GetActiveDTEs().ToArray();

                // Find DTE
                MessageFilter.Register();
                var result = dtes.FirstOrDefault(predicate);
                MessageFilter.Revoke();

                return result;
            }));
        }
예제 #4
0
 private VisualStudioDebugger(STAContext context, DTE dte, int processId)
 {
     this.context   = context;
     this.dte       = dte;
     this.ProcessId = processId;
 }