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. 4 5using System; 6using System.Diagnostics; 7using System.Globalization; 8using System.Runtime.InteropServices; 9using System.ComponentModel.Design; 10using Microsoft.Win32; 11using Microsoft.VisualStudio; 12using Microsoft.VisualStudio.Shell.Interop; 13using Microsoft.VisualStudio.OLE.Interop; 14using Microsoft.VisualStudio.Shell; 15using System.Windows.Forms; 16 17namespace ChromeDebug { 18 /// <summary> 19 /// This is the class that implements the package exposed by this assembly. 20 /// 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. 27 /// </summary> 28 // This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is 29 // a package. 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 { 38 /// <summary> 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. 44 /// </summary> 45 public ChromeDebugPackage() { 46 Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", 47 this.ToString())); 48 } 49 50 51 52 ///////////////////////////////////////////////////////////////////////////// 53 // Overridden Package Implementation 54 #region Package Members 55 56 /// <summary> 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. 59 /// </summary> 60 protected override void Initialize() { 61 Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString())); 62 base.Initialize(); 63 64 // Add our command handlers for menu (commands must exist in the .vsct file) 65 OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; 66 if (null != mcs) { 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); 71 } 72 } 73 #endregion 74 75 /// <summary> 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. 79 /// </summary> 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)); 83 84 IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell)); 85 Guid clsid = Guid.Empty; 86 IntPtr parentHwnd = IntPtr.Zero; 87 uiShell.GetDialogOwnerHwnd(out parentHwnd); 88 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) 98 continue; 99 p.Attach(); 100 System.Diagnostics.Debug.WriteLine("Attaching to process successful."); 101 break; 102 } 103 } 104 } 105 } 106 } 107} 108