GetService gives null for my DTE request
-
Wednesday, April 26, 2006 8:36 AM
I'm using following code within my Initialize function on my package.
DTE dte = GetService(typeof(DTE))
But dte is allways null. What's wrong with my code?
Also i should note that my package is loaded at startup that is I've used [ProvideAutoLoad(UIContextGuids.NoSolution)] attribute on my package
All Replies
-
Wednesday, April 26, 2006 6:24 PM
The reason for not being able to retrieve the DTE at this point is that the shell is not fully loaded - it has zombie state. The good thing is that you can get notified when the shell has finished loading. I have added sample code that demonstrates how to set an event listener for shell property changes. In short, you should implement IVsShellPropertyChanges and look for changes on __VSSPROPID.VSSPROPID_Zombie . Once you have retrieved the DTE you can stop listening to the property changes as shown below.
Thanks,
Ole
Sample c# code
public
class MyNestedPackage : ProjectPackage, IVsShellPropertyEvents{
DTE dte; 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
}
-
Thursday, April 27, 2006 6:41 AMThanks for your reply; its ok now

-fatih -
Tuesday, December 02, 2008 9:17 PMI'm having a similar problem, but I'm working on an Add-In, not a VSPackage. My _applicationObject.Solution.DTE object is empty...any ideas?
Thanks,
SB -
Wednesday, December 03, 2008 5:40 PM
More specifically, I'm having a problem in the Add-In with the following code:IVsShell shellService = GetService(typeof(SVsShell)) as IVsShell;
if (shellService != null)
ErrorHandler.ThrowOnFailure(shellService.AdviseShellPropertyChanges(this,out cookie));
The problem is that the first line is always returning a null, so the code never executes the AdviseShellPropertyChanges method.
Any idea on why the shellService is always null?
Thanks,
SB -
Friday, January 28, 2011 6:56 AM
Hi Ole,
I run the code as you mentioned above, butthe following “if” sentience return false
if ((int)__VSSPROPID.VSSPROPID_Zombie == propid)
what's wrong?
-
Friday, January 28, 2011 5:03 PMModerator
That just means it is a different property that is changing, your callback will be called for ALL properties that change, not just the Zombie property. It doesn't matter that the conditional is false on one given callback, it should be true on SOME callback, but not necessarily the very first one.
Ryan
- Proposed As Answer by andrea sang Monday, January 31, 2011 1:18 AM

