Visual Studio – v2019 – Add Reference – “Error HRESULT E_FAIL has been returned from a call to a COM component”

Background

Using Visual Studio 2019 and needing to add references to components, I accessed the “Add Reference” menu option but got the error noted below.

Error

Image

Textual

Error HRESULT E_FAIL has been returned from a call to a COM component.

TroubleShooting

Google Search

I googled on the error.

Unfortunately, the most popular Google match, a stack overflow QA post, provided too many suggestions and it was difficult to wield through each of the remediative steps.

BTW, that post is here:-

Stack Overflow
Error HRESULT E_FAIL has been returned from a call to a COM component VS2012 when debugging
Link

Simplified

Upon trying out a couple of tracks mentioned in the StackOverflow post, and not finding any joy, I knew I had to simplify my scenario a lot more and return to the basics.

Outline

Here are the steps we took towards simplicity:-

  1. Visual Studio – Create New Project
    • Closed the current solution
    • Created a new project
      • A simple .Net Console Project
      • Choices
        • .Net Core ( No )
        • .Net Framework ( Yes )
  2. Visual Studio – Add Reference
    • In Visual Studio, choose to add a new Reference
      • Click on “Add Reference”
  3. ActivityLog.XML

Visual Studio – Create New Project

Images

Image – Create a New Project

Image – Configure Your New Project

Visual Studio – Add Reference

Steps

  1. Accessed the Add Reference menu option and got an error
  2. Error
    • Title
      • The ‘ReferenceManagerPackage’ package did not load correctly.
    • Message
      • The problem may have been caused by a configuration change or by the installation of another extension.
      • You can get more information by examining the file ‘c:usersdadenijiAppDataRoamingMicrosoftVisualStudio16.0_9919f19dActivityLog.xml’
  3. Error Explanation
    • Error Text
      • c:users\dadeniji\AppData\Roaming\Microsoft\VisualStudio16.0_9919f19d\ActivityLog.xml
    • Error Template
      • c:users<username>AppDataRoamingMicrosoft\VisualStudio<version>ActivityLog.xml

Images

Image – The ‘ReferenceManagerPackage’ package did not load correctly

 

 

ActivityLog.xml

Entry

CreateInstance failed for package [ReferenceManagerPackage]Source: ‘mscorlib’ Description: Could not load type ‘Microsoft.VisualStudio.Shell.Interop.IVsReferenceManager2’ from assembly ‘Microsoft.VisualStudio.Shell.Interop.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’. System.TypeLoadException: Could not load type ‘Microsoft.VisualStudio.Shell.Interop.IVsReferenceManager2’ from assembly ‘Microsoft.VisualStudio.Shell.Interop.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’. at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type) at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase) at System.Activator.CreateInstanceFromInternal(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo) at System.AppDomain.CreateInstanceFrom(String assemblyFile, String typeName)

Image

Key Data

The key data from our error is that a type “Microsoft.VisualStudio.Shell.Interop.IVsReferenceManager2” could not be loaded.

And, that type is part of the “Microsoft.VisualStudio.Shell.Interop.11.0, Version=11.0.0.0, Culture=neutral”

 

Remediation

Outline

  1. Launch Visual Studio Command Prompt in Administrator Mode
  2. Determine the folder where our assembly ( Microsoft.VisualStudio.Shell.Interop.11.0 ) is located
  3. Change the contextual folder to the identified folder
  4. Register the assembly in GAC ( gacutil )

Launch Visual Studio Command Prompt

Outline

  1. Please launch command prompt preferably “Developer Command Prompt for VS < Version >
    • <Version> standing for the version number of your Visual Studio
  2. Confirm Command Shell (    )
    • The command shell should indicate “Administrator” by reading “Administrator:

Images

Image – Command Prompt – Search

Image – Command Prompt – Shell

 

Find Microsoft.VisualStudio.Shell.Interop.<Major>.<Minor> ( Microsoft.VisualStudio.Shell.Interop.11.0 )

