three-step visual studio build - visual-studio-2010

My VS 2010 solution has let's say 1 native C++ dll project and 1 c# wrapper project. What I want is a pure magic. My solution has 32 and 64 bit configurations, mostly because of native dll. I'd like to build the solution so that c# dll includes both 32bit and 64bit native dll as resources in c# dll. Then I will unpack proper bitness in runtime and use it (this is not a problem). My question is how do I configure VS solution to build both bitnesses and then put them into the C# project before it builds?
I could probably do a clever .msbuild script, but then I won't be able to build nicely from VS.

You can do the following:
Right-click on the C# project in Solution Explorer and select Unload Project.
The project will be unloaded. Right-click on the project again and select Edit *.csproj.
Now you can edit the project file inside Visual Studio. *.csproj file has rather simple XML format.
Go down the *.csproj file right before </Project> closing tag.
Add the following lines:
<Target Name="BeforeBuild">
<MSBuild Projects=".\..\NativeLibrary.sln" Properties="Configuration=Release;Platform=x64"/>
<MSBuild Projects=".\..\NativeLibrary.sln" Properties="Configuration=Release;Platform=Win32"/>
</Target>
No, you can right-click on the project file in Solution Explorer and select Reload Project.
Also, you will need to add 32-bit and 64-bit *.dll files to resources. I have used this approach for a long time and it works perfectly. You can build your C# project from inside Visual Studio.

Related

Is it possible for a C++ (vcxproj) project to reference a net5.0 C# project

