Register a library with the object manager

Symbols-browsing tools, such as Class View, Object Browser, Call Browser and Find Symbol Results, enable you to view symbols in your project or in external components. The symbols include namespaces, classes, interfaces, methods, and other language elements. The libraries track these symbols and expose them to the Visual Studio object manager that populates the tools with the data.

The object manager keeps track of all available libraries. Each library must register with the object manager before providing symbols for the symbol-browsing tools.

Typically, you register a library when a VSPackage loads. However, it can be done at another time as needed. You unregister the library when the VSPackage shuts down.

To register a library, use the RegisterLibrary method. For a managed code library, use the RegisterSimpleLibrary method.

To unregister a library, use the UnregisterLibrary method.

To obtain a reference to the object manager, IVsObjectManager2, pass the SVsObjectManager service ID to GetService method.

Register and unregister a library with the object manager

To register a library with the object manager

  1. Create a library.

    private CallBrowser.Library m_CallBrowserLibrary = null;
    private uint m_nLibraryCookie = 0;
    // Create Library.
    m_CallBrowserLibrary = new CallBrowser.Library();
    
    
  2. Obtain a reference to an object of the IVsObjectManager2 type and call the RegisterSimpleLibrary method.

    private void RegisterLibrary()
    {
        if (m_nLibraryCookie != 0)
            throw new Exception("Library already registered with Object Manager");
    
        // Obtain a reference to IVsObjectManager2 type object.
        IVsObjectManager2 objManager =
                          GetService(typeof(SVsObjectManager)) as IVsObjectManager2;
        if (objManager == null)
            throw new NullReferenceException("GetService failed for SVsObjectManager");
    
        try
        {
            int hr =
                objManager.RegisterSimpleLibrary(m_CallBrowserLibrary,
                                                 out m_nLibraryCookie);
                Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);
        }
        catch (Exception e)
        {
            // Code to handle any CLS-compliant exception.
            Trace.WriteLine(e.Message);
            throw;
        }
    }
    
    

To unregister a library with the object manager

  1. Obtain a reference to an object of the IVsObjectManager2 type and call the UnregisterLibrary method.

    private void UnregisterLibrary()
    {
        if (m_nLibraryCookie != 0)
        {
            // Obtain a reference to IVsObjectManager2 type object.
            IVsObjectManager2 objManager = GetService(typeof(SVsObjectManager)) as IVsObjectManager2;
            if (objManager == null)
                throw new NullReferenceException("GetService failed for SVsObjectManager");
    
            try
            {
                objManager.UnregisterLibrary(m_nLibraryCookie);
            }
            catch (Exception e)
            {
                // Code to handle any CLS-compliant exception.
                Trace.WriteLine(e.Message);
                throw;
            }
            finally
            {
                m_nLibraryCookie = 0;
            }
        }
    }