vbolshakov
vbolshakov

Reputation: 31

Prevent installation of VSTO Addin on build

How to prevent background installation of VSTO Outlook Addin on build in Visual Studio?

Addin installed when build/rebuild project or solution. Installed addin is not even shown in Programs and Features.

Upvotes: 3

Views: 707

Answers (2)

it3xl
it3xl

Reputation: 2672

I'm targeting a specific purpose. My client build machine projects fail when they build the same VSTO-projects from different Git-branches simultaneously.
VSTO-project tries to register its MS Office add-in to the Windows Registry during building.
If some VSTO-projects are building simultaneously then a Windows async exception can be thrown. See the exception below.

Of course you won't see this error in Visual Studio. Commonly, you're building one solution at a time. You have to build the same VSTO-project from different folders in parallel and do it many times to receive this error.

With a due respect to the question authors and future comers, let me post some "preliminary to build-machines" solutions.

I can't test them for an VSTO Outlook Add-in project. But they are tested for MS Word and Excel add-ins.

Actually, the Outlook project is slightly special. If something goes wrong with it, then you can learn MSBuild and dig farther out of
C:\Program Files (x86)\Microsoft Visual Studio\20<XX>\Professional\MSBuild\Microsoft\VisualStudio\v<XX>.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets

Hack MSBuild Task.

We will be using MSBuild Inline Tasks approach.
Let's do it for MS Word and Excel Add-ins.

For the Outlook VSTO, you have to override more tasks. Probably the RegisterFormRegions, FindRibbons, etc. I just can't test it.

  • In the Solution Explorer, click "Clean" on your VSTO Add-in project to delete previous registrations.
  • then do "Unload Project" on your project.
  • Edit {You-Project-Name}.csproj from the context menu on you project in the Solution Explorer.
  • Find <Import Project="$(VSToolsPath)\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets"
  • Before (!!!) this Import row, insert the following XML of my inline MSBuild task.
<UsingTask
  TaskName="SetOffice2007AddInRegistration"
  TaskFactory="CodeTaskFactory"
  AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
  <ParameterGroup>
      <Url ParameterType="System.String" Required="false"/>
      <AddInName ParameterType="System.String" Required="false"/>
      <OfficeApplication ParameterType="System.String" Required="false"/>
      <FriendlyName ParameterType="System.String" Required="false"/>
      <Description ParameterType="System.String" Required="false"/>
      <LoadBehavior ParameterType="System.String" Required="false"/>
      <Unregister ParameterType="System.String" Required="false"/>
      <SolutionID ParameterType="System.String" Required="false"/>
      <IsDocument ParameterType="System.String" Required="false"/>
  </ParameterGroup>
  <Task>
    <Code Type="Fragment" Language="cs">
<![CDATA[
Log.LogMessage(MessageImportance.High, "$(MSBuildProjectName) project - Overriding of SetOffice2007AddInRegistration to prevent the project's MS Office Add-in isntallation (registration)!");
]]>
    </Code>
  </Task>
</UsingTask>
  • Click Reload Project on your Add-in's project.

That's it.
You can also use C# and mock or derive from the existing C# class of SetOffice2007AddInRegistration task. But this will be quite verbose for the StackOverflow.

Hack the VisualStudioForApplicationsBuild Target

Everything is changing and VSTO is rather dead than alive.
So, why don't you copy <Target Name="VisualStudioForApplicationsBuild">...</Target> from the mentioned above Microsoft.VisualStudio.Tools.Office.targets file and override it in your VSTO add-in project file? Put it below the <Import Project="$(VSToolsPath)\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets" and do what ever you want.

Hack the RegisterOfficeAddin MSBuild target

This is for MS Word and Excel add-ins only.

As described above, find <Import Project="$(VSToolsPath)\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets".
and put below (!!!)

<Target Name="RegisterOfficeAddin" Condition ="'$(VSTO_ProjectType)' == 'Application'" />

More, More

Actually, there are other MSBuild approaches. But who bothers?

Hack on my laptop only

Sorry, this is out of my orbit, really :)
But knowing MSBuild, you can do it for a Windows login name or for an exact PC name. Just for fun. Why not use the MSBuild Inline Tasks as described above?

Build Machines

Let's prevent VSTO Add-ins installation/registration caused by VSTO Add-in project building.
The following works for MS Word and Excel VSTO Add-in projects.

  • Add -p:Disable_RegisterOfficeAddin=disable to your invocation of MSBuild

CMD example

