Home > Blockchain >  How to register for 32-Bit COM interop using Visual Studio 2022
How to register for 32-Bit COM interop using Visual Studio 2022

Time:12-11

I am having difficulty registering a C# component for COM interop using Visual Studio 2022. I thought I understood the problem, but even so, I have failed to get it working.

This is the basic problem:

  • Visual Studio 2022 is a 64-Bit application
  • When you build with Visual Studio 2022 it invokes the 64-Bit version of MSBuild
  • The 64-Bit version of MSBuild invokes the 64-Bit version of RegAsm, which registers the component as a 64-Bit COM component

Since most COM-Based applications in the real world are 32-Bit applications, this is most likely not what you want.

Although this is clearly by design, I find it to be an unfortunate design choice and I have opened an issue in the Developer Community. If you agree with me, it might help gave the issue an up-vote.

Following the instructions on this page I thought it was going to be easy to update our projects by:

  • unchecking the option "Register for COM interop"
  • replacing it with a post-build-event to run the 32 bit version of RegAsm

This is the command that I have defined as a post-build-event:

"%Windir%Microsoft.NET\Framework\v4.0.30319\regasm.exe” “$(TargetPath)” /codebase

This leads to the error MSB3073, and the text "The command ... exited with code 123".

In full the output line is:

1>C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(5710,5): error MSB3073: The command ""%Windir%Microsoft.NET\Framework\v4.0.30319\regasm.exe” “C:\CS3\Output\Applications\interop\McDaMBusTimer.dll” /codebase" exited with code 123.

I have tried several variations of the command, but I haven't found a definition which works.

Does anybody know the exact command that I have to enter as a post-build-step?

CodePudding user response:

The error was quite stupid and entirely my own fault.

The correct command is

"%windir%\Microsoft.NET\Framework\v4.0.30319\regasm.exe" "$(TargetPath)" /codebase

If you need to generate a type library file, you should use the command

"%windir%\Microsoft.NET\Framework\v4.0.30319\regasm.exe" "$(TargetPath)" /codebase /tlb

If you want to edit it directly into the project file, you can use

<PropertyGroup>
  <PostBuildEvent>"%windir%\Microsoft.NET\Framework\v4.0.30319\regasm.exe" "$(TargetPath)" /codebase /tlb</PostBuildEvent>  
</PropertyGroup>

As Simon Mourier pointed out, I was originally using the wrong inverted commas. It turns out that I was using the Right Double Quotation Mark, Unicode 0x201D instead of the normal Quotation Mark, Unicode 0x0022.

In my opinion, you cannot see the difference in the project properties in Visual Studio.

The reason I had this error, was that I originally copied the command

“%Windir%Microsoft.NETFramework[64]v4.0.xxxxxregasm” “$(TargetPath)” 

from this Microsoft page. If you look closely you can see the difference.

  • Related