![]() |
||||
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 Visual Studio package 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
public object GetService(object serviceProvider, System.Type type)
{
return GetService(serviceProvider, type.GUID);
}
public object GetService(object serviceProvider, System.Guid guid)
{
object objService = null;
IServiceProvider objIServiceProvider = null;
IntPtr objIntPtr;
int hr = 0;
Guid objSIDGuid;
Guid objIIDGuid;
objSIDGuid = guid;
objIIDGuid = objSIDGuid;
objIServiceProvider = (IServiceProvider)serviceProvider;
hr = objIServiceProvider.QueryService(ref objSIDGuid, ref objIIDGuid, out objIntPtr);
if (hr != 0)
{
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr);
}
else if (!objIntPtr.Equals(IntPtr.Zero))
{
objService = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(objIntPtr);
System.Runtime.InteropServices.Marshal.Release(objIntPtr);
}
return objService;
}
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 or higher (you need to add a reference to the Microsoft.VisualStudio.Data.Interop.dll assembly, in the subfolder "Microsoft Visual Studio <version>\Common7\IDE"). The _applicationObject is the EnvDTE.DTE instance:
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
object objService = null;
Microsoft.VisualStudio.Data.Interop.IVsDataConnectionsService objIVsDataConnectionsService = null;
int iConnectionIndex = 0;
string sConnectionName = null;
objService = GetService(_applicationObject, typeof(Microsoft.VisualStudio.Data.Interop.IVsDataConnectionsService));
objIVsDataConnectionsService = (Microsoft.VisualStudio.Data.Interop.IVsDataConnectionsService) objService;
for (iConnectionIndex = 0; iConnectionIndex <= objIVsDataConnectionsService.Count - 1; iConnectionIndex++) {
sConnectionName = objIVsDataConnectionsService.GetConnectionName(iConnectionIndex);
System.Windows.Forms.MessageBox.Show(sConnectionName);
}
Related articles Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
|
| Copyright © 2000-2013 MZTools Software. All Rights Reserved. Legal Notice |