/// <include file='doc\StyleBuilderSite.uex' path='docs/doc[@for="StyleBuilderSite.QueryService"]/*' /> /// <devdoc> /// </devdoc> public object QueryService(Type serviceType) { Debug.Assert(serviceType != null, "null service type"); Guid serviceGuid = serviceType.GUID; object service = null; if ((site != null) && !serviceGuid.Equals(Guid.Empty)) { IntPtr pUnk; int hr = site.QueryService(ref serviceGuid, ref NativeMethods.IID_IUnknown, out pUnk); if (NativeMethods.Succeeded(hr)) { service = Marshal.GetObjectForIUnknown(pUnk); Marshal.Release(pUnk); } else { service = null; } } return(service); }
/// <include file='doc\ServiceProvider.uex' path='docs/doc[@for="ServiceProvider.GetService2"]/*' /> /// <devdoc> /// Retrieves the requested service. The guid must be specified; the class is only /// used when debugging and it may be null. /// </devdoc> private object GetService(Guid guid, Type serviceClass) { object service = null; // No valid guid on the passed in class, so there is no service for it. // if (guid.Equals(Guid.Empty)) { Debug.WriteLineIf(Switches.TRACESERVICE.TraceVerbose, "\tNo SIDSystem.Runtime.InteropServices.Guid"); return(null); } // We provide a couple of services of our own. // if (guid.Equals(IID_IServiceProvider)) { return(serviceProvider); } if (guid.Equals(IID_IObjectWithSite)) { return((NativeMethods.IObjectWithSite) this); } IntPtr pUnk; int hr = serviceProvider.QueryService(ref guid, ref NativeMethods.IID_IUnknown, out pUnk); if (NativeMethods.Failed(hr) || pUnk == (IntPtr)0) { Debug.Assert(NativeMethods.Failed(hr), "QueryService succeeded but reurned a NULL pointer."); service = null; // These may be interesting to log. // Debug.WriteLineIf(Switches.TRACESERVICE.TraceVerbose, "\tQueryService failed: " + hr.ToString()); #if DEBUG // Ensure that this service failure was not the result of a bad QI implementation. // In C++, 99% of a service query uses SID == IID, but for us, we always use IID = IUnknown // first. If the service didn't implement IUnknown correctly, we'll fail the service request // and it's very difficult to track this down. // hr = serviceProvider.QueryService(ref guid, ref guid, out pUnk); if (NativeMethods.Succeeded(hr)) { object obj = Marshal.GetObjectForIUnknown(pUnk); Marshal.Release(pUnk); // Note that I do not return this service if we succeed -- I don't // want to make debug work correctly when retail doesn't! Debug.Assert(!System.Runtime.InteropServices.Marshal.IsComObject(obj), "The service " + (serviceClass != null ? serviceClass.Name : guid.ToString()) + " implements it's own interface, but does not implelement IUnknown!\r\n" + "This is a bad service implemementation, not a problem in the CLR service provider mechanism." + obj.ToString()); } #endif } else { service = Marshal.GetObjectForIUnknown(pUnk); Marshal.Release(pUnk); } return(service); }