Phil Jollans
Phil Jollans

Reputation: 3769

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

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:

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.


Attempted workaround:

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

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?

Upvotes: 8

Views: 7997

Answers (2)

David Johnston
David Johnston

Reputation: 1530

A more elegant solution is to use the MSBuild RegisterAssemblyMSBuildArchitecture property with a value of x86 to force the COM assembly registration task (or regasm) to register the project's assembly in the 32-bit registry. If you want to force 64-bit COM assembly registration use a value of x64 for RegisterAssemblyMSBuildArchitecture. I added the RegisterAssemblyMSBuildArchitecture property with a value of x86 to my project file and it worked. The assembly was now registered in the 32-bit registry keys, and not in the 64-bit registry keys.

Apparently this happens if the Platform MSBuild property is AnyCPU. In which case the COM assembly registration defaults to the same bitness of the MSBuild runtime. For Visual Studio 2022 that means a 64-bit MSBuild runtime. If Platform were x86 then the COM assembly registration would also be 32-bit without setting the RegisterAssemblyMSBuildArchitecture property. I can't confirm that because I haven't tried this approach.

This is pointed out by the resolution to the issue in the Microsoft Developer Community bug report linked in the original question.

As far as I can see RegisterAssemblyMSBuildArchitecture is not documented anywhere else.

Upvotes: 2

Phil Jollans
Phil Jollans

Reputation: 3769

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>"%25windir%25\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.

Upvotes: 6

Related Questions