~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Debugger/Debugger.AddIn/Service/AttachToProcessForm.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the BSD license (for details please see \src\AddIns\Debugger\Debugger.AddIn\license.txt)
 
3
 
 
4
using System;
 
5
using System.ComponentModel;
 
6
using System.Diagnostics;
 
7
using System.IO;
 
8
using System.Linq;
 
9
using System.Windows.Forms;
 
10
 
 
11
using ICSharpCode.Core;
 
12
using ICSharpCode.SharpDevelop.Debugging;
 
13
using ICSharpCode.SharpDevelop.Gui;
 
14
 
 
15
namespace ICSharpCode.SharpDevelop.Services
 
16
{
 
17
        public class AttachToProcessForm : AbstractAttachToProcessForm
 
18
        {
 
19
                class ProcessListViewItem : ListViewItem
 
20
                {
 
21
                        Process process;
 
22
                        bool managed;
 
23
                        
 
24
                        public ProcessListViewItem(Process process, WindowsDebugger debugger)
 
25
                        {
 
26
                                this.process = process;
 
27
                                try {
 
28
                                        var modules = process.Modules.Cast<ProcessModule>().Where(
 
29
                                                m => m.ModuleName.StartsWith("mscor", StringComparison.InvariantCultureIgnoreCase));
 
30
 
 
31
                                        managed = modules.Count() > 0;
 
32
                                } catch { }
 
33
                                
 
34
                                string fileName = Path.GetFileName(process.MainModule.FileName);
 
35
                                Text = fileName;
 
36
                                SubItems.Add(process.Id.ToString());
 
37
                                SubItems.Add(process.MainWindowTitle);
 
38
                                SubItems.Add(GetManagedString(managed));
 
39
                        }
 
40
                        
 
41
                        public Process Process {
 
42
                                get { return process; }
 
43
                        }
 
44
                        
 
45
                        public bool IsManaged {
 
46
                                get { return managed; }
 
47
                        }
 
48
                        
 
49
                        static string GetManagedString(bool managed)
 
50
                        {
 
51
                                if (managed) {
 
52
                                        return StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AttachToProcessForm.Managed}");
 
53
                                }
 
54
                                return String.Empty;
 
55
                        }
 
56
                }
 
57
                
 
58
                protected override void RefreshProcessList(ListView listView, bool showNonManaged)
 
59
                {
 
60
                        listView.Items.Clear();
 
61
                        WindowsDebugger debugger = (WindowsDebugger)DebuggerService.CurrentDebugger;
 
62
                        Process currentProcess = Process.GetCurrentProcess();
 
63
                        foreach (Process process in Process.GetProcesses()) {
 
64
                                try {
 
65
                                        if (process.HasExited) continue;
 
66
                                        // Prevent attaching to our own process.
 
67
                                        if (currentProcess.Id != process.Id) {
 
68
                                                ProcessListViewItem item = new ProcessListViewItem(process, debugger);
 
69
                                                if (showNonManaged || item.IsManaged) {
 
70
                                                        item.Tag = process;
 
71
                                                        listView.Items.Add(item);
 
72
                                                }
 
73
                                        }
 
74
                                } catch (Win32Exception) {
 
75
                                        // Do nothing.
 
76
                                }
 
77
                        }
 
78
                        listView.Sort();
 
79
                }
 
80
        }
 
81
}