PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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. #region Using directives
  52. using System;
  53. using System.Runtime.InteropServices;
  54. #endregion
  55. namespace CSDllCOMServer
  56. {
  57. #region Interfaces
  58. /// <summary>
  59. /// The public interface describing the COM interface of the coclass
  60. /// </summary>
  61. [Guid("32DBA9B0-BE1F-357D-827F-0196229FA0E2")] // IID
  62. [ComVisible(true)]
  63. // Dual interface by default. This allows the client to get the best of
  64. // both early binding and late binding.
  65. //[InterfaceType(ComInterfaceType.InterfaceIsDual)]
  66. //[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  67. //[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
  68. public interface ISimpleObject
  69. {
  70. #region Properties
  71. float FloatProperty { get; set; }
  72. #endregion
  73. #region Methods
  74. string HelloWorld();
  75. void GetProcessThreadID(out uint processId, out uint threadId);
  76. [ComVisible(false)]
  77. void HiddenFunction();
  78. #endregion
  79. }
  80. /// <summary>
  81. /// The public interface describing the events the coclass can sink
  82. /// </summary>
  83. [Guid("95DB823B-E204-428c-92A3-7FB29C0EC576")]
  84. [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
  85. [ComVisible(true)]
  86. public interface ISimpleObjectEvents
  87. {
  88. #region Events
  89. [DispId(1)]
  90. void FloatPropertyChanging(float NewValue, ref bool Cancel);
  91. #endregion
  92. }
  93. #endregion
  94. [ClassInterface(ClassInterfaceType.None)] // No ClassInterface
  95. [ComSourceInterfaces(typeof(ISimpleObjectEvents))]
  96. [Guid("4B65FE47-2F9D-37B8-B3CB-5BE4A7BC0926")] // CLSID
  97. //[ProgId("CSCOMServerDll.CustomSimpleObject")] // ProgID
  98. [ComVisible(true)]
  99. public class SimpleObject : ISimpleObject
  100. {
  101. #region Properties
  102. /// <summary>
  103. /// The private members don't make it into the type-library and are
  104. /// hidden from the COM clients.
  105. /// </summary>
  106. private float fField = 0;
  107. /// <summary>
  108. /// A public property with both get and set accessor methods.
  109. /// </summary>
  110. public float FloatProperty
  111. {
  112. get { return this.fField; }
  113. set
  114. {
  115. bool cancel = false;
  116. // Raise the event FloatPropertyChanging
  117. if (null != FloatPropertyChanging)
  118. FloatPropertyChanging(value, ref cancel);
  119. if (!cancel)
  120. this.fField = value;
  121. }
  122. }
  123. #endregion
  124. #region Methods
  125. /// <summary>
  126. /// A public method that returns a string "HelloWorld".
  127. /// </summary>
  128. /// <returns>"HelloWorld"</returns>
  129. public string HelloWorld()
  130. {
  131. return "HelloWorld";
  132. }
  133. /// <summary>
  134. /// A public method with two outputs: the current process Id and the
  135. /// current thread Id.
  136. /// </summary>
  137. /// <param name="processId">[out] The current process Id</param>
  138. /// <param name="threadId">[out] The current thread Id</param>
  139. public void GetProcessThreadID(out uint processId, out uint threadId)
  140. {
  141. processId = NativeMethod.GetCurrentProcessId();
  142. threadId = NativeMethod.GetCurrentThreadId();
  143. }
  144. /// <summary>
  145. /// A hidden method (ComVisible = false)
  146. /// </summary>
  147. public void HiddenFunction()
  148. {
  149. Console.WriteLine("HiddenFunction is called.");
  150. }
  151. #endregion
  152. #region Events
  153. [ComVisible(false)]
  154. public delegate void FloatPropertyChangingEventHandler(float NewValue, ref bool Cancel);
  155. /// <summary>
  156. /// A public event that is fired before new value is set to the
  157. /// FloatProperty property. The Cancel parameter allows the client
  158. /// to cancel the change of FloatProperty.
  159. /// </summary>
  160. public event FloatPropertyChangingEventHandler FloatPropertyChanging;
  161. #endregion
  162. }
  163. }