Ver versión en español    
 
Home
10 Reasons to use MZ-Tools
MZ-Tools 6.0 for VS.NET
Editions
Features
Online Documentation
MZ-Tools SDK
Download
Purchase
Version History (RSS)  
FAQ & Support
MZ-Tools 3.0 for VB6 & VBA
Features
Online Documentation
Download (freeware)
Donations (Paypal)
Version History (RSS)  
FAQ & Support
User Reviews
Community Place
Contact  
For Add-In Developers
About
   
 
User Testimonials

I'm an avid supporter of MZ-Tools. It's a product I couldn't do without and your level of support is outstanding.

Jan Hyde (Visual Basic MVP)

You will soon wonder how you ever lived without it.

Andy Maggs

More user reviews
 
HOWTO: Get a Visual Studio service from an add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: February 2007   Microsoft Visual Studio .NET 2003
      Microsoft Visual Studio 2005

Introduction

There are two main forms of extensibility for Visual Studio: add-ins and VS packages:

  • Add-ins are a high-level form of extensibility, based on the object model provided by EnvDTE.dll (and other DLLs as explained in the article INFO: Assemblies used in Visual Studio Extensibility)
  • VS packages are a low-level form of extensibility based on the Visual Studio 2005 SDK (or VSIP for previous versions of Visual Studio). Visual Studio works with registered packages, which provide services to the IDE. You can learn more in the Visual Studio 2005 SDK documentation.

While the extensibility model for add-ins (EnvDTE) is powerful enough for most add-ins, sometimes you need some functionality only available from Visual Studio services. Fortunately you don't need to convert your add-in to a VSPackage to get that functionality. This article explains how to get it.

More Information

Several objects provided by the extensibility model for add-ins, such as EnvDTE.DTE, are service providers and implement the Microsoft.VisualStudio.OLE.Interop.IServiceProvider (OLE) interface (do not confuse it with the System.IServiceProvider interface of the .NET Framework) and its QueryService method. You can get the definition of this interface in your source code by the following ways:

  • For Visual Studio .NET 2002/2003, the Microsoft.VisualStudio.OLE.Interop.dll assembly is supplied with Visual Studio Industry Partner Program (VSIP) SDK 2003 Extras, which in turn requires Visual Studio Industry Partner Program (VSIP) SDK 2003. Once installed, the required assembly is located at the folder:

    C:\Program Files\VSIP 7.1\EnvSDK\common\VSIA

    This DLL is redistributable using the MSI package C:\Program Files\VSIP 7.1\EnvSDK\VSIP Interop Assembly Redist.msi (which includes two other assemblies), but as long as you keep it private for your add-in, you don't need to redistribute the other two assemblies.

  • For Visual Studio 2005, the Microsoft.VisualStudio.OLE.Interop.dll assembly is supplied through two ways:
    • With Visual Studio 2005 SDK. Once installed, the required assembly is located at the folder:

      C:\Program Files\Visual Studio 2005 SDK\<year.month>\VisualStudioIntegration\Common\Assemblies
    • Since Visual Studio 2005 installs it in the GAC, you can get it from there (more simple than installing the SDK). See the article HOWTO: Reference a Visual Studio assembly in the GAC from an add-in.

    In both cases you don't need to redistribute the assembly since it is installed in the GAC by Visual Studio 2005.

  • If you don't want to reference that assembly at all in your add-in, you can declare it in your own source code:
    <ComVisible(True), ComImport(), Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), _
    InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
    Friend Interface IServiceProvider
       <PreserveSig()> _
       Function QueryService(<InAttribute()> ByRef guidService As Guid, _
          <InAttribute()> ByRef riid As Guid, <OutAttribute()> ByRef ppvObject As IntPtr) As Integer
    End Interface

Services are identified either by a GUID or by a type, so you can use the following helper functions to get a service from an object that implements the IServiceProvider:

Public Function GetService(ByVal serviceProvider As Object, ByVal type As System.Type) As Object
   Return GetService(serviceProvider, type.GUID)
End Function
Public Function GetService(ByVal serviceProvider As Object, ByVal guid As System.Guid) As Object
   Dim objService As Object = Nothing
   Dim objIServiceProvider As Microsoft.VisualStudio.OLE.Interop.IServiceProvider
   Dim objIntPtr As IntPtr
   Dim hr As Integer
   Dim objSIDGuid As Guid
   Dim objIIDGuid As Guid
   objSIDGuid = guid
   objIIDGuid = objSIDGuid
   objIServiceProvider = CType(serviceProvider, Microsoft.VisualStudio.OLE.Interop.IServiceProvider)
   hr = objIServiceProvider.QueryService(objSIDGuid, objIIDGuid, objIntPtr)
   If hr <> 0 Then
      System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr)
   ElseIf Not objIntPtr.Equals(IntPtr.Zero) Then
      objService = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(objIntPtr)
      System.Runtime.InteropServices.Marshal.Release(objIntPtr)
   End If
   Return objService
End Function

Since the service is returned as Object, you will likely need to cast it to the actual service, so you will need to reference the assembly containing the service (see the article above).

The following sample shows how to enumerate the data connections of the Server Explorer using the Microsoft.VisualStudio.Data.Interop.IVsDataConnectionsService service from an add-in for Visual Studio 2005 (you need to add a reference to the Microsoft.VisualStudio.Data.Interop.dll assembly):

Dim objService As Object
Dim objIVsDataConnectionsService As Microsoft.VisualStudio.Data.Interop.IVsDataConnectionsService
Dim iConnectionIndex As Integer
Dim sConnectionName As String
objService = GetService(_applicationObject, GetType(Microsoft.VisualStudio.Data.Interop.IVsDataConnectionsService))
objIVsDataConnectionsService = CType(objService, Microsoft.VisualStudio.Data.Interop.IVsDataConnectionsService)
For iConnectionIndex = 0 To objIVsDataConnectionsService.Count - 1
   sConnectionName = objIVsDataConnectionsService.GetConnectionName(iConnectionIndex)
   System.Windows.Forms.MessageBox.Show(sConnectionName)
Next

Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this

MZ-Tools 6.0 for Visual Studio .NET

You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:

Buy MZ-Tools Now Download MZ-Tools Demo

   Top