![]() |
||||
|
Introduction There are two main forms of extensibility for Visual Studio: add-ins and VS packages:
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:
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
You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||