Null DTE when VSIP Package is Auto Loaded on Startup

Print Friendly

In my VSIP package, I’ve mark with the following attribute to indicade it should load at startup:

[ProvideAutoLoad(UIContextGuids.NoSolution)]

You can find details about other auto load options on my other post.
And then in initialize function I’ve try to get DTE object with the following code .

DTE dte = GetService(typeof(DTE))

But DTE is allways null. What’s wrong with my code?

    In fact, the problem is since shell is not fully loaded, DTE object is unavaliable. That was the bad news. But with every bas news, there should be good also :)
    Visaul Studio would notify us when it finished loading via IVsShell interface. We should listen property changes on __VSSPROPID.VSSPROPID_Zombie. When it changes from true to false; its time to retrieve DTE object. Here is the sample code that explains how to get notified when VS fully loaded :

public class MyNestedPackage : ProjectPackage, IVsShellPropertyEvents {    private DTE dte;    private uint cookie;
    protected override void Initialize() {       base.Initialize();
       //Set an eventlistener for shell property changes since we ant to know when the zombie state changes from true to false       IVsShell shellService = GetService(typeof(SVsShell)) as IVsShell;
       if (shellService != null)            ErrorHandler.ThrowOnFailure(shellService.AdviseShellPropertyChanges(this, out cookie));       //Continue initialize ...       this.RegisterProjectFactory(new MyNestedProjectFactory(this));    }
    #region IVsShellPropertyEvents Members
    public int OnShellPropertyChange(int propid, object var) {        // If zombie state changes from true to false we can go ahead and        // ask for the DTE service and then stop listening for property changes        if ((int)__VSSPROPID.VSSPROPID_Zombie == propid) {            if ((bool)var == false) {                this.dte = GetService(typeof(DTE)) as DTE;                IVsShell shellService = GetService(typeof(SVsShell)) as IVsShell;                if (shellService != null)                    ErrorHandler.ThrowOnFailure(shellService.UnadviseShellPropertyChanges(this.cookie));                this.cookie = 0;            }        }
        return VSConstants.S_OK;    }    #endregion}

        Note that, this code is not written by me. Orginal author is Ole Preisler – MSFT and the code is send by him to my question on MSDN forums.

Related posts:

  1. Visual Studio 2005 Deneysel kayıt Kütüğünü Sıfırlamak
  2. Quick Open Add-in for Visual Studio
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!

Leave a Reply

 
QR Code Business Card