Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro expansion in Visual Studio macro or add in

Tags:

I have a VS project with an IntermediateDirectory like this: "....\temp\$(SolutionName)\$(ProjectName)".

I can read this value using a macro or add in, however, I would need the actual directory to manipulate files there. Right now, I manually replace the "$(SolutionName)" and "$(ProjectName)" with the respective values, which works fine but might become complicated when different macros or even user macros from property sheets are used.

So my question is: Does the Visual Studio API have a built in function to expand macros like these? Or is there some other elegant solution?

like image 381
Dietmar Hauser Avatar asked Aug 21 '09 09:08

Dietmar Hauser


People also ask

What is expansion of macro?

A macro consists of name, set of formal parameters and body of code. The use of macro name with set of actual parameters is replaced by some code generated by its body. This is called macro expansion.

What is macro in Visual Studio?

Visual Studio Macros are one way to increase productivity when a particular action or task needs to be repeated multiple times. Addins are another. Almost anything that can be done in the Visual Studio Integrated Development Environment (IDE) manually can be done through macros, except for thinking!

What is macro expansion in C++?

The process of replacing a macro call with the processed copy of the body is called expansion of the macro call. In practical terms, there are two types of macros. Object-like macros take no arguments. Function-like macros can be defined to accept arguments, so that they look and act like function calls.

Does Visual Studio have macros?

Depending on your installation options, Visual Studio can make hundreds of macros available to you in an MSBuild-based . vcxproj Visual Studio project.


1 Answers

There is an elegant solution! But I only know the one that applies to C++ projects.

Assuming you're in a C# add-in:


// Get the main project from the first startup project

VCProject vcMainProject = (VCProject)(_applicationObject.Solution.SolutionBuild.StartupProjects as IVCCollection).Item(1);

Project mainProj = (Project)_vcMainProject .Object;

// Get the configuration we'll be using

IVCCollection cfgs = (IVCCollection)_vcMainProject .Configurations;

VCConfiguration vcCfg = (VCConfiguration) cfgs.Item(mainProj.ConfigurationManager.ActiveConfiguration.ConfigurationName + "|" + mainProj.ConfigurationManager.ActiveConfiguration.PlatformName);

string finalString = vcCfg.Evaluate("....\temp\$(SolutionName)\$(ProjectName)");


You can also check out this page: http://msdn.microsoft.com/en-us/library/czt44k0x%28VS.71%29.aspx

If you're not using this for C++, there should be a similar interface for the Project, Configuration, and Solution classes provided for other languages (C# and VB).

like image 98
Lake Avatar answered Oct 12 '22 22:10

Lake