Click here to Skip to main content
15,881,898 members
Articles / Programming Languages / JScript .NET

Stored Procedure Invocation Code Generator for VB, C# and JScript.NET

Rate me:
Please Sign up or sign in to vote.
4.92/5 (24 votes)
14 Jun 20038 min read 386.9K   3.4K   133  
Stored Procedure Invocation Code Generator for VB, C# and JScript.NET
//================================================================================
//
//  Copyright (c) objectnation GmbH
//
//================================================================================
//
//  Author          simon.wilson
//  Creation Date   13.09.2002
//  Creation Time   16:47:52
//  IDE Version     Microsoft Development Environment Enterprise Edition 7.00
//  OS Version      Microsoft Windows NT 5.1.2600.0
//
//================================================================================

#region Copyright � objectnation GmbH

// This software is provided 'as-is'. While the greatest care has been taken to 
// ensure that the software functions correctly, no guarantee can be made to this effect.
//
// objectnation grants permission to anyone to use this software for any purpose as long as 
// it is in compliance with the following restrictions:
//
// 1.	This software is the intellectual property and copyright of objectnation GmbH. 
//    Under no circumstances may the origin of the software be misrepresented.
//
// 2.	Should this software be used in the development of a commercial product, 
//    then an acknowledgement in that product's documentation is a requirement. 
//    The following text should be used:
//
//    This product contains code generated by objectnation SP/Invoke, 
//    copyright � objectnation GmbH (http://www.objectnation.com).
//
// 3.	The software may not be redistributed without the express written permission of 
//    the copyright holder, namely objectnation GmbH, Switzerland.

#endregion

namespace Microsoft.VisualStudio.Extensibility {

    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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions