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.

306 lines
9.0 KiB

  1. //-----------------------------------------------------------------------------
  2. // NMGR_CClientDataExtractor.h
  3. //
  4. //
  5. // This header containes the template implementation of T_DataExtractor.
  6. //
  7. // The name of this header will be changing!!!!!!
  8. //
  9. //
  10. //
  11. //
  12. //
  13. // Copyright (c) 1997-1999 Microsoft Corporation
  14. //-----------------------------------------------------------------------------
  15. #if !defined(__CClientDataExtractor_h)
  16. #define __CClientDataExtractor_h
  17. #include "SimpleArray.h"
  18. //-----------------------------------------------------------------------------
  19. // template T_DataExtractor
  20. //
  21. //
  22. // This template class allows you to act as if a pointer to an IDataObject
  23. // is the clipboard format that you are trying to extract from the IDataObject.
  24. // What????
  25. //
  26. // Ok. Given that any IDataObject exposes one of more CCF_'s (Clip board formats)
  27. // you want to ask the IDataObject for a specific CCF_. Using this template
  28. // allows you to "auto-magically" handle both the asking of the question
  29. // "Does this data object support this CCF_?" and the subsequent extraction of
  30. // particular clip board format.
  31. //
  32. // Syntax:
  33. //
  34. // T_DataExtractor<__type,CCF_> data;
  35. //
  36. // __type is the actual type of data you hope to extract from the IDataObject
  37. // CCF_ is the registered clip board format for the given type that you want
  38. // to extract.
  39. //
  40. //
  41. // Examples:
  42. // int : T_DataExtractor<int, CCF_INT> iMyInt;
  43. // CClass * : T_DataExtractor<CClass*, CCF_CCLASSPTR> pMyClass;
  44. //
  45. //
  46. template<typename TNData,const wchar_t* pClipFormat>
  47. class T_DataExtractor
  48. {
  49. private:
  50. typedef CSimpleArray<HGLOBAL> HGlobVector;
  51. IDataObject * m_pDataObject; // Wrapped Data Object
  52. HGlobVector m_MemoryHandles; // Memory Allocated
  53. TNData * m_pData; // "Cached" Value
  54. static UINT m_nClipFormat; // Registered Clipboard Format
  55. protected:
  56. //-------------------------------------
  57. // Extract : Does the data extraction.
  58. TNData* Extract()
  59. {
  60. HGLOBAL hMem = GlobalAlloc(GMEM_SHARE,sizeof(TNData));
  61. TNData * pRet = NULL;
  62. if(hMem != NULL)
  63. {
  64. m_MemoryHandles.Add(hMem);
  65. STGMEDIUM stgmedium = { TYMED_HGLOBAL, (HBITMAP) hMem};
  66. FORMATETC formatetc = { m_nClipFormat, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  67. if(m_pDataObject->GetDataHere(&formatetc, &stgmedium) == S_OK )
  68. {
  69. pRet = reinterpret_cast<TNData*>(hMem);
  70. }
  71. }
  72. return pRet;
  73. }
  74. public:
  75. //---------------------------------------------------------
  76. // TDataExtractor : C-Tor
  77. //
  78. // Create an extractor object from an IDataObject pointer
  79. //
  80. // After the object has been constructed, you must test
  81. // to see if the IDataObject pointer exposed the
  82. // clipboard format that you were looking for. If a call
  83. // to IsValidData returns true you know two things.
  84. //
  85. // 1) The data object exposed the clipboard format
  86. // you asked for.
  87. //
  88. // 2.) This class was able to extract a copy of the data
  89. // and now holds a local copy of the data.
  90. //
  91. // _pObject : Pointer to the IDataObject we "manage"
  92. // bAutoExtract : Automatically attempt to extract
  93. // the data from the IDataObject pointer
  94. //
  95. T_DataExtractor(IDataObject * _pObject,bool bAutoExtract = true)
  96. {
  97. m_pDataObject = _pObject;
  98. m_pData = NULL;
  99. if(m_pDataObject)
  100. {
  101. if(bAutoExtract)
  102. {
  103. m_pData = Extract();
  104. }
  105. m_pDataObject->AddRef();
  106. }
  107. }
  108. //-------------------------------------------------------
  109. // IsValidData: True if the clipboard format
  110. // was exposed by the IDataObject and
  111. // was copied into our local version.
  112. //
  113. // This is only useful if you construct the
  114. // class with bAutoExtract = true!!!!!!
  115. //
  116. // Note: No guarentee is made for the quality
  117. // of the data. This just indicates
  118. // that the data was extracted.
  119. bool IsValidData()
  120. {
  121. return m_pData != NULL;
  122. }
  123. //-------------------------------------------------------
  124. // ~T_DataExtractor : D-Tor
  125. //
  126. // Cleans up any allocated memory and releases our
  127. // AddRef on the IDataObject.
  128. //
  129. ~T_DataExtractor()
  130. {
  131. HGLOBAL walk;
  132. for(int i = 0; i > m_MemoryHandles.GetSize(); i++)
  133. {
  134. walk = m_MemoryHandles[i];
  135. GlobalFree(walk);
  136. }
  137. m_pDataObject->Release();
  138. }
  139. //-------------------------------------------------------
  140. // operator TNData
  141. //
  142. // This conversion operator should allow you to act apon
  143. // this class as if it was the underlying data type that
  144. // was extracted from the IDataObject
  145. //
  146. // i.e. Pretend CCF_INT exposes an integer:
  147. //
  148. // void SomeIntFunction(int iMyData) {}
  149. //
  150. // T_DataExtractor<int,CC_INT> iMyInt;
  151. //
  152. // SomeIntFunction(iMyInt);
  153. //
  154. //
  155. operator TNData()
  156. {
  157. return *m_pData;
  158. }
  159. //-------------------------------------------------------
  160. // TNData operator->
  161. //
  162. // If a clpboard format is exposed as a pointer, this
  163. // will allow you to use the T_DataExtractor class as
  164. // if it were the actual underlying pointer type.
  165. //
  166. // i.e.
  167. //
  168. // class CMyClass;
  169. //
  170. // T_DataExtractor<CMyClass *,CCF_MYCLASS> pMyClass;
  171. //
  172. // pMyClass->SomeMemberFunction();
  173. //
  174. //
  175. TNData operator->()
  176. {
  177. return *m_pData;
  178. }
  179. //-------------------------------------------------------
  180. // GetDataPointer
  181. //
  182. // In the case that you need to extract a pointer to
  183. // the acutal data item. (Say extracting the clipboard format
  184. // increments a value or something.) This will alow you to
  185. // Get a pointer to the data.
  186. //
  187. // This is also very useful if the data item is quite large.
  188. // It would be very expensive to be continuely accessing
  189. // the data via the above operators.
  190. //
  191. // Each time you call this member a NEW data item will be
  192. // extracted. If your data item is large, make sure that
  193. // you construct the class without automatically extracting
  194. // the clipboard format.
  195. //
  196. TNData * GetDataPointer()
  197. {
  198. return Extract();
  199. }
  200. };
  201. template<typename TNData,const wchar_t* pClipFormat>
  202. UINT T_DataExtractor<TNData,pClipFormat>::m_nClipFormat = RegisterClipboardFormatW(pClipFormat);
  203. template<const wchar_t* pClipFormat>
  204. class T_bstr_tExtractor
  205. {
  206. private:
  207. IDataObject * m_pDataObject;
  208. _bstr_t m_sString;
  209. bool m_bIsValidData;
  210. static UINT m_nClipFormat; // Registered Clipboard Format
  211. protected:
  212. void GetString()
  213. {
  214. STGMEDIUM stgmedium = { TYMED_HGLOBAL, NULL };
  215. FORMATETC formatetc = { m_nClipFormat, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  216. if(m_pDataObject->GetData(&formatetc, &stgmedium) == S_OK )
  217. {
  218. m_sString = reinterpret_cast<wchar_t*>(stgmedium.hGlobal);
  219. m_bIsValidData = true;
  220. GlobalFree(stgmedium.hGlobal);
  221. }
  222. }
  223. public:
  224. T_bstr_tExtractor(IDataObject * _pDO)
  225. {
  226. m_bIsValidData = false;
  227. m_pDataObject = _pDO;
  228. m_pDataObject->AddRef();
  229. GetString();
  230. m_pDataObject->Release();
  231. }
  232. ~T_bstr_tExtractor()
  233. {
  234. }
  235. operator _bstr_t&()
  236. {
  237. return m_sString;
  238. }
  239. bool IsValidData() { return m_bIsValidData; }
  240. };
  241. template<const wchar_t* pClipFormat>
  242. UINT T_bstr_tExtractor<pClipFormat>::m_nClipFormat = RegisterClipboardFormatW(pClipFormat);
  243. #endif // __CCientDataExtractor_h