Search code examples
visual-studiovisual-c++visual-studio-extensionsvsix

How to get Custom Build Tool property from VCConfiguration


Currently working on making an extension for Visual Studio C++ when dealing with files that are built using a custom build tool. For the life of me I can't find a way to drill down into the item that's currently selected's property panel.

Scenario:

  • I have a test.myfile file in solution
  • It is set to be built with a custom build tool

You can imagine it looks like this enter image description here

I currently have a command button in the item menu that executes as expected. I can get the selected items and from that grab the project items selected. I have the VCProjectItem and the VCConfiguration of the project tied to the file for the active configuration.

How can I get the "Command Line" property from the property page?


Solution

  • How can I get the "Command Line" property from the property page?

    After l do a deep research, I found that we cannot use the property VCCustomBuildTool.CommandLine to get the custom file's property Command. It can be used to get the project's property(right-click on the project) not the specific file's property(right-click on the file).

    Or another way to think about it, since the file is created with the node like CustomBuild, so we can obtain them in xxxx.vcxproj file.

    enter image description here

    We can use DTE interface to get the current proj file of the current project and then read the child node command in the parent node custom build.

    For an example:

            using EnvDTE;
            using System.Xml;
    
            .........
    
            VCProject prj; 
            XmlDocument doc = new XmlDocument();
            DTE dTE = Package.GetGlobalService(typeof(DTE)) as DTE;
            prj = dTE.Solution.Projects.Item(1).Object;
            doc.Load(prj.ProjectFile);  //read the proj file for the current project
            XmlElement root = doc.DocumentElement;
            XmlNodeList nodeList = root.GetElementsByTagName("CustomBuild"); //get the customBuild Node
            string str="";
            foreach (XmlNode node in nodeList) //search the child node 'command' in the parent node named custombuild
            {
                str += node["Command"].InnerText.ToString()+"-----";
            }
    

    Please so not forget to reference the envdte.dll which exists in C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\PublicAssemblies.

    Hope it could help you.