|
namespace CustomToolGenerator {
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
/// <summary>
/// This wraps the IOleServiceProvider interface and provides an easy COM+ way to get at
/// services.
/// </summary>
public class ServiceProvider : IServiceProvider, IObjectWithSite {
private static Guid IID_IUnknown = new Guid("{00000000-0000-0000-C000-000000000046}");
private IOleServiceProvider serviceProvider;
/// <summary>
/// Creates a new ServiceProvider object and uses the given interface to resolve
/// services.
/// </summary>
/// <param name='sp'>
/// The IOleServiceProvider interface to use.
/// </param>
public ServiceProvider(IOleServiceProvider sp) {
serviceProvider = sp;
}
/// <summary>
/// gives this class a chance to free its references.
/// </summary>
public virtual void Dispose() {
if (serviceProvider != null) {
serviceProvider = null;
}
}
/// <summary>
/// returns true if the given HRESULT is a failure HRESULT
/// </summary>
/// <param name="hr">HRESULT to test</param>
/// <returns>true if the HRESULT is a failure, false if not.</returns>
public static bool Failed(int hr) {
return(hr < 0);
}
/// <summary>
/// Retrieves the requested service.
/// </summary>
/// <param name='serviceClass'>
/// The class of the service to retrieve.
/// </param>
/// <returns>
/// an instance of serviceClass or null if no
/// such service exists.
/// </returns>
public virtual object GetService(Type serviceClass) {
if (serviceClass == null) {
return null;
}
return GetService(serviceClass.GUID, serviceClass);
}
/// <summary>
/// Retrieves the requested service.
/// </summary>
/// <param name='guid'>
/// The GUID of the service to retrieve.
/// </param>
/// <returns>
/// an instance of the service or null if no
/// such service exists.
/// </returns>
public virtual object GetService(Guid guid) {
return GetService(guid, null);
}
/// <summary>
/// Retrieves the requested service. The guid must be specified; the class is only
/// used when debugging and it may be null.
/// </summary>
private object GetService(Guid guid, Type serviceClass) {
// Valid, but wierd for caller to init us with a NULL sp
//
if (serviceProvider == null) {
return null;
}
object service = null;
// No valid guid on the passed in class, so there is no service for it.
//
if (guid.Equals(Guid.Empty)) {
return null;
}
// We provide a couple of services of our own.
//
if (guid.Equals(typeof(IOleServiceProvider).GUID)) {
return serviceProvider;
}
if (guid.Equals(typeof(IObjectWithSite).GUID)) {
return (IObjectWithSite)this;
}
IntPtr pUnk;
int hr = serviceProvider.QueryService(ref guid, ref IID_IUnknown, out pUnk);
if (Succeeded(hr) && (pUnk != IntPtr.Zero)) {
service = Marshal.GetObjectForIUnknown(pUnk);
Marshal.Release(pUnk);
}
return service;
}
/// <summary>
/// Retrieves the current site object we're using to
/// resolve services.
/// </summary>
/// <param name='riid'>
/// Must be IServiceProvider.class.GUID
/// </param>
/// <param name='ppvSite'>
/// Outparam that will contain the site object.
/// </param>
/// <seealso cref='IObjectWithSite'/>
void IObjectWithSite.GetSite(ref Guid riid, object[] ppvSite) {
ppvSite[0] = GetService(riid);
}
/// <summary>
/// Sets the site object we will be using to resolve services.
/// </summary>
/// <param name='pUnkSite'>
/// The site we will use. This site will only be
/// used if it also implements IOleServiceProvider.
/// </param>
/// <seealso cref='IObjectWithSite'/>
void IObjectWithSite.SetSite(object pUnkSite) {
if (pUnkSite is IOleServiceProvider) {
serviceProvider = (IOleServiceProvider)pUnkSite;
}
}
/// <summary>
/// returns true if the given HRESULT is a success HRESULT
/// </summary>
/// <param name="hr">HRESULT to test</param>
/// <returns>true if the HRESULT is a success, false if not.</returns>
public static bool Succeeded(int hr) {
return(hr >= 0);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.