AGCEventData.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /////////////////////////////////////////////////////////////////////////////
  2. // AGCEventData.cpp : Implementation of CAGCEventData
  3. //
  4. #include "pch.h"
  5. #include "AGCEventData.h"
  6. #include <..\TCAtl\SimpleStream.h>
  7. #include <..\TCAtl\TCNullStreamImpl.h>
  8. /////////////////////////////////////////////////////////////////////////////
  9. // CAGCEventData
  10. /////////////////////////////////////////////////////////////////////////////
  11. // Construction / Destruction
  12. /////////////////////////////////////////////////////////////////////////////
  13. // Attributes
  14. void CAGCEventData::GetVarData(BSTR* pbstrContext, BSTR* pbstrSubject,
  15. CAGCEventData::XProperties* pProperties) const
  16. {
  17. // Get a pointer to the beginning of the variable data block
  18. assert(m_pbData && m_cbData >= sizeof(XData));
  19. XData* pData = reinterpret_cast<XData*>(m_pbData);
  20. BYTE* pbData = reinterpret_cast<BYTE*>(pData + 1);
  21. // Read the context string
  22. pbData += CreateBSTRFromData_LPSTR(pbData, pbstrContext);
  23. // Read the subject name
  24. pbData += CreateBSTRFromData_LPWSTR(pbData, pbstrSubject);
  25. // Add each association to the specified property map
  26. CComBSTR bstrKey;
  27. CComVariant varItem;
  28. for (long cArgTriplets = GetArgCount(); cArgTriplets; --cArgTriplets)
  29. {
  30. // Read the argument name
  31. pbData += CreateBSTRFromData_LPSTR(pbData, &bstrKey);
  32. // Read the argument value
  33. pbData += CreateVARIANTFromData(pbData, varItem);
  34. // Add the association to the property map
  35. pProperties->insert(XProperties::value_type(bstrKey, varItem));
  36. // Free the BSTR
  37. bstrKey.Empty();
  38. }
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. // Implementation
  42. /////////////////////////////////////////////////////////////////////////////
  43. // Useful macro for implementing size calculations of variant types
  44. //
  45. #define VT_SIZE(vt, type) \
  46. case vt: \
  47. va_arg(argptr, type); \
  48. cbTotal += sizeof(type); \
  49. break;
  50. /////////////////////////////////////////////////////////////////////////////
  51. //
  52. UINT CAGCEventData::ComputeVariableDataSize(LPCSTR pszContext,
  53. LPCOLESTR pszSubject, long cArgTriplets, va_list argptr)
  54. {
  55. // Start with the string length of the specified context string, if any
  56. UINT cbTotal = pszContext ? strlen(pszContext) : 0;
  57. cbTotal += sizeof(cbTotal);
  58. // Next is the string length of the specified subject string, if any
  59. cbTotal += pszSubject ? (wcslen(pszSubject) * sizeof(WCHAR)) : 0;
  60. cbTotal += sizeof(cbTotal);
  61. // Iterate through the argument triplets
  62. for (long i = 0; i < cArgTriplets; ++i)
  63. {
  64. // Get the argument name
  65. LPCSTR pszArgName = va_arg(argptr, LPCSTR);
  66. assert(!IsBadStringPtrA(pszArgName, UINT(-1)));
  67. // Get the argument name length
  68. UINT cchName = strlen(pszArgName);
  69. cbTotal += sizeof(cchName) + cchName;
  70. // Get the argument variant type and value
  71. VARTYPE vt = va_arg(argptr, VARTYPE);
  72. cbTotal += sizeof(vt);
  73. switch (vt)
  74. {
  75. VT_SIZE(VT_BOOL , VARIANT_BOOL)
  76. VT_SIZE(VT_I1 , CHAR)
  77. VT_SIZE(VT_I2 , SHORT)
  78. VT_SIZE(VT_I4 , LONG)
  79. VT_SIZE(VT_UI1 , BYTE)
  80. VT_SIZE(VT_UI2 , USHORT)
  81. VT_SIZE(VT_ERROR , SCODE)
  82. case VT_R4:
  83. {
  84. // (...) pushes a float argument onto the stack as a double
  85. va_arg(argptr, DOUBLE);
  86. cbTotal += sizeof(float);
  87. break;
  88. }
  89. VT_SIZE(VT_R8 , DOUBLE)
  90. VT_SIZE(VT_DECIMAL, DECIMAL)
  91. VT_SIZE(VT_CY , CY)
  92. VT_SIZE(VT_DATE , DATE)
  93. case VT_BSTR:
  94. {
  95. BSTR bstr = va_arg(argptr, BSTR);
  96. UINT cb = bstr ? SysStringByteLen(bstr) : 0;
  97. cbTotal += sizeof(cb) + cb;
  98. break;
  99. }
  100. case VT_UNKNOWN:
  101. case VT_DISPATCH:
  102. {
  103. // Object *must* support IPersistStream(Init)
  104. IUnknown* punk = va_arg(argptr, IUnknown*);
  105. IPersistStreamPtr sppstm(punk);
  106. if (punk && NULL == sppstm)
  107. ZSucceeded(punk->QueryInterface(IID_IPersistStreamInit,
  108. (void**)&sppstm));
  109. // Create a Null stream for calculating persisent size
  110. TCNullStreamImpl stm;
  111. // 'Persist' the object to the stream to determine its size
  112. OleSaveToStream(sppstm, &stm);
  113. // Get the object's persistent size
  114. ULARGE_INTEGER cbSize;
  115. LARGE_INTEGER dlibMove = {0, 0};
  116. ZSucceeded(stm.Seek(dlibMove, STREAM_SEEK_CUR, &cbSize));
  117. assert(0 == cbSize.HighPart);
  118. cbTotal += sizeof(cbSize.LowPart) + cbSize.LowPart;
  119. break;
  120. }
  121. case VT_VARIANT:
  122. // TODO: Could recurse here to support VT_VARIANT
  123. ZError("Unsupported Variant Type");
  124. break;
  125. case VT_LPSTR:
  126. {
  127. LPCSTR psz = va_arg(argptr, LPCSTR);
  128. UINT cch = psz ? strlen(psz) : 0;
  129. cbTotal += sizeof(cch) + cch;
  130. break;
  131. }
  132. case VT_LPWSTR:
  133. {
  134. LPCWSTR psz = va_arg(argptr, LPCWSTR);
  135. UINT cch = psz ? wcslen(psz) : 0;
  136. cbTotal += sizeof(cch) + cch * sizeof(WCHAR);
  137. break;
  138. }
  139. default:
  140. ZError("Unsupported Variant Type");
  141. }
  142. }
  143. // Return the total size
  144. return cbTotal;
  145. }
  146. /////////////////////////////////////////////////////////////////////////////
  147. // Useful macros for implementing data copying
  148. //
  149. #define COPY_DATA(pData, cb) \
  150. { \
  151. int _cb = (cb); \
  152. if (_cb) \
  153. { \
  154. assert(cbData >= _cb); \
  155. CopyMemory(pbData, pData, _cb); \
  156. cbData -= _cb; \
  157. pbData += _cb; \
  158. } \
  159. }
  160. #define COPY_VAR(var) \
  161. COPY_DATA(&var, sizeof(var))
  162. #define VT_COPY(vt, type) \
  163. case vt: \
  164. { \
  165. type var = va_arg(argptr, type); \
  166. COPY_VAR(var); \
  167. break; \
  168. }
  169. /////////////////////////////////////////////////////////////////////////////
  170. //
  171. void CAGCEventData::CopyVariableData(LPCSTR pszContext, LPCOLESTR pszSubject,
  172. long cArgTriplets, va_list argptr)
  173. {
  174. // Get initial pointers and byte count
  175. UINT cbData = m_cbData - sizeof(XData);
  176. BYTE* pbData = m_pbData + sizeof(XData);
  177. // Start with the specified context string, if any
  178. UINT cb = pszContext ? strlen(pszContext) : 0;
  179. COPY_VAR(cb);
  180. COPY_DATA(pszContext, cb);
  181. // Next is the specified subject string, if any
  182. cb = pszSubject ? (wcslen(pszSubject) * sizeof(WCHAR)) : 0;
  183. COPY_VAR(cb);
  184. COPY_DATA(pszSubject, cb);
  185. // Iterate through the argument triplets
  186. for (long i = 0; i < cArgTriplets; ++i)
  187. {
  188. // Get the argument name
  189. LPCSTR pszArgName = va_arg(argptr, LPCSTR);
  190. cb = strlen(pszArgName);
  191. COPY_VAR(cb);
  192. COPY_DATA(pszArgName, cb);
  193. // Get the argument variant type and value
  194. VARTYPE vt = va_arg(argptr, VARTYPE);
  195. COPY_VAR(vt);
  196. switch (vt)
  197. {
  198. VT_COPY(VT_BOOL , VARIANT_BOOL)
  199. VT_COPY(VT_I1 , CHAR)
  200. VT_COPY(VT_I2 , SHORT)
  201. VT_COPY(VT_I4 , LONG)
  202. VT_COPY(VT_UI1 , BYTE)
  203. VT_COPY(VT_UI2 , USHORT)
  204. VT_COPY(VT_ERROR , SCODE)
  205. case VT_R4:
  206. {
  207. // (...) pushes a float argument onto the stack as a double
  208. float var = va_arg(argptr, DOUBLE);
  209. COPY_VAR(var);
  210. break;
  211. }
  212. VT_COPY(VT_R8 , DOUBLE)
  213. VT_COPY(VT_DECIMAL, DECIMAL)
  214. VT_COPY(VT_CY , CY)
  215. VT_COPY(VT_DATE , DATE)
  216. case VT_BSTR:
  217. {
  218. BSTR bstr = va_arg(argptr, BSTR);
  219. cb = bstr ? SysStringByteLen(bstr) : 0;
  220. COPY_VAR(cb);
  221. COPY_DATA(bstr, cb);
  222. break;
  223. }
  224. case VT_UNKNOWN:
  225. case VT_DISPATCH:
  226. {
  227. // Object *must* support IPersistStream(Init)
  228. IUnknown* punk = va_arg(argptr, IUnknown*);
  229. IPersistStreamPtr sppstm(punk);
  230. if (punk && NULL == sppstm)
  231. ZSucceeded(punk->QueryInterface(IID_IPersistStreamInit,
  232. (void**)&sppstm));
  233. // Create a simple stream on the memory block
  234. TCSimpleStream stm;
  235. stm.Init(cbData - sizeof(ULONG), pbData + sizeof(ULONG));
  236. // Persist the object to the memory block stream
  237. OleSaveToStream(sppstm, &stm);
  238. // Get the object's persistent size
  239. ULARGE_INTEGER cbSize;
  240. LARGE_INTEGER dlibMove = {0, 0};
  241. ZSucceeded(stm.Seek(dlibMove, STREAM_SEEK_CUR, &cbSize));
  242. assert(0 == cbSize.HighPart);
  243. // Save the size of the persistent object
  244. COPY_VAR(cbSize.LowPart);
  245. // Just skip past bytes already written as stream
  246. cbData -= cbSize.LowPart;
  247. pbData += cbSize.LowPart;
  248. break;
  249. }
  250. case VT_VARIANT:
  251. // TODO: Could recurse here to support VT_VARIANT
  252. ZError("Unsupported Variant Type");
  253. break;
  254. case VT_LPSTR:
  255. {
  256. LPCSTR psz = va_arg(argptr, LPCSTR);
  257. cb = psz ? strlen(psz) : 0;
  258. COPY_VAR(cb);
  259. COPY_DATA(psz, cb);
  260. break;
  261. }
  262. case VT_LPWSTR:
  263. {
  264. LPCWSTR psz = va_arg(argptr, LPCWSTR);
  265. cb = psz ? (wcslen(psz) * sizeof(WCHAR)) : 0;
  266. COPY_VAR(cb);
  267. COPY_DATA(psz, cb);
  268. break;
  269. }
  270. default:
  271. ZError("Unsupported Variant Type");
  272. }
  273. }
  274. }
  275. /////////////////////////////////////////////////////////////////////////////
  276. // Useful macro for implementing VARIANT creations of simple variant types.
  277. //
  278. #define VT_CREATE(_vt, type) \
  279. case VT##_vt: \
  280. V##_vt(&var) = *reinterpret_cast<type*>(pbData); \
  281. cbData = sizeof(type); \
  282. break;
  283. /////////////////////////////////////////////////////////////////////////////
  284. //
  285. // Return Value: The number of bytes consumed from the input buffer. The
  286. // caller should increment its pointer by this many bytes.
  287. //
  288. UINT CAGCEventData::CreateVARIANTFromData(BYTE* pbData, CComVariant& var)
  289. {
  290. // Clear the specified VARIANT
  291. ZSucceeded(var.Clear());
  292. // Get the VARTYPE from the specified buffer
  293. V_VT(&var) = *reinterpret_cast<VARTYPE*>(pbData);
  294. pbData += sizeof(V_VT(&var));
  295. // Switch on the VARTYPE
  296. UINT cbData = 0;
  297. switch (V_VT(&var))
  298. {
  299. VT_CREATE(_BOOL , VARIANT_BOOL)
  300. VT_CREATE(_I1 , CHAR)
  301. VT_CREATE(_I2 , SHORT)
  302. VT_CREATE(_I4 , LONG)
  303. VT_CREATE(_UI1 , BYTE)
  304. VT_CREATE(_UI2 , USHORT)
  305. VT_CREATE(_ERROR , SCODE)
  306. VT_CREATE(_R4 , FLOAT)
  307. VT_CREATE(_R8 , DOUBLE)
  308. VT_CREATE(_DECIMAL, DECIMAL)
  309. VT_CREATE(_CY , CY)
  310. VT_CREATE(_DATE , DATE)
  311. case VT_BSTR:
  312. cbData = CreateBSTRFromData(pbData, &V_BSTR(&var));
  313. break;
  314. case VT_UNKNOWN:
  315. cbData = CreateObjectFromData(pbData, IID_IUnknown,
  316. (void**)&V_UNKNOWN(&var));
  317. break;
  318. case VT_DISPATCH:
  319. cbData = CreateObjectFromData(pbData, IID_IDispatch,
  320. (void**)&V_DISPATCH(&var));
  321. break;
  322. case VT_VARIANT:
  323. // TODO: Could recurse here to support VT_VARIANT
  324. ZError("Unsupported Variant Type");
  325. break;
  326. case VT_LPSTR:
  327. cbData = CreateBSTRFromData_LPSTR(pbData, &V_BSTR(&var));
  328. V_VT(&var) = VT_BSTR;
  329. break;
  330. case VT_LPWSTR:
  331. cbData = CreateBSTRFromData_LPWSTR(pbData, &V_BSTR(&var));
  332. V_VT(&var) = VT_BSTR;
  333. break;
  334. default:
  335. ZError("Unsupported Variant Type");
  336. }
  337. // Return the number of bytes consumed from the buffer
  338. return sizeof(V_VT(&var)) + cbData;
  339. }
  340. /////////////////////////////////////////////////////////////////////////////
  341. //
  342. // Return Value: The number of bytes consumed from the input buffer. The
  343. // caller should increment its pointer by this many bytes.
  344. //
  345. UINT CAGCEventData::CreateBSTRFromData(BYTE* pbData, BSTR* pbstr)
  346. {
  347. // Get the size of the following persistence block
  348. UINT cbData = *reinterpret_cast<UINT*>(pbData);
  349. pbData += sizeof(cbData);
  350. // Create the BSTR from the persistence block
  351. *pbstr = SysAllocStringByteLen(reinterpret_cast<LPCSTR>(pbData), cbData);
  352. assert(*pbstr);
  353. // Return the number of bytes consumed
  354. return sizeof(cbData) + cbData;
  355. }
  356. /////////////////////////////////////////////////////////////////////////////
  357. //
  358. // Return Value: The number of bytes consumed from the input buffer. The
  359. // caller should increment its pointer by this many bytes.
  360. //
  361. UINT CAGCEventData::CreateBSTRFromData_LPSTR(BYTE* pbData, BSTR* pbstr)
  362. {
  363. // Get the size of the following persistence block
  364. UINT cch = *reinterpret_cast<UINT*>(pbData);
  365. pbData += sizeof(cch);
  366. // Convert the ANSI persistence block to UNICODE
  367. LPOLESTR pwsz = (LPOLESTR)_alloca((cch + 1) * sizeof(OLECHAR));
  368. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
  369. reinterpret_cast<LPCSTR>(pbData), cch, pwsz, cch + 1);
  370. // Create the BSTR from the converted persistence block
  371. *pbstr = SysAllocStringLen(pwsz, cch);
  372. assert(*pbstr);
  373. // Return the number of bytes consumed
  374. return sizeof(cch) + cch;
  375. }
  376. /////////////////////////////////////////////////////////////////////////////
  377. //
  378. // Return Value: The number of bytes consumed from the input buffer. The
  379. // caller should increment its pointer by this many bytes.
  380. //
  381. UINT CAGCEventData::CreateBSTRFromData_LPWSTR(BYTE* pbData, BSTR* pbstr)
  382. {
  383. // Get the size of the following persistence block
  384. UINT cb = *reinterpret_cast<UINT*>(pbData);
  385. pbData += sizeof(cb);
  386. // Create the BSTR from the persistence block
  387. UINT cch = cb / sizeof(OLECHAR);
  388. *pbstr = SysAllocStringLen(reinterpret_cast<LPOLESTR>(pbData), cch);
  389. assert(*pbstr);
  390. // Return the number of bytes consumed
  391. return sizeof(cb) + cb;
  392. }
  393. /////////////////////////////////////////////////////////////////////////////
  394. //
  395. // Return Value: The number of bytes consumed from the input buffer. The
  396. // caller should increment its pointer by this many bytes.
  397. //
  398. UINT CAGCEventData::CreateObjectFromData(BYTE* pbData, REFIID riid,
  399. void** ppvUnk)
  400. {
  401. // Get the size of the following persistence block
  402. UINT cbData = *reinterpret_cast<UINT*>(pbData);
  403. pbData += sizeof(cbData);
  404. // Create a simple stream on the persistence block
  405. TCSimpleStream stm;
  406. stm.Init(cbData, pbData);
  407. // Create the object from the data
  408. ZSucceeded(OleLoadFromStream(&stm, riid, ppvUnk));
  409. // Return the number of bytes consumed
  410. return sizeof(cbData) + cbData;
  411. }