'Can I use EnvDTE and VisualStudio.SDK in a .Net 6 project?
With VisualStudio 2019, I used to be able to use EnvDTE80 that comes with VS2019 installation. It worked fine within my .Net 3.1 project. Then I upgraded my project to .Net 6 using VS2022. It appears that Microsoft introduced some breaking changes. For one, EnvtDTE is now part of VisualStudio SDK that can be downloaded as a Nuget or installed using VS installer. However, after installing it in both ways, I'm unable to access Microsoft.VisualStudio namespace as well as EnvDTE. I spent a couple of days trying to look up more information, but couldn't find anything useful. Any help with this is greatly appreciated.
P.S the project is a 64-bit WPF desktop application using .Net 6.
For more context, I'm trying to open an instance of Visual Studio, load a C++ project, add files to it and build/run the project using EnvDTE:
static class VisualStudio
{
private static EnvDTE80.DTE2 _vsInstance = null;
private static readonly string _progID = "VisualStudio.DTE.17.0";
[DllImport("ole32.dll")]
private static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);
[DllImport("ole32.dll")]
private static extern int GetRunningObjectTable(uint reserved, out IRunningObjectTable pprot);
public static void OpenVisualStudio(string solutionPath)
{
IRunningObjectTable rot = null;
IEnumMoniker monikersTable = null;
IBindCtx bindCtx = null;
try
{
if (_vsInstance == null)
{
int hResult = GetRunningObjectTable(0, out rot);
if (hResult < 0 || rot == null) throw new COMException($"GetRunningObjectTable() returned HRESULT: {hResult:X8}");
rot.EnumRunning(out monikersTable);
monikersTable.Reset();
hResult = CreateBindCtx(0, out bindCtx);
if (hResult < 0) throw new COMException($"CreateBindCtx() returned HRESULT: {hResult:X8}");
IMoniker[] currentMoniker = new IMoniker[1];
while (monikersTable.Next(1, currentMoniker, IntPtr.Zero) == 0)
{
string name = "";
currentMoniker[0]?.GetDisplayName(bindCtx, null, out name);
if (name.Contains(_progID))
{
hResult = rot.GetObject(currentMoniker[0], out object obj);
if (hResult < 0) throw new COMException($"Running Object Table GetObject() returned HRESULT: {hResult:X8}");
EnvDTE80.DTE2 dte = obj as EnvDTE80.DTE2;
var solutionName = string.Empty;
solutionName = dte.Solution.FullName;
if (solutionName == solutionPath)
{
_vsInstance = dte;
break;
}
}
}
if (_vsInstance == null)
{
Type visualStudioType = Type.GetTypeFromProgID(_progID, true);
_vsInstance = Activator.CreateInstance(visualStudioType) as EnvDTE80.DTE2;
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
if (monikersTable != null)
{
Marshal.ReleaseComObject(monikersTable);
}
if (rot != null)
{
Marshal.ReleaseComObject(rot);
}
if (bindCtx != null)
{
Marshal.ReleaseComObject(bindCtx);
}
}
}
public static void BuildSolution(string solutionPath)
{
OpenVisualStudio(solutionPath);
if (!_vsInstance.Solution.IsOpen)
_vsInstance.Solution.Open(solutionPath);
_vsInstance.MainWindow.Visible = true;
var configName = "Debug"; // Or Release
_vsInstance.Solution.SolutionBuild.SolutionConfigurations.Item(configName).Activate();
_vsInstance.ExecuteCommand("Build.BuildSolution");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|