Setup targeting both x86 and x64? - c#

I have a program that requires both x64 and x86 dlls (it figures out which ones it needs at run time), but when trying to create a setup, it complains:
File AlphaVSS.WinXP.x64.dll' targeting 'AMD64' is not compatible with th project's target platform 'x86'
File AlphaVSS.Win2003.x64.dll' targeting 'AMD64' is not compatible with th project's target platform 'x86'
File AlphaVSS.Win2008.x64.dll' targeting 'AMD64' is not compatible with th project's target platform 'x86'
How can I make my setup target both platforms like my program does?

The MSI created by the setup project (in Visual Studio) can only target one platform at a time. Your option is to either make 2 MSI's, merge them together and make a custom setup boot strapper that choose between the two.
There are some 3rd party products,like Advanced Installer for example, that can do this for you.

I ran into this too and wrote a blog post about my solution:
deflate the file using deflate.exe, naming it with a different extension (e.g. .x64)
add it to your main project as a content file
add a custom action project to your solution
add the custom action to the setup projects "Install" custom actions
inflate the file inside the custom actions Install method using
System.IO.Compression.DeflateStream (see code above)
do a little dance around your desk, down the hall, and past as many coworkers as you care to annoy :)
The deflate.exe file can be downloaded from its repository on google code.

.Net has an "Any CPU" option. It's tempting to think of it as more of a "generic" option that's going to only use the lesser x86 features, but really it lets the JIT compiler on each machine pick the appropriate cpu type for that machine.
The only time you shouldn't use it is if you know you have dependencies or requirements that aren't good for one architecture or the other. For example: you know you need a lot of ram, you have a dependancy on a 32-bit native dll, or you want to pre-compile the app.
There's a danger here because you have a platform-specific dll dependancy. But you have dlls for both types and it sounds like you know how to pick the right one at runtime. So will the 'Any CPU' option work for you?

Open a deployment project.
In the Solution Explorer, select the deployment project.
In the Properties window, select the TargetPlatform property.
Choose either Itanium for an Intel Itanium 64-bit platform, or x64 for any other 64-bit platform (such as AMD64 and EM64T instruction sets).
At installation time, an error will be raised and installation will be halted if the target computer is not compatible with the specified platform.

Related

How to build a x64 and x86 executable at the same time

In dotnet core/.net 5 is there the possibility to have a project that builds executable for x86 and x64?
An obvious option would be to work with two separate project files as suggested here: Build both x86 and x64 at.
Is there an alternative? Neither <PlatformTargets>x64;x86</PlatformTargets> nor <RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers> in .csproj file yield the desired result. I even tried with a publish-profile using<RuntimeIdentifiers>win-x64;win-x86</RuntimeIdentifiers> which seems not to have the desired effect.
I would like to have both files as native items in the nuget package so I think it would be nice if I could do everything with just one project.
Update: I cannot use AnyCPU because it might be shipped as 32-bit bundle together with 32-bit native libraries. The AnyCPU might run as 64-bit then (there is no constraint the JIT could detect) and be incompatible with the native files and fail as a consequence.
Have you tried to build with 2 build scripts, then you can specify the "PlatformTargets" separately.

Error when setting up desktop application for MSIX packing in Visual Studio

I followed this tutorial from official Microsoft team to use the Windows Application Packaging Project project in Visual Studio to generate a package for my WPF desktop app. A Note here reads: If you're packaging a desktop application, right click on the the Windows Application Packaging Project node.. So I did just that (as shown below). But at the end of the wizard, I got the error shown at the end below:
Remark: My WPF project, MyWPFProject is a .NET 5 project, and the Application Packaging Project project name is WapProjTemplate1. I'm using latest version 16.9.3 of VS2019
Question: Why the error, and how can we resolve it. As, the following figure shows, my both projects target the same platform.
Error: There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "C:\MyFolder\MyWPFProject\bin\x86\Debug\net5.0-windows\win-x86\MyWPFProject.dll", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project. WapProjTemplate1 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets
Please try to modify your Configuration Manager as below:
After completing the above steps, please right click your WapProjTemplate1, choose Publish on the popup and select Create App Packages... Then select Release for Architecture on Select and configure packages wizard as below:
Update:
On a 86-bit Windows operating system: Executables and DLL that are compiled with the Any CPU execute on the 32-bit CLR.
On a 64-bit Windows operating system: A DLL compiled with the Any CPU executes on the same CLR as the process into which it's loaded. Executables that are compiled with the Any CPU execute on the 64-bit CLR.
The Target Platform of the program and the referenced DLL should be consistent at runtime. Generally speaking, we can set the Target Platform of the program to be consistent with the deployed operating system according to actual needs and the DLL is preferably Any CPU. The special deployment environment requires special consideration.
Maybe because your project is compatible with "Any CPU" but you have a dependency on a project or DLL that is either x86 or x64. Because you have an x86 dependency, technically your project is therefore not compatible with "Any CPU".

How to use wixlibs as file references with support for different platforms?