Outline

  1. Find Microsoft.VisualStudio.Shell.Interop.<Major>.<Minor>
  2. In our case “Microsoft.VisualStudio.Shell.Interop.11.0
  3. There are a few options for looking for a file
    • In this post, we will use the where command

Command

Command – Where

Outline
  1. The syntax for using the where command is
    • option
      • /r ( recursive )
    • Base Folder
    • Filename
Syntax

where /r <folder-base> <filename>

Sample

where /r c: "Microsoft.VisualStudio.Shell.Interop.11.0.dll"

Output

Output – Image

Output – Textual

where /r c:\ “Microsoft.VisualStudio.Shell.Interop.11.0.dll”

c:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.Shell.Interop.11.0.dll

c:\Program Files (x86)\Microsoft Visual Studio\2017\SQL\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.Shell.Interop.11.0.dll

c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.Shell.Interop.11.0.dll

c:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.Shell.Interop.11.0.dll

Explanation

In our case, we have four matches for Microsoft.VisualStudio.Shell.Interop.11.0.dll.

Those are:-

  1. Microsoft – SQL Server Management Studio
    • Microsoft SQL Server Management Studio – Version 18
  2. Microsoft – Visual Studio
    • Microsoft Visual Studio 2017
    • Microsoft Visual Studio 2019
      • Edition – Community
      • Edition – Professional

Change Directory

Syntax


cd "c:\Program Files (x86)\Microsoft Visual Studio<version>\Professional\Common<version2>\IDE\PublicAssemblies"

Sample


cd c:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\PublicAssemblies\"

Output

Image

 

Register Assembly ( gacutil )

Outline

  1. Issue gacutil against assembly
  2. Review gac for the presence of the assembly

Register Assembly ( gacutil )

Syntax


gacutil -i Microsoft.VisualStudio.Shell.Interop.<versionMajor>.<versionMinor>.dll

Sample


gacutil -i Microsoft.VisualStudio.Shell.Interop.11.0.dll

Output

Image

Textual

Assembly successfully added to the cache.

Review Assembly ( gacutil )

Outline

Please review the registered assemblies in GAC by issuing “gacutil -l“.

Specific Assembly

Syntax

gacutil -l <assembly>

Sample

gacutil -l Microsoft.VisualStudio.Shell.Interop.11.0.dll

Output
Image

Textual

>gacutil.exe -l  "Microsoft.VisualStudio.Shell.Interop.11.0"
Microsoft (R) .NET Global Assembly Cache Utility.  Version 4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.

The Global Assembly Cache contains the following assemblies:
  Microsoft.VisualStudio.Shell.Interop.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL

Number of items = 1

Matching Assemblies

Syntax

gacutil -l | findstr <assembly>

Sample

gacutil -l | findstr "Microsoft.VisualStudio.Shell.Interop."

Output
Image

Textual

>gacutil.exe -l | findstr "Microsoft.VisualStudio.Shell.Interop"

  Microsoft.VisualStudio.Shell.Interop.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL

  Microsoft.VisualStudio.Shell.Interop.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL

  Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

  Microsoft.VisualStudio.Shell.Interop.8.0, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

  Microsoft.VisualStudio.Shell.Interop.9.0, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

>

 

Confirmation

Let us go confirm.

We do so by exiting our Visual Studio Session, restarting Visual Studio, and attempting to add a Reference.

 

References

  1. Microsoft
    • Visual Studio Community
      • ReferenceManagerPackage failed to load
        Link
      • Add a reference raise error: Error HRESULT E_FAIL has been returned from a call to a COM component
        Link
  2. Stack Overflow
    • Error HRESULT E_FAIL has been returned from a call to a COM component VS2012 when debugging
      Link
  3. Cameron Dwyer
    • How to fix “The operation could not be completed” error adding references to Visual Studio 2017
      Link
  4. SS64
    • Where Command
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s