Exemple #1
0
        private InterfaceType OleService <InterfaceType>(Type serviceType)
            where InterfaceType : class
        {
            Guid          serviceGuid   = serviceType.GUID;
            Guid          interfaceGuid = typeof(InterfaceType).GUID;
            IntPtr        unknown       = IntPtr.Zero;
            InterfaceType service       = null;
            int           hr            = psp.QueryService(ref serviceGuid, ref interfaceGuid, out unknown);

            if (hr != VSConstants.S_OK)
            {
                Marshal.ThrowExceptionForHR(hr);
            }

            try
            {
                service = (InterfaceType)Marshal.GetObjectForIUnknown(unknown);
            }
            finally
            {
                if (unknown != IntPtr.Zero)
                {
                    Marshal.Release(unknown);
                }
            }
            return(service);
        }
Exemple #2
0
        internal static object GetService(Microsoft.VisualStudio.OLE.Interop.IServiceProvider serviceProvider, Guid guidService, bool unique)
        {
            Guid   guidInterface = VSConstants.IID_IUnknown;
            IntPtr ptrObject     = IntPtr.Zero;
            object service       = null;

            int hr = serviceProvider.QueryService(ref guidService, ref guidInterface, out ptrObject);

            if (hr >= 0 && ptrObject != IntPtr.Zero)
            {
                try
                {
                    if (unique)
                    {
                        service = Marshal.GetUniqueObjectForIUnknown(ptrObject);
                    }
                    else
                    {
                        service = Marshal.GetObjectForIUnknown(ptrObject);
                    }
                }
                finally
                {
                    Marshal.Release(ptrObject);
                }
            }

            return(service);
        }
        protected override object GetServiceImpl(Guid serviceGuid)
        {
            IntPtr zero = IntPtr.Zero;
            Guid   unk  = new Guid("00000000-0000-0000-c000-000000000046");
            Guid   self = new Guid("6d5140c1-7436-11ce-8034-00aa006009fa");

            if (serviceGuid == self)
            {
                return(this);
            }

            int status = _psp.QueryService(ref serviceGuid, ref unk, out zero);

            if (status < 0)
            {
                if ((status == -2147467259) || (status == -2147467262))
                {
                    return(null);
                }
                Marshal.ThrowExceptionForHR(status);
            }
            object objectForIUnknown = Marshal.GetObjectForIUnknown(zero);

            Marshal.Release(zero);
            return(objectForIUnknown);
        }
Exemple #4
0
 protected void SetWaitCursor()
 {
     if (this.site != null)
     {
         IVsHierarchy service = this.GetService(typeof(IVsHierarchy)) as IVsHierarchy;
         if (service != null)
         {
             Microsoft.VisualStudio.OLE.Interop.IServiceProvider ppSP = null;
             if (service.GetSite(out ppSP) == 0)
             {
                 IntPtr ptr;
                 Guid   gUID = typeof(SVsUIShell).GUID;
                 Guid   riid = typeof(IVsUIShell).GUID;
                 if (ErrorHandler.Succeeded(ppSP.QueryService(ref gUID, ref riid, out ptr)))
                 {
                     IVsUIShell objectForIUnknown = Marshal.GetObjectForIUnknown(ptr) as IVsUIShell;
                     if (objectForIUnknown != null)
                     {
                         objectForIUnknown.SetWaitCursor();
                     }
                     Marshal.Release(ptr);
                 }
             }
         }
     }
 }
Exemple #5
0
        private T GetService <T>(ITextBuffer textBuffer, Type serviceType)
        {
            var vsBuffer = adaptersFactory.GetBufferAdapter(textBuffer);

            if (vsBuffer == null)
            {
                return(default(T));
            }

            Guid            guidServiceProvider = VSConstants.IID_IUnknown;
            IObjectWithSite objectWithSite      = vsBuffer as IObjectWithSite;
            IntPtr          ptrServiceProvider  = IntPtr.Zero;

            objectWithSite.GetSite(ref guidServiceProvider, out ptrServiceProvider);
            Microsoft.VisualStudio.OLE.Interop.IServiceProvider serviceProvider =
                (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)Marshal.GetObjectForIUnknown(ptrServiceProvider);

            Guid   guidService   = serviceType.GUID;
            Guid   guidInterface = typeof(T).GUID;
            IntPtr ptrObject     = IntPtr.Zero;

            int hr = serviceProvider.QueryService(ref guidService, ref guidInterface, out ptrObject);

            if (ErrorHandler.Failed(hr) || ptrObject == IntPtr.Zero)
            {
                return(default(T));
            }

            T result = (T)Marshal.GetObjectForIUnknown(ptrObject);

            Marshal.Release(ptrObject);

            return(result);
        }