I just am trying to replicate my distributed C# project structure to WIX setup projects. Now there is the following problem:
LIB: a C# library solution that builds AnyCPU .NET dlls from several C# projects
APP: the dlls from LIB are referenced (as file references) by this main application solution. Additionally there are platform dependent libs included in this application solution, therefore it is important to being able to create setups for the two specific target platforms x86 and x64.
Now I started to create a wixsetup project within the APP solution (which works fine). Then I proceeded with creating a wixlib within the LIB solution that references the LIB .NET dlls into the wixlib.
Now the problem:
The wixlib references the AnyCPU .NET dlls within a DirectoryRef which seems to be platform specificly tagged when creating the wixlib. Therefore I have to go back to the LIB solution, build the project with one platform target, copy the built files (via SVN externals mechanisms) to the APP solution, build this project with the exact same target platform as the wixlib was created with and repeat this procedure for creating the other platform.
It may seem that this is kind of complicated, but doable. Due to the fact that I omitted several other library solutions for which the same problem applies and the fact that all those libraries are used in multiple application solutions and - finally - everything has to run on our build server automatically as well, it is clear that this will not work.
I know of the following solution, though:
Double the .NET dll references within wixlib to assign them to different DirectoryRef INSTALLDIR and INSTALLDIR32 e. g. and to implement those different directory references in the wixsetup.
But this would complicate things as well and is not my preferred solution therefore - if there is an alternative.
If there is no smart alternative, just tell me and I will do things as described in the last paragraph.

How to build x64 and x86 projects that reference same projects

I have three projects, ProjectA (exe), ProjectB (exe) and ProjectD (class library)
Project A references the System.Data.OracleClient.dll and ProjectD. Project B just references ProjectD. The 32-bit client version of oracle is installed and therefore ProjectA has to be a 32-bit application. Project B can be built as a 64-bit application.
Project A build settings:
Platform: Active (x86)
Platform target: x86
Project B build settings:
Platform: Active (Any CPU)
Platform target: Any CPU
My questions are what should the build settings be for ProjectD (the class library) and when ProjectA and ProjectB get built does it build ProjectD differently? A deeper explanation of the CLR would be great in terms of communications of the projects.
ProjectA and ProjectB are to be used on 64-bit Windows Server 2008. No installation, just standalone exe's.
Only the Platform target setting for the EXE project matters. That's the assembly that gets loaded first and determines the bitness of the entire process.
A DLL doesn't get a choice, it must be compatible with whatever was selected by the EXE project. Picking AnyCPU for a DLL project is therefore almost always the correct selection.
There are just a few cases where you'd use an explicit setting. You'd only do so if you know that the class library has a dependency on some kind of native code, like the Oracle provider, and that trying to run that native code in the wrong bitness produces a completely inscrutable exception. You can avoid that exception and get a (slightly) better one by picking the Platform target for the DLL, the program will fail with a BadImageFormatException early when it tries to load the assembly. Albeit that this exception isn't exactly a very informative one either. Some odds that an admin is going to try to reinstall the DLL a couple of times before deciding that the real problem is elsewhere.
So basic ground rules: pick x86 for the EXE project, AnyCPU for all other class library projects, a nastygram to Oracle for doing nothing to make this easy.
My questions are what should the build settings be for ProjectD (the
class library) and when ProjectA and ProjectB get built does it build
ProjectD differently? A deeper explanation of the CLR would be great
in terms of communications of the projects.
Just use Any CPU for your lib. It'll build a unique assembly that can be executed in both 32-bit en 64-bit environments.
Technically, the just in time compilation with either produce 32-bit code or 64-bit code at runtime.

Platform configuration for projects in VS 2010

I have a third-party project type in Visual Studio which for some reason only supports the .NET Platform configuration for the build, for all other (standard C#) projects in the solution I only have AnyCPU. Unfortunately, ever since upgrading to VS 2010 it produces following error when built :
Error 39 The OutputPath property is
not set for project
'ReferencedBusinessProject.csproj'.
Please check to make sure that you
have specified a valid combination of
Configuration and Platform for this
project. Configuration='Debug'
Platform='.NET'. This error may also
appear if some other project is trying
to follow a project-to-project
reference to this project, this
project has been unloaded or is not
included in the solution, and the
referencing project does not build
using the same or an equivalent
Configuration or
Platform. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets 483 10 CustomTypeProject
It's pretty much descriptive in what is missing but I haven't found any way to fix it so far. Do you have any idea how this can be resolved or what can be the problem ?
The .Net Platform must have been created for the project before you received it for transparency reasons check the project settings and if it's building any cpu then fix the project configuration. (Standards are AnyCPU, x86, x64, win32) etc...
I would suggest you right click on the ".sln" file and in configuration manager set the properties of what you would like build when you call a platform. I.e.
This sample is best served with a configuration called "Mixed Platforms"
csproj1 platform=AnyCPU configuration=debug build checkbox (checked)
csproj2 platform=.net configuration=debug build checkbox (checked)
This will allow you to build with msbuild
The call would be
msbuild my.sln /p:configuration="Debug" /p:platform="Mixed Platforms"
Both projects will build.

Categories