locked
Casting problems when trying to use VCCodeModel RRS feed

  • Question

  • I have been trying to create an Add-In that reads the contents of certain C++ files, in a project. I have had success with previous add-ins in the past and I started to enjoy EnvDTE - but using VCProject - and VCCodeModel - proves a lot more challenging, nothing I try works.

    I have typed the code from Microsoft sites identically... example: http://msdn.microsoft.com/en-US/library/ms228770(v=vs.100).aspx

    public void test( DTE2 application) 
    {
      try            
      {                
        VCCodeModel vcCM = null;                
        VCCodeElement vcCodeElement = null;                
        vcCM = ((VCCodeModel (application.Solution.Item(1).CodeModel));                
        vcCodeElement = ((VCCodeElement) (vcCM.CodeElements.Item(1)));                
        // AddCommentAtStart(vcCodeElement);                
        // AddCommentAtEnd(vcCodeElement);            
      }            
      catch (System.Exception ex)            
      {                
        MessageBox.Show(ex.Message);            
      }
    }

    I have tried a lot of other examples, nothing I try works - it all fails at the very beginning, at the simplest cast.

    So far I have been able to iterate through the ProjectItems and get the .cpp and .h files included, and... what I really would like to do is get a list of #includes from these files. Like http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vccodemodel.vccodeinclude.aspx... Except it doesn't work...

    I have started something like 

    public void GetIncludes(ProjectItem projectItem)        
    {
      try
      {
        FileCodeModel fileCodeModel = projectItem.FileCodeModel;
        CodeElement codeElem = fileCodeModel.CodeElements.Item(1);
        // nothing else would work
      }
      catch (System.Exception ex)
      {
        MessageBox.Show(ex.Message + "\n" + ex.StackTrace.ToString());
      }
    }

    Please, can somebody help me get something to work ?

    Thank you.


    • Moved by Lisa Zhu Monday, December 17, 2012 6:18 AM vs extensibility (From:Visual C# Language)
    Friday, December 14, 2012 3:44 AM

Answers

  • My workaround was to not use the VCCodeModel, but parse the project files myself (using Xml.Path, or simply string search methods, as needed).

    When I have time, I will come back to VCCodeModel, as it will provide for a more elegant solution.

    It is still what I will want to use... 

    There is a limitation I have - I found out that the CodeModel requires the Intellisense database, which is disabled on some computers that I might be running the project.

    When I have a better answer, I will come back and post it, and mark it as "the answer".



    • Marked as answer by emptyheaded Monday, January 7, 2013 8:45 PM
    • Edited by emptyheaded Friday, January 11, 2013 3:21 PM
    Monday, January 7, 2013 8:41 PM

All replies

  • Hi emptyheaded,

    From your description, I ‘d like to move this post to  Visual Studio Extensibility forum for better support .

    There are more  experts in this aspect, so you have more luck getting answers.

    Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 17, 2012 6:17 AM
  • Hi,

    As I know, VCProject and VCCodeModel are just used in VC++ project.

    Where did you run your add-in, in a C++ project?

    If not, the exception will be reported.

    You can set a breakpoint in your codes and do more tests.

    Best regards,


    Ego [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 17, 2012 6:27 AM
  • I created an Add-in in C#, and tried to run it on a c++ project. I am aware of the fact that the VC prefix from VCProject and VCCodeModel stand for C++.

    I would try breakpoints, but the error occurs before any object is even created.

    Actually, breakpoints in the current project are sort of impossible... I have created the Add-in as a separate class, to integrate with others I have created... Each overrides "Perform" - which may be disabling the breakpoints, I have been unable to use them in previous Add-ins I have created.

    I will experiment with this some more, because it is an interesting topic that I want to learn, but I have solved the problem I was trying to solve, through a different approach...

    • Marked as answer by Ego Jiang Monday, December 24, 2012 9:16 AM
    • Unmarked as answer by emptyheaded Friday, January 11, 2013 3:23 PM
    Monday, December 17, 2012 4:50 PM
  • Hi,

    What did you find caused the error?

    You said you have solved the problem. Please share your workaround here which may be very beneficial for other community members who have similar questions.

    Thank you for your understanding and support.

    Best regards,


    Ego [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 19, 2012 9:32 AM
  • My workaround was to not use the VCCodeModel, but parse the project files myself (using Xml.Path, or simply string search methods, as needed).

    When I have time, I will come back to VCCodeModel, as it will provide for a more elegant solution.

    It is still what I will want to use... 

    There is a limitation I have - I found out that the CodeModel requires the Intellisense database, which is disabled on some computers that I might be running the project.

    When I have a better answer, I will come back and post it, and mark it as "the answer".



    • Marked as answer by emptyheaded Monday, January 7, 2013 8:45 PM
    • Edited by emptyheaded Friday, January 11, 2013 3:21 PM
    Monday, January 7, 2013 8:41 PM