CALL "%env_manager%\MSBuild_Wrapper_VS2017.bat"^
  MySolution.sln^
  -target:Build^
  -p:Configuration=Release^
  -p:Disable_RegisterOfficeAddin=disable^
  -verbosity:%env_MsBuild_verbosity%^
  -maxCpuCount
  • Edit ".csproj" file of your VSTO Add-in project.
  • find <Import Project="$(VSToolsPath)\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets" row and put below (!!!) it the following XML.
<!-- Prevent VSTO MS Office add-ins installation/registering during building of VSTO-projects on build machines. -->
<Target Name="RegisterOfficeAddin" Condition ="'$(VSTO_ProjectType)' == 'Application'">

  <Message Text="RegisterOfficeAddin target is redefined in $(MSBuildProjectName) project" Importance="high" />
  <SetOffice2007AddInRegistration
      Url="$(AbsolutePathToTheDeploymentManifest)"
      AddInName="$(TargetName)"
      OfficeApplication="$(OfficeApplication)"
      FriendlyName="$(FriendlyName)"
      Description="$(OfficeApplicationDescription)"
      LoadBehavior="$(LoadBehavior)"
      
      Condition="'$(Disable_RegisterOfficeAddin)' != 'disable'"
      />
  <Message Text="SetOffice2007AddInRegistration task is suppressed in $(MSBuildProjectName) project" Condition="'$(Disable_RegisterOfficeAddin)' == 'disable'" Importance="high" />
  <Message Text="as Disable_RegisterOfficeAddin parameter is $(Disable_RegisterOfficeAddin)" Condition="'$(Disable_RegisterOfficeAddin)' == 'disable'" Importance="high" />
    
</Target>

The Exception

Let me provide you with my full exception from VSTO-project building

The "SetOffice2007AddInRegistration" task failed unexpectedly. System.IO.IOException: Illegal operation attempted on a registry key that has been marked for deletion.

BTW, there are no "Clean Project" commands in my build processes.

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(314,9): error MSB4018: The "SetOffice2007AddInRegistration" task failed unexpectedly. [C:\Build-Machine\My\Build\Source\MyAddin.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(314,9): error MSB4018: System.IO.IOException: Illegal operation attempted on a registry key that has been marked for deletion. [C:\Build-Machine\My\Build\Source\MyAddin.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(314,9): error MSB4018:  [C:\Build-Machine\My\Build\Source\MyAddin.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(314,9): error MSB4018:    at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str) [C:\Build-Machine\My\Build\Source\MyAddin.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(314,9): error MSB4018:    at Microsoft.Win32.RegistryKey.SetValue(String name, Object value, RegistryValueKind valueKind) [C:\Build-Machine\My\Build\Source\MyAddin.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(314,9): error MSB4018:    at Microsoft.VisualStudio.Tools.Office.Runtime.AddInRegistryKeyManager.CreateKey(RegistryKey rootKey, String url, String addInName, String officeApplication, String friendlyName, String description, Int32 loadBehavior) [C:\Build-Machine\My\Build\Source\MyAddin.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(314,9): error MSB4018:    at Microsoft.VisualStudio.Tools.Office.Runtime.AddInRegistryKeyManager.RegisterAddIn(OfficeSolutionMetadata parser) [C:\Build-Machine\My\Build\Source\MyAddin.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(314,9): error MSB4018:    at Microsoft.VisualStudio.Tools.Office.Runtime.AddInRegistryKeyManager.RegisterAddIn(Uri manifestUri, String addInName, String officeApplication, String friendlyName, String description, Int32 loadBehavior, String compatibleFrameworkXML, Boolean runLocal) [C:\Build-Machine\My\Build\Source\MyAddin.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(314,9): error MSB4018:    at Microsoft.VisualStudio.Tools.Office.BuildTasks.SetOffice2007AddInRegistration.Execute() [C:\Build-Machine\My\Build\Source\MyAddin.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(314,9): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [C:\Build-Machine\My\Build\Source\MyAddin.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(314,9): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext() [C:\Build-Machine\My\Build\Source\MyAddin.csproj]

Upvotes: 2

Eugene Astafiev
Eugene Astafiev

Reputation: 49453

Addin installed when build/rebuild project or solution. Installed addin even not shown in Programs and Features.

The add-in is not actually installed. It is registered, see Registry Entries for Application-Level Add-Ins for more more information.

You may try to add a post-build action to remove the registry keys to prevent it from loading (or just change the LoadBehavior key).

Upvotes: 1

Related Questions