PageRenderTime 38ms CodeModel.GetById 27ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSDllCOMServer/SimpleObject.cs

#
C# | 191 lines | 75 code | 28 blank | 88 comment | 3 complexity | dbb2287b3c7193835bee0cc0ee62298b MD5 | raw file
  1/****************************** Module Header ******************************\
  2* Module Name:  SimpleObject.cs
  3* Project:      CSDllCOMServer
  4* Copyright (c) Microsoft Corporation.
  5* 
  6* This sample focuses on exposing .NET Framework components to COM, which 
  7* allows us to write a .NET type and consuming that type from unmanaged code 
  8* with distinct activities for COM developers. The code file defines a COM 
  9* component whose class interface is explicitly defined:
 10* 
 11* SimpleObject - [Explicitly Define a Class Interface]
 12* 
 13* Program ID: CSDllCOMServer.CSExplicitInterfaceObject
 14* CLSID_CSExplicitInterfaceObject: 4B65FE47-2F9D-37B8-B3CB-5BE4A7BC0926
 15* IID_ICSExplicitInterfaceObject: 32DBA9B0-BE1F-357D-827F-0196229FA0E2
 16* DIID_ICSExplicitInterfaceObjectEvents: 95DB823B-E204-428c-92A3-7FB29C0EC576
 17* LIBID_CSDllCOMServer: F0998D9A-0E79-4F67-B944-9E837F479587
 18* 
 19* Properties:
 20* // With both get and set accessor methods.
 21* public float FloatProperty
 22* 
 23* Methods:
 24* // HelloWorld returns a string "HelloWorld"
 25* public string HelloWorld();
 26* // GetProcessThreadID outputs the running process ID and thread ID
 27* public void GetProcessThreadID(out uint processId, out uint threadId);
 28* 
 29* Events:
 30* // FloatPropertyChanging is fired before new value is set to the 
 31* // FloatProperty property. The Cancel parameter allows the client to cancel 
 32* // the change of FloatProperty.
 33* void FloatPropertyChanging(float NewValue, ref bool Cancel);
 34* 
 35* -------
 36* The recommended way of modeling a .NET component to be exposed to the COM  
 37* aware clients is to do away with ClassInterface, and instead, explicitly 
 38* factor out the members to be exported into a separate interface, and have 
 39* the .NET component implement that interface. Using a Class Interface is a 
 40* quick and easy way to get the .NET component exposed to COM aware clients 
 41* (See CSImplicitInterfaceObject), but it is not the recommended way. 
 42* 
 43* This source is subject to the Microsoft Public License.
 44* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 45* All other rights reserved.
 46* 
 47* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 48* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 49* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
 50\***************************************************************************/
 51
 52#region Using directives
 53using System;
 54using System.Runtime.InteropServices;
 55#endregion
 56
 57
 58namespace CSDllCOMServer
 59{
 60    #region Interfaces
 61
 62    /// <summary>
 63    /// The public interface describing the COM interface of the coclass 
 64    /// </summary>
 65    [Guid("32DBA9B0-BE1F-357D-827F-0196229FA0E2")]          // IID
 66    [ComVisible(true)]
 67    // Dual interface by default. This allows the client to get the best of 
 68    // both early binding and late binding.
 69    //[InterfaceType(ComInterfaceType.InterfaceIsDual)]
 70    //[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
 71    //[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
 72    public interface ISimpleObject
 73    {
 74        #region Properties
 75
 76        float FloatProperty { get; set; }
 77
 78        #endregion
 79
 80        #region Methods
 81
 82        string HelloWorld();
 83
 84        void GetProcessThreadID(out uint processId, out uint threadId);
 85
 86        [ComVisible(false)]
 87        void HiddenFunction();
 88
 89        #endregion
 90    }
 91
 92    /// <summary>
 93    /// The public interface describing the events the coclass can sink
 94    /// </summary>
 95    [Guid("95DB823B-E204-428c-92A3-7FB29C0EC576")]
 96    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
 97    [ComVisible(true)]
 98    public interface ISimpleObjectEvents
 99    {
100        #region Events
101
102        [DispId(1)]
103        void FloatPropertyChanging(float NewValue, ref bool Cancel);
104
105        #endregion
106    }
107
108    #endregion
109
110    [ClassInterface(ClassInterfaceType.None)]           // No ClassInterface
111    [ComSourceInterfaces(typeof(ISimpleObjectEvents))]
112    [Guid("4B65FE47-2F9D-37B8-B3CB-5BE4A7BC0926")]      // CLSID
113    //[ProgId("CSCOMServerDll.CustomSimpleObject")]     // ProgID
114    [ComVisible(true)]
115    public class SimpleObject : ISimpleObject
116    {
117        #region Properties
118
119        /// <summary>
120        /// The private members don't make it into the type-library and are 
121        /// hidden from the COM clients.
122        /// </summary>
123        private float fField = 0;
124
125        /// <summary>
126        /// A public property with both get and set accessor methods.
127        /// </summary>
128        public float FloatProperty
129        {
130            get { return this.fField; }
131            set
132            {
133                bool cancel = false;
134                // Raise the event FloatPropertyChanging
135                if (null != FloatPropertyChanging)
136                    FloatPropertyChanging(value, ref cancel);
137                if (!cancel)
138                    this.fField = value;
139            }
140        }
141
142        #endregion
143
144        #region Methods
145
146        /// <summary>
147        /// A public method that returns a string "HelloWorld".
148        /// </summary>
149        /// <returns>"HelloWorld"</returns>
150        public string HelloWorld()
151        {
152            return "HelloWorld";
153        }
154
155        /// <summary>
156        /// A public method with two outputs: the current process Id and the
157        /// current thread Id.
158        /// </summary>
159        /// <param name="processId">[out] The current process Id</param>
160        /// <param name="threadId">[out] The current thread Id</param>
161        public void GetProcessThreadID(out uint processId, out uint threadId)
162        {
163            processId = NativeMethod.GetCurrentProcessId();
164            threadId = NativeMethod.GetCurrentThreadId();
165        }
166
167        /// <summary>
168        /// A hidden method (ComVisible = false)
169        /// </summary>
170        public void HiddenFunction()
171        {
172            Console.WriteLine("HiddenFunction is called.");
173        }
174        
175        #endregion
176
177        #region Events
178
179        [ComVisible(false)]
180        public delegate void FloatPropertyChangingEventHandler(float NewValue, ref bool Cancel);
181
182        /// <summary>
183        /// A public event that is fired before new value is set to the
184        /// FloatProperty property. The Cancel parameter allows the client 
185        /// to cancel the change of FloatProperty.
186        /// </summary>
187        public event FloatPropertyChangingEventHandler FloatPropertyChanging;
188
189        #endregion
190    }
191}