32-bit COM Assembly Doesn't Work With x86/AnyCPU Target Platform in Visual Studio 2022

Ashish Vora 1 Reputation point
2022-03-04T05:28:15.48+00:00

I have a web app which has list of projects, all under single solution. Many of them refers the COM DLLs which is 3rd-party provided. Project X1 refers 32-bit COM DLL and its target platform set as AnyCPU, whereas the start-up project's target platform is x86. All projects targets .Net Framework v4.8.04084.

This combination is working very much fine with Visual Studio 2015 v14.0.25420.01 Update 3.

----------

Migration from VS2015 to VS2022

Opening the same solution in Visual Studio 2022 v17.1.0, it opens all projects including some errors for project X1, see below.

179955-image.png

However, the build is successful for all projects including project X1. When try to run it, it throws System.BadImageFormatException for a 32-bit DLL referred in project X1. Find attached file containing the detailed error information.

Same problem if I change the target platform of start-up project to AnyCPU.

I have verified the component installation, visual studio versions and many more things, nothings seems to be causing this.

Is this because Visual Studio is x64 ? Even if it is, there should not be such backword compatibility issues after migration.

Can we have solution for this ? This is really a blocker for people like me migrating older projects to latest VS2022 needs to attended.

----------

Edit:

Opening the same solution in Visual Studio 2019 v16.11.5 works well without any changes.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,178 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,263 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,613 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,121 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Albert Kallal 4,651 Reputation points
    2022-03-16T19:46:00.773+00:00

    As a FYI?
    The default for VS, I think up to 2017 was "any cpu" would run as x32 bits.

    Now, in 2019 (or later), "any cpu" will result in your project running as x64 bits.

    So, you have to FORCE the project to run as x86.

    183873-image.png

    If you don't have a x86 setting, then click on configuration manger in above and add x86.
    (you can use the x64 template during that prompting process).

    So, what has changed?
    Any CPU now defaults to x64 compared to previous versions of VS defaulted to x32 when using "any cpu". So, now, and these days, you have to set and "force" the issue.

    Note that if you accidently, (or on purpose referenced some of the other assembles, and re-compiled them, that I would check those other projects - set to x86, and re-compile (rebuild all).

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments