Is there a way in MSBuild logic to determine if I am running managed vs unmanaged code? Not C++ vs C#, but just managed vs unmanaged? I'd like to set some properties (usually just version information) differently depending on whether the code is managed or unmanaged.
-
*running managed vs unmanaged code* do you mean *building* (as in *compiling/linking*) or effectively *running* an application? In the first case: what project type are you using? It's not as simple as *Not C++ vs C#*: in a C# project it's managed, in a C++ it can be both – stijn Sep 18 '14 at 07:20
-
"in a C++ it can be both" Do you mean both or either? I am using this same logic across many projects, some are c# (always managed), and some are C++ (sometimes compiling managed sometimes not). So I mean compiling managed (using the clr) vs compiling unmanaged. – Eric Kulcyk Sep 18 '14 at 15:47
-
Sorry meant 'either' indeed. But now I understand what you're after. – stijn Sep 18 '14 at 18:13
2 Answers
There are normally two things that change in a vcxproj file for managed complation (afaik, at least that's how we have it in our master c++/cli property sheet used for all cli projects: the CLRSupport
property is set to true and the ClCompile
ItemGroup has the CompileAsManaged
metadata set to true. You can check on any of these or both. Here's a target which prints the values:
<Target Name="CheckManaged">
<ItemGroup>
<ClCompile Include="dummy.cpp" />
</ItemGroup>
<PropertyGroup>
<CompileAsManaged>@(ClCompile->AnyHaveMetadataValue('CompileAsManaged','true'))</CompileAsManaged>
</PropertyGroup>
<Message Text="CompileAsManaged is $(CompileAsManaged) and CLRSupport is $(CLRSupport)" />
<ItemGroup>
<ClCompile Remove="dummy.cpp" />
</ItemGroup>
</Target>
As you can see getting the CompileAsManaged
metadata value requires some treatment: I'm adding an item to the ClCompile group because if the group is empty you canot use CompileAsManaged; normally you can just omit this.

- 34,664
- 13
- 111
- 163
In C++, each item in ClCompile (list of source files) has a CompileAsManaged
metadata value. Setting properties is difficult since it can vary for each source file, but is more straightforward if you only expect (and support) keying off the whole-project setting. Toggle that in the IDE and see what changes in the vcxproj file. It has a few different values to choose from.

- 5,592
- 3
- 24
- 45