-1

我看过很多关于相关的问题,但还是觉得很困惑如何在Visual Studio Extension中使用调试API?

我现在主要遇到的问题是无法获得调试过程的当前状态,比如何时遇到断点。

我已经看到了很多可以用来IDebugEventCallback2解决问题的问题,但我是一个新手,没有难做具体的例子来了解

我从来没有写过这相关的代码,MSDN上都可发现这种信息也很少的例子,如果有一些资料或例子我会很感激.... QAQ

英语不是我的母语,可能会有一些语法错误,我感到很抱歉。

+0

时得到通知的模式改变您可以使用AdviseDebuggerEvents/UnadviseDebuggerEvents方法传递实现IVsDebuggerEvents接口的类,例如IDebugEve ntCallback2 https://github.com/Excel-DNA/VSExcel/blob/master/Source/ExcelDnaTools/DebugManager.cs –

+0

非常感谢,现在我可以在VS – liziyi

+0

的扩展中获得调试状态我很高兴知道您是否可以解决问题,可否请您发布解决方案并将其标记为答案,这对有类似问题的其他社区会有好处。 –

回答

0

这个答案是在C#中的Visual Studio包模板 文件结构如下,不同的项目名称设置可能会有所不同,但类似的基地,我已经修改了所选择的两个文件(MyControl.xaml,VSPackageHW2Package.cs ) FileStruct

1.定义从VSPackageHW2Package.cs可变

public static VSPackageHW2Package package; 
readonly IVsDebugger _debugger; 
readonly DTE _dte; 
readonly Debugger2 _dteDebugger; 
readonly uint _debuggerEventsCookie; 

2.pass值MyControl.xaml(改变VSPackageHW2Package.cs唯一的地方)

在MyControl.xaml
public VSPackageHW2Package() 
{ 
    Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString())); 
    MyControl.package = this; 
} 

3.Implement接口 IVsDebuggerEvents

4.in构造

public MyControl() 
{ 
    InitializeComponent(); 

    var packageServiceProvider = (IServiceProvider)package; 
    _debugger = packageServiceProvider.GetService(typeof(SVsShellDebugger)) as IVsDebugger; 
    _dte = packageServiceProvider.GetService(typeof(SDTE)) as DTE; 

    if (_debugger.AdviseDebuggerEvents(this, out _debuggerEventsCookie) != VSConstants.S_OK) 
    { 
     MessageBox.Show("DebugManager setup failed"); 
    } 
    else 
    { 
     MessageBox.Show("ok"); 
    } 
} 

完全MyControl.xaml文件:

using EnvDTE; 
using EnvDTE80; 
using Microsoft.VisualStudio; 
using Microsoft.VisualStudio.Shell.Interop; 
using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace Company.VSPackageHW2 
{ 
    /// <summary> 
    /// Interaction logic for MyControl.xaml 
    /// </summary> 
    public partial class MyControl : UserControl,IVsDebuggerEvents 
    { 
     public static VSPackageHW2Package package; 
     readonly IVsDebugger _debugger; 
     readonly DTE _dte; 
     readonly Debugger2 _dteDebugger; 
     readonly uint _debuggerEventsCookie; 

     public MyControl() 
     { 
      InitializeComponent(); 

      var packageServiceProvider = (IServiceProvider)package; 
      _debugger = packageServiceProvider.GetService(typeof(SVsShellDebugger)) as IVsDebugger; 
      _dte = packageServiceProvider.GetService(typeof(SDTE)) as DTE; 

      if (_debugger.AdviseDebuggerEvents(this, out _debuggerEventsCookie) != VSConstants.S_OK) 
      { 
       MessageBox.Show("DebugManager setup failed"); 
      } 
      else 
      { 
       MessageBox.Show("ok"); 
      } 
     } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")] 



     private void button1_Click(object sender, RoutedEventArgs e) 
     { 

      MessageBox.Show(string.Format(System.Globalization.CultureInfo.CurrentUICulture, "We are inside {0}.button1_Click()", this.ToString()), 
          "lzyToolWindow"); 

     } 

     public int OnModeChange(DBGMODE dbgmodeNew) 
     { 
      MessageBox.Show("debug mode change"); 
      throw new NotImplementedException(); 
     } 
    } 
}