1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 using System.Diagnostics;
7 using System.Globalization;
8 using System.Runtime.InteropServices;
9 using System.ComponentModel.Design;
10 using Microsoft.Win32;
11 using Microsoft.VisualStudio;
12 using Microsoft.VisualStudio.Shell.Interop;
13 using Microsoft.VisualStudio.OLE.Interop;
14 using Microsoft.VisualStudio.Shell;
15 using System.Windows.Forms;
17 namespace ChromeDebug {
19 /// This is the class that implements the package exposed by this assembly.
21 /// The minimum requirement for a class to be considered a valid package for Visual Studio
22 /// is to implement the IVsPackage interface and register itself with the shell.
23 /// This package uses the helper classes defined inside the Managed Package Framework (MPF)
24 /// to do it: it derives from the Package class that provides the implementation of the
25 /// IVsPackage interface and uses the registration attributes defined in the framework to
26 /// register itself and its components with the shell.
28 // This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
30 [PackageRegistration(UseManagedResourcesOnly = true)]
31 // This attribute is used to register the information needed to show this package
32 // in the Help/About dialog of Visual Studio.
33 [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
34 // This attribute is needed to let the shell know that this package exposes some menus.
35 [ProvideMenuResource("Menus.ctmenu", 1)]
36 [Guid(GuidList.guidChromeDebugPkgString)]
37 public sealed class ChromeDebugPackage : Package {
39 /// Default constructor of the package.
40 /// Inside this method you can place any initialization code that does not require
41 /// any Visual Studio service because at this point the package object is created but
42 /// not sited yet inside Visual Studio environment. The place to do all the other
43 /// initialization is the Initialize method.
45 public ChromeDebugPackage() {
46 Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}",
52 /////////////////////////////////////////////////////////////////////////////
53 // Overridden Package Implementation
54 #region Package Members
57 /// Initialization of the package; this method is called right after the package is sited, so this is the place
58 /// where you can put all the initialization code that rely on services provided by VisualStudio.
60 protected override void Initialize() {
61 Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
64 // Add our command handlers for menu (commands must exist in the .vsct file)
65 OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
67 // Create the command for the menu item.
68 CommandID menuCommandID = new CommandID(GuidList.guidChromeDebugCmdSet, (int)PkgCmdIDList.cmdidAttachToProcess);
69 MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
70 mcs.AddCommand(menuItem);
76 /// This function is the callback used to execute a command when the a menu item is clicked.
77 /// See the Initialize method to see how the menu item is associated to this function using
78 /// the OleMenuCommandService service and the MenuCommand class.
80 private void MenuItemCallback(object sender, EventArgs e) {
81 // Show a Message Box to prove we were here
82 EnvDTE.DTE dte = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));
84 IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
85 Guid clsid = Guid.Empty;
86 IntPtr parentHwnd = IntPtr.Zero;
87 uiShell.GetDialogOwnerHwnd(out parentHwnd);
89 NativeWindow parentShim = new NativeWindow();
90 parentShim.AssignHandle(parentHwnd);
91 AttachDialog dialog = new AttachDialog();
92 DialogResult result = dialog.ShowDialog(parentShim);
93 if (result == DialogResult.OK) {
94 foreach (int selected_id in dialog.SelectedItems) {
95 foreach (EnvDTE90a.Process4 p in dte.Debugger.LocalProcesses) {
96 System.Diagnostics.Debug.WriteLine("Found process {0}", p.ProcessID);
97 if (p.ProcessID != selected_id)
100 System.Diagnostics.Debug.WriteLine("Attaching to process successful.");