-1

I have seen a lot about the related issues, but still feel very puzzled

I now mainly encountered the problem is unable to obtain the current state of the debugging process, such as when to encounter breakpoints.

I have seen a lot of problems that can be used IDebugEventCallback2 to solve the problem, but I was a novice, no specific examples difficult to understand

I have never written this related code, MSDN can be found on the information is also very few examples, if there are some information or examples I would be very grateful....QAQ

English is not my mother tongue, there may be some grammatical mistakes and i feel Sorry for it.

liziyi
  • 1
  • 1
  • To be notified when the mode changes you can use the AdviseDebuggerEvents / UnadviseDebuggerEvents methods passing a class that implements the IVsDebuggerEvents interface, such as IDebugEventCallback2 https://github.com/Excel-DNA/VSExcel/blob/master/Source/ExcelDnaTools/DebugManager.cs – Zhanglong Wu - MSFT Apr 06 '17 at 02:08
  • thank you so much,now i can get the debug status in the extension in VS – liziyi Apr 06 '17 at 04:26
  • I am glad to know that you resolve the issue, could you please Post your solution and mark it as answer, which will be beneficial to other communities who have the similar issue. – Zhanglong Wu - MSFT Apr 06 '17 at 05:28

1 Answers1

0

this answer is base on the Visual Studio Package template in C# File structure is as follows, different project name settings may be different but similar, I have made changes to the selected two documents (MyControl.xaml, VSPackageHW2Package.cs) FileStruct

1.Define the variable

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

2.pass value from VSPackageHW2Package.cs to MyControl.xaml(the only place to change VSPackageHW2Package.cs)

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

3.Implement interface in MyControl.xaml IVsDebuggerEvents

4.in Constructor

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");
    }
}

Complete MyControl.xaml file:

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();
        }
    }
}
liziyi
  • 1
  • 1
  • In the production code, one should implement IVsDebuggerEvents in a non-user control class control to separate business logic from UI code and call IVsDebugger.UnadviseDebuggerEvents in VSPackage.Dispose – Mikhail Filimonov Jul 08 '19 at 10:47