Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
6.2 KiB

  1. //
  2. // virtualdefs.h
  3. //
  4. // Contains definitions for pure virtual functions which must defined via
  5. // the IDispatch. This header is used in CTBGlobal.cpp and CTBShell.cpp
  6. //
  7. // WHY did I do it this way instead of object inheritance?
  8. //
  9. // OLE Automation requires the IDL file objects to inherit
  10. // IDispatch members. The IDispatch object contains pure virtual
  11. // functions as a layout, which must be defined by me, the user of it.
  12. // The problem is, I have two objects which need to define identical code.
  13. // To prevent this, I just included the source.
  14. //
  15. // Object inheritance will not work in this case because if I define
  16. // an object which defines the pure virtual functions, and eventually
  17. // inherit them through CTBShell and CTBGlobal, it will still not work
  18. // because I also inherited the automatic IDL generated header which
  19. // makes a second path to undefined pure virtual function. I COULD
  20. // make two objects with two names (to have two different parents) but
  21. // I would end up duplicating code again.. It looks like this:
  22. //
  23. // pure virtual methods
  24. // |
  25. // / \
  26. // / \
  27. // / \
  28. // / \
  29. // / \
  30. // OLE Obj 1 OLE Obj 2
  31. // | |
  32. // | <-1-- | <-2------ including this file at this layer
  33. // | |
  34. // My Obj 1 My Obj 2
  35. //
  36. //
  37. // Copyright (C) 2001 Microsoft Corporation
  38. //
  39. // Author: a-devjen (Devin Jenson)
  40. //
  41. // CTBOBJECT::Init
  42. //
  43. // Initializes the TypeInfo and RefIID.
  44. //
  45. // No return value.
  46. void CTBOBJECT::Init(REFIID RefIID)
  47. {
  48. RefCount = 0;
  49. ObjRefIID = RefIID;
  50. // Load actual "code" into memory.. its referenced as ITypeInfo,
  51. // but think of it as like a DLL, but you can't access it the
  52. // same way.
  53. if (FAILED(SCPLoadTypeInfoFromThisModule(RefIID, &TypeInfo))) {
  54. _ASSERT(FALSE);
  55. TypeInfo = NULL;
  56. }
  57. else
  58. TypeInfo->AddRef();
  59. }
  60. // CTBOBJECT::UnInit
  61. //
  62. // Releases the type info.
  63. //
  64. // No return value.
  65. void CTBOBJECT::UnInit(void)
  66. {
  67. // Release the TypeInfo if we have it
  68. if(TypeInfo != NULL)
  69. TypeInfo->Release();
  70. TypeInfo = NULL;
  71. }
  72. //
  73. //
  74. // Begin the IUnknown inherited interface
  75. //
  76. //
  77. // CTBOBJECT::QueryInterface
  78. //
  79. // This is a COM exported method used for retrieving the interface.
  80. //
  81. // Returns S_OK on success, or E_NOINTERFACE on failure.
  82. STDMETHODIMP CTBOBJECT::QueryInterface(REFIID RefIID, void **vObject)
  83. {
  84. // This interface is either IID_ITBGlobal, IID_ITBShell,
  85. // IID_IUnknown, or IID_IDispatch to get the TypeInfo...
  86. if (RefIID == ObjRefIID || RefIID == IID_IDispatch ||
  87. RefIID == IID_IUnknown)
  88. *vObject = TypeInfo != NULL ? this : NULL;
  89. // We received an unsupported RefIID
  90. else {
  91. // De-reference the passed in pointer and error out
  92. *vObject = NULL;
  93. return E_NOINTERFACE;
  94. }
  95. // Add a reference
  96. if (*vObject != NULL)
  97. ((IUnknown*)*vObject)->AddRef();
  98. return S_OK;
  99. }
  100. // CTBOBJECT::AddRef
  101. //
  102. // Simply increments a number indicating the number of objects that contain
  103. // a reference to this object.
  104. //
  105. // Returns the new reference count.
  106. STDMETHODIMP_(ULONG) CTBOBJECT::AddRef(void)
  107. {
  108. return InterlockedIncrement(&RefCount);
  109. }
  110. // CTBOBJECT::Release
  111. //
  112. // Simply decrements a number indicating the number of objects that contain
  113. // a reference to this object. If the resulting reference count is zero,
  114. // no objects contain a reference handle, therefore delete itself from
  115. // memory as it is no longer used.
  116. //
  117. // Returns the new reference count.
  118. STDMETHODIMP_(ULONG) CTBOBJECT::Release(void)
  119. {
  120. // Decrememt
  121. if (InterlockedDecrement(&RefCount) != 0)
  122. // Return the new value
  123. return RefCount;
  124. // It is 0, so delete itself
  125. delete this;
  126. return 0;
  127. }
  128. //
  129. //
  130. // Begin the IDispatch inherited interface
  131. //
  132. //
  133. // CTBOBJECT::GetTypeInfoCount
  134. //
  135. // Retrieves the number of TypeInfo's we have.
  136. //
  137. // Returns S_OK on success, or E_POINTER on failure.
  138. STDMETHODIMP CTBOBJECT::GetTypeInfoCount(UINT *TypeInfoCount)
  139. {
  140. __try {
  141. // We never have more than 1 type info per object
  142. *TypeInfoCount = 1;
  143. }
  144. __except (EXCEPTION_EXECUTE_HANDLER) {
  145. // This really should never happen...
  146. _ASSERT(FALSE);
  147. return E_POINTER;
  148. }
  149. return S_OK;
  150. }
  151. // CTBOBJECT::GetTypeInfo
  152. //
  153. // Retrieves a pointer to the specified TypeInfo.
  154. //
  155. // Returns S_OK on success, or E_POINTER on failure.
  156. STDMETHODIMP CTBOBJECT::GetTypeInfo(UINT TypeInfoNum, LCID Lcid, ITypeInfo **TypeInfoPtr)
  157. {
  158. // Check our interface first
  159. _ASSERT(TypeInfo != NULL);
  160. __try {
  161. // The only TypeInfo we have is the one in this object...
  162. *TypeInfoPtr = TypeInfo;
  163. TypeInfo->AddRef();
  164. }
  165. __except (EXCEPTION_EXECUTE_HANDLER) {
  166. // This really should never happen...
  167. _ASSERT(FALSE);
  168. return E_POINTER;
  169. }
  170. return S_OK;
  171. }
  172. // CTBOBJECT::GetIDsOfNames
  173. //
  174. // Get ID's of the specified names in our TypeInfo.
  175. //
  176. // Returns any HRESULT value.
  177. STDMETHODIMP CTBOBJECT::GetIDsOfNames(REFIID RefIID, OLECHAR **NamePtrList,
  178. UINT NameCount, LCID Lcid, DISPID *DispID)
  179. {
  180. HRESULT Result;
  181. // Check our pointer first
  182. _ASSERT(TypeInfo != NULL);
  183. // Use the TypeInfo of this function instead
  184. Result = TypeInfo->GetIDsOfNames(NamePtrList, NameCount, DispID);
  185. // Assert uncommon return values
  186. _ASSERT(Result == S_OK || Result == DISP_E_UNKNOWNNAME);
  187. return Result;
  188. }
  189. // CTBOBJECT::Invoke
  190. //
  191. // Invokes a method in the TypeInfo.
  192. //
  193. // Returns any HRESULT value.
  194. STDMETHODIMP CTBOBJECT::Invoke(DISPID DispID, REFIID RefIID, LCID Lcid,
  195. WORD Flags, DISPPARAMS *DispParms, VARIANT *Variant,
  196. EXCEPINFO *ExceptionInfo, UINT *ArgErr)
  197. {
  198. HRESULT Result;
  199. // Check our pointer first
  200. _ASSERT(TypeInfo != NULL);
  201. // Invoke the method
  202. Result = TypeInfo->Invoke(this, DispID, Flags,
  203. DispParms, Variant, ExceptionInfo, ArgErr);
  204. _ASSERT(Result == S_OK);
  205. return Result;
  206. }