How to reference/consumme assembly manifest based on CPU in WPF C# Application project?

Nicolas Montmarquette 0 Reputation points
2024-04-16T12:44:11.6233333+00:00

I want to reference an older Win32 assembly from a Wpf C# application.

I do have both x86 or x64 versions of the Win32 assembly but I don't see how to reference based on selection target CPU.I can only specify either one.

The Visual Studio C# project only seems to allow one reference and I don't see how to reference different version of the assembly based on the selected CPU unless I use an ugly scheme of copying file over each other in a post-build step.

Is it posisble to create a "fat" assembly containing both CPU architecture? I don't recall ever seing that and can't find documentation of example of that. If that exists, what is the syntax and file struture?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,674 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,262 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 48,396 Reputation points
    2024-04-16T14:40:07.84+00:00

    Windows does not allow an x86 process to load an x64 DLL and vice versa. This is an inherent limit of Windows.

    I'm assuming you're talking about NET Framework here. If you're using NET 6+ then things are little different.

    You're using the term "Win32 assembly" here but is this actually a .NET assembly that was not created using the AnyCPU platform? Outside executables that should be rare unless the assembly loads a native binary. If you are working with a .NET assembly that was compiled for x86/x64 then you'll have to dynamically load the assembly at runtime. Unless you go all in on reflection then you'll need to have a set of wrapper types (likely interfaces) that the dynamically loaded types are callable through. For example supposed the assembly has an Engine class, it would need to implement an interface that resides in a third assembly that you can load that is not platform-specific. This is the common assembly. Your app would load this assembly normally and use only the types defined in it. At runtime you would need to use AppDomain.Load or LoadFile to load the assembly from the appropriate subdirectory that you have the platform-specific binary stored in. Once the assembly is loaded you'll need to create instances of the types using this assembly. A set of wrapper types would likely make the most sense here.

    If you are instead referring to a standard Win32 DLL created in something like C or C++ then you're limited to calling functions but it is probably going to be easier. You'll use DllImport to mark the functions that you need to load. Since this is a deferred call, when your application starts identify the platform you need and copy the appropriate binary into the search path so your app will find it when it is loaded. Note that if you are using NET 6+ then you can use a custom DllImportResolver to load the native DLL outside the normal search path.


  2. Bruce (SqlWork.com) 56,286 Reputation points
    2024-04-16T22:56:38.15+00:00

    you can use different dll for each binary. you just use conditionals in the project file for the reference. the side does not support this, you need to hand edit the project file.

    0 comments No comments