0

I am trying to create a Visual Studio extension using the SDK, creating a VSIX package. I am using Microsoft Visual Studio 2019 Preview; Version 16.7.0 Preview 1.0

private readonly AsyncPackage package;

private void Test1()
{
    ThreadHelper.ThrowIfNotOnUIThread();

    var ivsSolution = (IVsSolution)Package.GetGlobalService(typeof(IVsSolution));
    var dte = (EnvDTE80.DTE2)Package.GetGlobalService(typeof(EnvDTE.DTE));
    var errorListProvider = new ErrorListProvider(package);
    var tasks = errorListProvider.Tasks.Count;
}

The last line evaluates to zero, even though the error window shows many errors. What am I doing wrong?

Thanks,

as9876
  • 934
  • 1
  • 13
  • 38
  • 1
    To access the actual error list, you may use [ToolWindows.ErrorList](https://learn.microsoft.com/en-us/dotnet/api/envdte80.toolwindows.errorlist) – Klaus Gütter May 27 '20 at 05:05
  • @KlausGütter how do I obtain _applicationObject in the example in that link? TY – as9876 May 27 '20 at 15:09

1 Answers1

1

ErrorListProvider's provide errors to the error list toolwindow. To display (or access) existing errors, you can use the ToolWindows.ErrorList as Klaus suggested. The sample code in the documentation isn't exactly correct. It looks like it was lifted from a custom add-in project, which is now obsolete. With those older add-in projects the applicationObject often referred to the DTE interface.

From a VSSDK Package you'd do something similar to the following:

using System;
using System.ComponentModel.Design;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;
using EnvDTE80;

namespace GetErrorsDemo
{

    [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
    [Guid(GetErrorsDemoPackage.PackageGuidString)]
    [ProvideMenuResource("Menus.ctmenu", 1)]
    public sealed class GetErrorsDemoPackage : AsyncPackage
    {
        public const string PackageGuidString = "90adc626-67bd-42d5-babc-6e4c5aa6e351";
        public static readonly Guid CommandSet = new Guid("5a7f888e-8767-4a4a-a06b-1b06c4f1e3f4");
        public const int ErrorsListCommand = 0x0100;

        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
        {
            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            // add our menu handler
            OleMenuCommandService commandService = await this.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
            Assumes.Present(commandService);
            var menuCommandID = new CommandID(CommandSet, ErrorsListCommand);
            var menuItem = new MenuCommand(this.OnErrorsListCommand, menuCommandID);
            commandService.AddCommand(menuItem);
        }

        private void OnErrorsListCommand(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            DTE2 dte = (DTE2)GetService(typeof(SDTE));
            Assumes.Present(dte);
            ErrorList errorList = dte.ToolWindows.ErrorList;
            for (int i = 1; i <= errorList.ErrorItems.Count; i++)
            {
                string msg = string.Format("Description: {0}", errorList.ErrorItems.Item(i).Description);
                VsShellUtilities.ShowMessageBox(this, msg, "GetErrors Demo", OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
            }
        }
    }
}
Ed Dore
  • 2,013
  • 1
  • 10
  • 8