Visual Studio Registry conflict on Terminal Server

Visual Studio has been installed as a TS Aware application.

But when DEVs used the option “register for COM intercop” from Project properties, they started to face Registry conflicts.

In fact, this option calls “regasm.exe” which will register the DLL path into the “HKEY_LOCAL_MACHINE” Windows Registry and leads to conflict as they are used by all users.

They are others solutions to by pass this as the Reg-Free COM using a Manifest file but it was unusable is our case.

So I decided to force VS to register those paths into the “HKEY_CURRENT_USER” Registry using a PowerShell script running on Post-Build.

What will do the script?:

  • Run “regasm.exe” to generate the reg file.
  • Find and replace “HKEY_CLASSES_ROOT” (which is a mixed view of the “HKEY_LOCAL_MACHINE” and others) by “HKEY_CURRENT_USER\Software\Classes”.
  • Run “regedit.exe” to apply the .reg.

Note: I’m using 32bit version of regedit to make sure it registers the keys to Wow6432Node. The path is not present in the .reg file but regedit knows how to handle it properly.

That’s all.

Script to place in your Project “..\bin\Debug” folder (.ps1), just replace A,B,C,D by the name of your DLLs without extension:

$dlls=@("A","B","C","D")

ForEach ($i in $dlls) {

C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe /nologo /codebase "$i.dll" /regfile:"$env:temp\$i.reg"
 (Get-Content "$env:temp\$i.reg") | ForEach-Object { $_ -replace "HKEY_CLASSES_ROOT", "HKEY_CURRENT_USER\Software\Classes" } | Set-Content "$env:temp\$i.reg"
 C:\Windows\syswow64\regedit.exe /s "$env:temp\$i.reg"
 
}

VS Post-build event command-line:

Powershell.exe -executionpolicy remotesigned -File script_name.ps1
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s