2

I am using reference of EnvDTE80 in my code to open a visual studio solution and then traverse through the projects present in it. I am new to this and am using the below code snippet : Firstly defined an object of the below type :

EnvDTE80.DTE2 dte2;

then tried to access the solution through it :

Solution2 solution = dte2.Solution as E2.Solution2;
if (solution == null)
{
return;
}

Projects projects = solution.Projects;
foreach (E1.Project project in projects)
{
Property outputPath =
project.ConfigurationManager.ActiveConfiguration.Properties
.Item("outputPath");
outputPath.Value = buildFolderPath;
project.Save(project.FullName);
}

Basically I am trying to change the project output path through the code snippet and whenever I run the code I get an error stating that "Object Reference not set to an instance of an object". The "dte2" object is null.

Any suggestions how to initialize it ?

priya
  • 31
  • 4
  • You did not initialize your `dte2` variable, so it is `null`. You need to _get an instance_ of that interface from somewhere, like `dte2 = GetService(typeof(DTE2));`, but I currently don't know which service provider is availabe in your code. – René Vogt Jan 23 '17 at 10:19
  • Your exception's stack trace should clearly show that it's not `dte2.Solution` which is null, but `dte2` itself. – René Vogt Jan 23 '17 at 10:20
  • https://msdn.microsoft.com/en-us/library/68shb4dw.aspx – René Vogt Jan 23 '17 at 10:21
  • @RenéVogt Yes the dte2 itself is null. And I tried to initialize it using **(EnvDTE80.DTE2)System.Runtime.InteropServices. Marshal.GetActiveObject("VisualStudio.DTE.12.0");** But it gives me an exception saying "Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING)) " – priya Jan 23 '17 at 12:40
  • Go, read, do what it says, and if you don't figure out how to fix this, you can [edit] to let us know what you did and what happened and we can reopen this. –  Jan 23 '17 at 14:46
  • @Will Aware about the Null Reference Exception. I needed help with how to initialize the EnvDTE2 object. I have solved it now by using **IRunningObjectTable and IEnumMonikerpassing** and getting the display name of the moniker and appending the process id of the same Visual Studio instance to get the Active Object. `!VisualStudio.DTE.9.0:" + pId.ToString());` Thanks for your help anyway. – priya Jan 24 '17 at 05:58
  • @RenéVogt Thanks. Your suggestions somehow led me to the solution. – priya Jan 24 '17 at 05:58
  • You should add this comment http://stackoverflow.com/questions/41803809/how-to-initialize-envdte80-dte2-object-to-access-the-solution?noredirect=1#comment70800762_41803809 as an [edit] to your question. That's the crucial bit of info it is lacking. Then you can add an answer with some detail about how you used the ROT and accept it as correct. –  Jan 24 '17 at 16:47

1 Answers1

0

For future googler's, this is how to instantiate

        var slnPath = @"C:\Code\public\src\website.sln";
        var envDteType = Type.GetTypeFromProgID("VisualStudio.DTE.15.0");

        var envDte = Activator.CreateInstance(envDteType, true);
        var dte2 = (DTE2)envDte;

        var solution = (Solution4) dte2.Solution;
        solution.Open(slnPath);
fiat
  • 15,501
  • 9
  • 81
  • 103