PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CppExeCOMServer/SimpleObject.cpp

#
C++ | 248 lines | 159 code | 42 blank | 47 comment | 25 complexity | 8d3fb9eb9b9ec0f5eabdd37c2f99cf2d MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. Module Name: SimpleObject.cpp
  3. Project: CppExeCOMServer
  4. Copyright (c) Microsoft Corporation.
  5. This source is subject to the Microsoft Public License.
  6. See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  7. All other rights reserved.
  8. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  9. EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  10. WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  11. \***************************************************************************/
  12. #include "SimpleObject.h"
  13. #include "CppExeCOMServer_i.c" // For component GUIDs
  14. SimpleObject::SimpleObject() : m_cRef(1), m_fField(0.0F), m_ptinfo(NULL)
  15. {
  16. }
  17. SimpleObject::~SimpleObject()
  18. {
  19. }
  20. //
  21. // IUnknown
  22. //
  23. // Query to the interface the component supported.
  24. IFACEMETHODIMP SimpleObject::QueryInterface(REFIID riid, void **ppv)
  25. {
  26. HRESULT hr = S_OK;
  27. if (IsEqualIID(IID_IUnknown, riid))
  28. {
  29. *ppv = static_cast<IUnknown *>(this);
  30. }
  31. else if (IsEqualIID(IID_IDispatch, riid)) // For implementing IDispatch
  32. {
  33. *ppv = static_cast<IDispatch *>(this);
  34. }
  35. else if (IsEqualIID(IID_ISimpleObject, riid))
  36. {
  37. *ppv = static_cast<ISimpleObject *>(this);
  38. }
  39. else
  40. {
  41. hr = E_NOINTERFACE;
  42. *ppv = NULL;
  43. }
  44. if (*ppv)
  45. {
  46. AddRef();
  47. }
  48. return hr;
  49. }
  50. // Increase the reference count for an interface on an object.
  51. IFACEMETHODIMP_(ULONG) SimpleObject::AddRef()
  52. {
  53. return InterlockedIncrement(&m_cRef);
  54. }
  55. // Decrease the reference count for an interface on an object.
  56. IFACEMETHODIMP_(ULONG) SimpleObject::Release()
  57. {
  58. ULONG cRef = InterlockedDecrement(&m_cRef);
  59. if (0 == cRef)
  60. {
  61. delete this;
  62. }
  63. return cRef;
  64. }
  65. //
  66. // IDispatch
  67. //
  68. // This method retrieves the number of type information interfaces that an
  69. // object provides, either 0 or 1. If the object provides type information,
  70. // this *pctinfo is 1; otherwise the *pctinfo is 0.
  71. IFACEMETHODIMP SimpleObject::GetTypeInfoCount(UINT *pctinfo)
  72. {
  73. if (pctinfo == NULL)
  74. {
  75. return E_POINTER;
  76. }
  77. *pctinfo = 1;
  78. return S_OK;
  79. }
  80. // This method retrieves the type information for an object.
  81. IFACEMETHODIMP SimpleObject::GetTypeInfo(UINT itinfo, LCID lcid, ITypeInfo **pptinfo)
  82. {
  83. HRESULT hr = S_OK;
  84. *pptinfo = NULL;
  85. if (itinfo != 0)
  86. {
  87. return ResultFromScode(DISP_E_BADINDEX);
  88. }
  89. if (m_ptinfo == NULL)
  90. {
  91. // Load the type info.
  92. hr = LoadTypeInfo(&m_ptinfo, LIBID_CppExeCOMServerLib, IID_ISimpleObject, 0);
  93. }
  94. if (SUCCEEDED(hr))
  95. {
  96. // AddRef and return pointer to cached typeinfo.
  97. m_ptinfo->AddRef();
  98. *pptinfo = m_ptinfo;
  99. }
  100. return hr;
  101. }
  102. // Helper function to load the type info.
  103. HRESULT SimpleObject::LoadTypeInfo(ITypeInfo **pptinfo,
  104. const CLSID& libid,
  105. const CLSID& iid,
  106. LCID lcid)
  107. {
  108. HRESULT hr;
  109. LPTYPELIB ptlib = NULL;
  110. LPTYPEINFO ptinfo = NULL;
  111. *pptinfo = NULL;
  112. // Load the type library.
  113. hr = LoadRegTypeLib(libid, 1, 0, lcid, &ptlib);
  114. if (SUCCEEDED(hr))
  115. {
  116. // Get type information for interface of the object.
  117. hr = ptlib->GetTypeInfoOfGuid(iid, &ptinfo);
  118. if (SUCCEEDED(hr))
  119. {
  120. *pptinfo = ptinfo;
  121. }
  122. ptlib->Release();
  123. }
  124. return hr;
  125. }
  126. // Maps a single member and an optional set of argument names to a
  127. // corresponding set of integer DISPIDs, which can be used on subsequent
  128. // calls to IDispatch::Invoke. The dispatch function DispGetIDsOfNames
  129. // provides a standard implementation of GetIDsOfNames.
  130. IFACEMETHODIMP SimpleObject::GetIDsOfNames(REFIID riid,
  131. LPOLESTR *rgszNames,
  132. UINT cNames,
  133. LCID lcid,
  134. DISPID* rgdispid)
  135. {
  136. HRESULT hr = S_OK;
  137. if (m_ptinfo == NULL)
  138. {
  139. // Load the type info.
  140. hr = LoadTypeInfo(&m_ptinfo, LIBID_CppExeCOMServerLib, IID_ISimpleObject, 0);
  141. }
  142. if (SUCCEEDED(hr))
  143. {
  144. hr = DispGetIDsOfNames(m_ptinfo, rgszNames, cNames, rgdispid);
  145. }
  146. return hr;
  147. }
  148. // Provides access to properties and methods exposed by an object. The
  149. // dispatch function DispInvoke Function provides a standard implementation
  150. // of IDispatch::Invoke.
  151. IFACEMETHODIMP SimpleObject::Invoke(DISPID dispidMember,
  152. REFIID riid,
  153. LCID lcid,
  154. WORD wFlags,
  155. DISPPARAMS *pdispParams,
  156. VARIANT *pvarResult,
  157. EXCEPINFO *pExcepInfo,
  158. UINT *puArgErr)
  159. {
  160. HRESULT hr = S_OK;
  161. if (m_ptinfo == NULL)
  162. {
  163. // Load the type info.
  164. hr = LoadTypeInfo(&m_ptinfo, LIBID_CppExeCOMServerLib, IID_ISimpleObject, 0);
  165. }
  166. if (SUCCEEDED(hr))
  167. {
  168. hr = DispInvoke(this, m_ptinfo, dispidMember, wFlags, pdispParams,
  169. pvarResult, pExcepInfo, puArgErr);
  170. }
  171. return hr;
  172. }
  173. //
  174. // ISimpleObject
  175. //
  176. IFACEMETHODIMP SimpleObject::get_FloatProperty(FLOAT *pVal)
  177. {
  178. *pVal = m_fField;
  179. return S_OK;
  180. }
  181. IFACEMETHODIMP SimpleObject::put_FloatProperty(FLOAT newVal)
  182. {
  183. m_fField = newVal;
  184. return S_OK;
  185. }
  186. IFACEMETHODIMP SimpleObject::HelloWorld(BSTR *pRet)
  187. {
  188. // Allocate memory for the string.
  189. *pRet = ::SysAllocString(L"HelloWorld");
  190. if (pRet == NULL)
  191. {
  192. return E_OUTOFMEMORY;
  193. }
  194. // The client is now responsible for freeing the BSTR.
  195. return S_OK;
  196. }
  197. IFACEMETHODIMP SimpleObject::GetProcessThreadID(LONG *pdwProcessId, LONG *pdwThreadId)
  198. {
  199. *pdwProcessId = GetCurrentProcessId();
  200. *pdwThreadId = GetCurrentThreadId();
  201. return S_OK;
  202. }