I have a C# class library project which I consume from a C++ project. This works perfectly well when:
the C# project has TargetFramework set to netstandard2.0
the C++ project has TargetFrameworkVersion set to v4.7.2
However, I now need to upgrade the C# library to net5.0...
the C# project has TargetFramework set to net5.0 (using new style project file)
the C++ project has TargetFrameworkVersion set to v5.0
and receive this error....
C:\Program Files\Microsoft Visual
Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(1806,5):
error : Project '..\cslib\cslib.csproj' targets 'net5.0'.
It cannot
be referenced by a project that targets '.NETFramework,Version=v5.0'.
Note that it's not possible to set the TargetFrameworkVersion for the C++ project to "net5.0" or "net5.0-windows" since that results in a project load error:
It's not clear that "v5.0" is the correct TFM for a C++/vcxproj format file - it's possible msbuild is just falling back to v4.7.2 by default but I can't think of a better alternative.
Is there a way to accomplish this? It seems like it should be possible to target net5.0 in a C++ project by now. (I'm using Visual Studio 2022 Preview and PlatformToolset is set to v143 which is the latest).
It's a bit tricky, but the following should help:
If using the GUI, the following settings need to be made in properties: Under "Root->Extended" setting "Common language runtime support": "Net Core runtime support /clr:core" and ".NET Core Target framework": ".NET 5.0" (you might need to click "apply" after choosing the first, to get the possible settings in the second box to update)
In the .vxcproj, this results in:
...
<PropertyGroup Label="Globals">
<TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
<Keyword>ManagedCProj</Keyword>
<TargetFramework>net5.0</TargetFramework>
...
</PropertyGroup>
and (once for each build configuration):
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<CLRSupport>NetCore</CLRSupport>
...
</PropertyGroup>
Note that it is not possible to create C++ exes in .NET Core. C++/CLI projects must be libraries and cannot contain the main entry point (create a stub-loader C# project if you have to).
You may then experience:
error NETSDK1145: The Apphost pack is not installed
This documentation suggests this can be overcome by modifying the project file but if that does not work, a global.json file can be added in the solution root folder to point to the desired SDK.

Setting executable icon in Visual C++ 2010 Express

I'm rather unfamiliar with programming tools in Windows but have been forced to use VC++ 2010 Express for a project recently. We have been working on a piece of software that we didn't start and now we are reaching the finish line. Only problem is that I want to be able to set the executable icon.
Since the project is written mostly in SDL we have set the titlebar icon using the SDL_WM_SetIcon call as recommende on several sites but I still can't find any references to how to set the executable icon. All my google searches has turned up results about Visual Studio 2010 and Visual C++ 2008, neither of which seems appicable.
Since VC++ 2010 Express lacks resource editor but not resource compiler this can be done by manually creating a resource.rc file in the project directory (same as where your .vcxproj files are located), also drop in your .ico version of your icon in the same directory.
In resource.rc add the following line:
IDI_APP ICON "icon.ico"
And add the file, in VC++, to the Resource Files.
Doing this in VC++ should result in something like this to be added to your .vcxproj:
<ItemGroup>
<ResourceCompile Include="resource.rc" />
</ItemGroup>
And to your vcxproj.filters:
<ItemGroup>
<ResourceCompile Include="resource.rc">
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup>
I ran into a similar situation recently with the Express edition.
I came up with a different way to approach this (see gif).
Posting it here in case someone runs into similar problem.

How to build MFC library from source with Visual Studio 2010

Visual Studio has historically always included the MFC library as source so you can build it yourself with the supplied makefile. But in Visual Studio 2010 there is no makefile for MFC. So, how can you rebuild it?
There is documentation on MS implying the makefile should be there:
http://msdn.microsoft.com/en-us/library/bs046sh0.aspx
.. so perhaps its an oversight, or perhaps they migrated it to msbuild but forgot to include the mfc msbuild project file.
If anyone has succesfully built a custom MFC based on that in VS2010 how did you build it?
Can the makefile from VS2008 be used with minimal tweaks? Or does anyone have an msbuild script for it?
I'm only interested in a statically linked library to be used with a specific app.
since in the end MFC is a library as any other, you can just create a new project in visual studio and add all MFC source files to it. Set the options to create a static library, set compilation/linker options as desired (eventually based on the 2008 makefile) and you're ready to go.

msbuild: "compile" context menu item for Custom Build Action in Visual Studio 2010 (on C++ project)

I've added a new build target to my C++ Visual Studio project (vcxproj).
This target runs a custom tool when the project is built. The tool processes specific files on the solution according to the ContentType and ItemType I specified.
This works well with project actions such as "Build" and "Clean".
Now I would like to support an action equivalent to "compile", i.e. right click on a file in the Solution Explorer and select to process this specific file with my custom tool (the same way "compile" runs "CL" for "C/C++ Code" file types).
I know I could add a Visual Studio macro to do this. This is not a good solution for me because it's harder to deploy for many users.
A better solution is to customize the vcxproj (or files imported by it).
I wonder if it's possible to add a "compile" like action in the menu (or change the "compile" behavior for file types other than "C/C++ Code") through msbuild targets scripts or PropertyPageSchema.
UPDATE: I've started a discussion on MSDN forum. Got some answers from a Microsoft moderator that helped clearing up some things, but the problem is still unsolved.
UPDATE (2016), for VS2015
AvailableItemName seems to solve this on VS2015. For example, I have a custom target to process Excel files.
On the targets file:
<ItemGroup>
<PropertyPageSchema Include="$(SolutionDir)\ExcelOptions.xml" />
</ItemGroup>
<ItemGroup>
<AvailableItemName Include="Excel">
<Targets>ProcessExcel</Targets>
</AvailableItemName>
</ItemGroup>
On the options file:
<FileExtension Name=".xls" ContentType="Excel"/>
<ContentType Name="Excel" DisplayName="Excel File" ItemType="Excel"/>
<ItemType Name="Excel" DisplayName="Excel File"/>
Now Compile is accessible on the Solution Explorer context menu after selecting an excel file, and CTRL-F7 works as well (for files that can be edited on VS, not for excel files)

VS2010 project dependencies

I have a c++ project in VS2010 and a c# project that is to consume this c++ project output (it uses it for p/invoke). I was thinking that I could ensure that the c++ project was build before the c# project by editing the "Project dependencies..." in the solution but this does not seem to have any effect, the build on my buildserver does not respect this setting (I'm using TeamCity to bootstrap an MSBuild file that builds the entire solution file)
I think this used to work, has anything changed with VS2010? Or should I declare the dependency in another way?
SOLUTION: The trick was to hand-edit the csproj file outside VS2010 and add a section like this:
<ProjectReference Include="..\CobraLib\CobraLib.vcxproj">
<Project>{598506DA-91DA-4F25-948D-A14CB16ABEBA}</Project>
<Name>CobraLib</Name>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
That made the build server process my projects in the correct order. Only caveat is that VS2010 displays an error on the project reference ("not a .NET project") but otherwise things are working as I intended

Resources