Search code examples
visual-studiovisual-studio-extensionsenvdte

How can I detect an unmanaged C++ project in a visual studio extension


In my Visual Studio Extension, I need to detect whether a C++ project is managed or unmanaged code.

Previously, I had a satisfactory method, described in this posting in an MSDN forum.

In that example, it was necessary to get the ManagedExtensions property of the active configuration.

Sub Macro1() 

  Dim objProject As EnvDTE.Project 
  Dim objConfiguration As EnvDTE.Configuration 
  Dim objProperty As EnvDTE.Property 

  For Each objProject In DTE.Solution.Projects 
      objConfiguration = objProject.ConfigurationManager.ActiveConfiguration() 
      objProperty = objConfiguration.Properties.Item("ManagedExtensions") 
      System.Windows.Forms.MessageBox.Show(objProject.Name & " (" & objConfiguration.ConfigurationName & ") ManagedExtensions: " & objProperty.Value.ToString) 
  Next 

End Sub 

Unfortunately, this method is no longer working for me.

For unmanaged projects, I get an exception trying to fetch the ActiveConfiguration.

For managed projects, I can get the ActiveConfiguration, but the ManagedExtensions property is not available. In fact, I think that the properties collection is empty.

Is there a new way to recognize an unmanaged C++ project?


Solution

  • You can get the ManagedExtensions property via the VCConfiguration object, with code something like

    Private Enum compileAsManagedOptions
      managedNotSet            = 0
      managedAssembly          = 1
      managedAssemblyPure      = 2
      managedAssemblySafe      = 3
      managedAssemblyOldSyntax = 4
    End Enum
    
    
    Dim VCProj            As Object                   'VCProject
    Dim VCConfig          As Object                   'VCConfiguration
    Dim VCManagedOption   As compileAsManagedOptions = compileAsManagedOptions.managedAssemblyPure
    
    VCProj = prj.Object
    If VCProj IsNot Nothing Then
      VCConfig = VCProj.Configurations.Item(1)
      If VCConfig IsNot Nothing Then
        VCManagedOption = VCConfig.ManagedExtensions
      End If
    End If
    

    where prj is the Envdte.Project object.

    This code is only executed if I already know that it is a C++ project, based on the project kind.

    I defined the variables as object, so that I don't have to add a reference to
    Microsoft.VisualStudio.VCProject.dll
    to my package, because this DLL will only be present if support for C++ projects has been installed.