Exemple #6
0
 public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
 {
     if (_vsServiceProvider != null)
     {
         return(_vsServiceProvider.QueryService(ref guidService, ref riid, out ppvObject));
     }
     else
     {
         ppvObject = IntPtr.Zero;
         return(VSConstants.E_NOINTERFACE);
     }
 }
        // ------------------------------------------------------
        /// <summary>
        /// Gets the IVsTrackProjectDocuments2 service from Visual Studio.
        /// </summary>
        /// <returns>
        /// The IVsTrackProjectDocuments2 interface reference.
        /// </returns>
        private IVsTrackProjectDocuments2 GetTrackProjectDocuments()
        {
            Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp =
                (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)
                GetService(typeof(DTE));

            Guid   guidSP  = typeof(SVsTrackProjectDocuments).GUID;
            Guid   guidIID = typeof(IVsTrackProjectDocuments2).GUID;
            IntPtr ptrUnknown;

            sp.QueryService(ref guidSP, ref guidIID, out ptrUnknown);
            IVsTrackProjectDocuments2 vsTrackProjDocs =
                (IVsTrackProjectDocuments2)
                Marshal.GetObjectForIUnknown(ptrUnknown);

            Marshal.Release(ptrUnknown);

            return(vsTrackProjDocs);
        }
Exemple #8
0
        /// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
        /// <param term='commandName'>The name of the command to execute.</param>
        /// <param term='executeOption'>Describes how the command should be run.</param>
        /// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
        /// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
        /// <param term='handled'>Informs the caller if the command was handled or not.</param>
        /// <seealso class='Exec' />
        public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                //<Snippet2>
                if (commandName == "Addin.Connect.Addin")
                {
                    IOleServiceProvider sp = (IOleServiceProvider)
                                             _applicationObject;
                    Guid   SID = typeof(SVsUIShell).GUID;
                    Guid   IID = typeof(IVsUIShell).GUID;
                    IntPtr output;
                    sp.QueryService(ref SID, ref IID, out output);
                    IVsUIShell uiShell = (IVsUIShell)Marshal.GetObjectForIUnknown(output);

                    Guid clsid = Guid.Empty;
                    int  result;
                    uiShell.ShowMessageBox(
                        0,
                        ref clsid,
                        "MyAddin",
                        string.Format(
                            CultureInfo.CurrentCulture, "Inside " + this.ToString()),
                        string.Empty,
                        0,
                        OLEMSGBUTTON.OLEMSGBUTTON_OK,
                        OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
                        OLEMSGICON.OLEMSGICON_INFO,
                        0,
                        out result);

                    handled = true;
                    return;
                }
                //</Snippet2>
            }
        }
        /// <summary>
        /// Gets the specified service from known service provider and for given GUID.
        /// </summary>
        public static object GetService(Microsoft.VisualStudio.OLE.Interop.IServiceProvider serviceProvider, Guid serviceGUID)
        {
            object objService = null;
            IntPtr objInPtr;
            Guid   objSIDGuid = serviceGUID;
            Guid   objIIDGuid = serviceGUID;

            int hResult = serviceProvider.QueryService(ref objSIDGuid, ref objIIDGuid, out objInPtr);

            if (hResult != 0)
            {
                //Marshal.ThrowExceptionForHR(hResult);
                return(null);
            }

            if (objInPtr != IntPtr.Zero)
            {
                objService = Marshal.GetObjectForIUnknown(objInPtr);
                Marshal.Release(objInPtr);
            }

            return(objService);
        }
Exemple #10
0
 public int QueryService(ref Guid sid, ref Guid iid, out IntPtr ppvObject)
 {
     return(_otherServiceProvider.QueryService(ref sid, ref iid, out ppvObject));
 }
Exemple #11
0
        /// <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 serviceType)
        {
            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(TRACESERVICE.TraceVerbose, "\tNo SID -- Guid is empty");
                return(null);
            }

            // We provide a couple of services of our own.
            //
            if (defaultServices)
            {
                if (guid.Equals(NativeMethods.IID_IServiceProvider))
                {
                    return(serviceProvider);
                }
                if (guid.Equals(NativeMethods.IID_IObjectWithSite))
                {
                    return((IObjectWithSite)this);
                }
            }

            IntPtr pUnk    = IntPtr.Zero;
            Guid   guidUnk = NativeMethods.IID_IUnknown;
            int    hr      = serviceProvider.QueryService(ref guid, ref guidUnk, out pUnk);

            if ((NativeMethods.Succeeded(hr)) && (IntPtr.Zero != pUnk))
            {
                try {
                    service = Marshal.GetObjectForIUnknown(pUnk);
                }
                finally {
                    Marshal.Release(pUnk);
                }
            }
            else
            {
                service = null;

                // These may be interesting to log.
                //
                Debug.WriteLineIf(TRACESERVICE.TraceVerbose, "\tQueryService failed");

                #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.
                //
                pUnk = IntPtr.Zero;
                hr   = serviceProvider.QueryService(ref guid, ref guid, out pUnk);

                if ((NativeMethods.Succeeded(hr)) && (IntPtr.Zero != pUnk))
                {
                    object obj = null;
                    try {
                        obj = Marshal.GetObjectForIUnknown(pUnk);
                    }
                    finally {
                        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 " + (serviceType != null ? serviceType.Name : guid.ToString()) +
                                 " implements it's own interface, but does not implement IUnknown!\r\n" +
                                 "This is a bad service implementation, not a problem in the CLR service provider mechanism." + obj.ToString());
                }
                #endif
            }

            return(service);
        }