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.

577 lines
13 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved
  3. Module Name:
  4. wsbguid.cpp
  5. Abstract:
  6. This component is an object representations of the GUID standard type. It
  7. is both a persistable and collectable.
  8. Author:
  9. Chuck Bardeen [cbardeen] 29-Oct-1996
  10. Revision History:
  11. --*/
  12. #include "stdafx.h"
  13. #include "wsbguid.h"
  14. HRESULT
  15. CWsbGuid::CompareToGuid(
  16. IN GUID value,
  17. OUT SHORT* pResult
  18. )
  19. /*++
  20. Implements:
  21. IWsbGuid::CompareToGuid
  22. --*/
  23. {
  24. HRESULT hr = E_FAIL;
  25. SHORT result;
  26. WsbTraceIn(OLESTR("CWsbGuid::CompareToGuid"), OLESTR("value = <%ls>"), WsbGuidAsString(value));
  27. // Compare.
  28. result = WsbSign( memcmp(&m_value, &value, sizeof(GUID)) );
  29. // If the aren't equal, then return false.
  30. if (result != 0) {
  31. hr = S_FALSE;
  32. }
  33. else {
  34. hr = S_OK;
  35. }
  36. // If they asked for the relative value back, then return it to them.
  37. if (pResult != NULL) {
  38. *pResult = result;
  39. }
  40. WsbTraceOut(OLESTR("CWsbGuid::CompareToGuid"), OLESTR("hr = <%ls>, result = <%d>"), WsbHrAsString(hr), result);
  41. return(hr);
  42. }
  43. HRESULT
  44. CWsbGuid::CompareToIGuid(
  45. IN IWsbGuid* pGuid,
  46. OUT SHORT* pResult
  47. )
  48. /*++
  49. Implements:
  50. IWsbGuid::CompareToIGuid
  51. --*/
  52. {
  53. HRESULT hr = E_FAIL;
  54. GUID value;
  55. WsbTraceIn(OLESTR("CWsbGuid::CompareToIGuid"), OLESTR(""));
  56. try {
  57. // Did they give us a valid item to compare to?
  58. WsbAssert(0 != pGuid, E_POINTER);
  59. // Get it's value and compare them.
  60. WsbAffirmHr(pGuid->GetGuid(&value));
  61. hr = CompareToGuid(value, pResult);
  62. } WsbCatch(hr);
  63. WsbTraceOut(OLESTR("CWsbGuid::CompareToIGuid"), OLESTR("hr = <%ls>, result = <%ls>"), WsbHrAsString(hr), WsbPtrToShortAsString(pResult));
  64. return(hr);
  65. }
  66. HRESULT
  67. CWsbGuid::CompareTo(
  68. IN IUnknown* pCollectable,
  69. OUT SHORT* pResult
  70. )
  71. /*++
  72. Implements:
  73. IWsbCollectable::CompareTo
  74. --*/
  75. {
  76. HRESULT hr = E_FAIL;
  77. IWsbGuid* pGuid;
  78. WsbTraceIn(OLESTR("CWsbGuid::CompareTo"), OLESTR(""));
  79. try {
  80. // Did they give us a valid item to compare to?
  81. WsbAssert(0 != pCollectable, E_POINTER);
  82. // We need the IWsbGuid interface to get the value of the object.
  83. WsbAffirmHr(pCollectable->QueryInterface(IID_IWsbGuid, (void**) &pGuid));
  84. hr = CompareToIGuid(pGuid, pResult);
  85. } WsbCatch(hr);
  86. WsbTraceOut(OLESTR("CWsbGuid::CompareTo"), OLESTR("hr = <%ls>, result = <%ls>"), WsbHrAsString(hr), WsbPtrToShortAsString(pResult));
  87. return(hr);
  88. }
  89. HRESULT
  90. CWsbGuid::FinalConstruct(
  91. void
  92. )
  93. /*++
  94. Implements:
  95. CComObjectRoot::FinalConstruct
  96. --*/
  97. {
  98. HRESULT hr = S_OK;
  99. try {
  100. WsbAffirmHr(CWsbObject::FinalConstruct());
  101. m_value = GUID_NULL;
  102. } WsbCatch(hr);
  103. return(hr);
  104. }
  105. HRESULT
  106. CWsbGuid::GetGuid(
  107. OUT GUID* pValue
  108. )
  109. /*++
  110. Implements:
  111. IWsbGuid::GetGuid
  112. --*/
  113. {
  114. HRESULT hr = S_OK;
  115. WsbTraceIn(OLESTR("CWsbGuid::GetGuid"), OLESTR(""));
  116. try {
  117. WsbAssert(0 != pValue, E_POINTER);
  118. *pValue = m_value;
  119. } WsbCatch(hr);
  120. WsbTraceOut(OLESTR("CWsbGuid::GetGuid"), OLESTR("hr = <%ls>, value = <%ls>"), WsbHrAsString(hr), WsbGuidAsString(m_value));
  121. return(hr);
  122. }
  123. HRESULT
  124. CWsbGuid::GetClassID(
  125. OUT CLSID* pClsid
  126. )
  127. /*++
  128. Implements:
  129. IPersist::GetClassID
  130. --*/
  131. {
  132. HRESULT hr = S_OK;
  133. WsbTraceIn(OLESTR("CWsbGuid::GetClassID"), OLESTR(""));
  134. try {
  135. WsbAssert(0 != pClsid, E_POINTER);
  136. *pClsid = CLSID_CWsbGuid;
  137. } WsbCatch(hr);
  138. WsbTraceOut(OLESTR("CWsbGuid::GetClassID"), OLESTR("hr = <%ls>, CLSID = <%ls>"), WsbHrAsString(hr), WsbGuidAsString(*pClsid));
  139. return(hr);
  140. }
  141. HRESULT
  142. CWsbGuid::GetSizeMax(
  143. OUT ULARGE_INTEGER* pcbSize
  144. )
  145. /*++
  146. Implements:
  147. IPersistStream::GetSizeMax
  148. --*/
  149. {
  150. HRESULT hr = S_OK;
  151. WsbTraceIn(OLESTR("CWsbGuid::GetSizeMax"), OLESTR(""));
  152. try {
  153. WsbAssert(0 != pcbSize, E_POINTER);
  154. pcbSize->QuadPart = WsbPersistSizeOf(GUID);
  155. } WsbCatch(hr);
  156. WsbTraceOut(OLESTR("CWsbGuid::GetSizeMax"), OLESTR("hr = <%ls>, Size = <%ls>"), WsbHrAsString(hr), WsbPtrToUliAsString(pcbSize));
  157. return(hr);
  158. }
  159. HRESULT
  160. CWsbGuid::Load(
  161. IN IStream* pStream
  162. )
  163. /*++
  164. Implements:
  165. IPersistStream::Load
  166. --*/
  167. {
  168. HRESULT hr = S_OK;
  169. WsbTraceIn(OLESTR("CWsbGuid::Load"), OLESTR(""));
  170. try {
  171. WsbAffirmHr(WsbLoadFromStream(pStream, &m_value));
  172. } WsbCatch(hr);
  173. WsbTraceOut(OLESTR("CWsbGuid::Load"), OLESTR("hr = <%ls>, value = <%ls>"), WsbHrAsString(hr), WsbGuidAsString(m_value));
  174. return(hr);
  175. }
  176. HRESULT
  177. CWsbGuid::Save(
  178. IN IStream* pStream,
  179. IN BOOL clearDirty
  180. )
  181. /*++
  182. Implements:
  183. IPersistStream::Save
  184. --*/
  185. {
  186. HRESULT hr = S_OK;
  187. WsbTraceIn(OLESTR("CWsbGuid::Save"), OLESTR("clearDirty = <%ls>"), WsbBoolAsString(clearDirty));
  188. try {
  189. WsbAffirmHr(WsbSaveToStream(pStream, m_value));
  190. // If we got it saved and we were asked to clear the dirty bit, then
  191. // do so now.
  192. if (clearDirty) {
  193. m_isDirty = FALSE;
  194. }
  195. } WsbCatch(hr);
  196. WsbTraceOut(OLESTR("CWsbGuid::Save"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  197. return(hr);
  198. }
  199. HRESULT
  200. CWsbGuid::SetGuid(
  201. IN GUID value
  202. )
  203. /*++
  204. Implements:
  205. IWsbGuid::SetGuid
  206. --*/
  207. {
  208. WsbTraceIn(OLESTR("CWsbGuid::SetGuid"), OLESTR("value = <%ls>"), WsbGuidAsString(value));
  209. m_isDirty = TRUE;
  210. m_value = value;
  211. WsbTraceOut(OLESTR("CWsbGuid::SetGuid"), OLESTR(""));
  212. return(S_OK);
  213. }
  214. HRESULT
  215. CWsbGuid::Test(
  216. OUT USHORT* passed,
  217. OUT USHORT* failed
  218. )
  219. /*++
  220. Implements:
  221. IWsbTestable::Test
  222. --*/
  223. {
  224. *passed = 0;
  225. *failed = 0;
  226. HRESULT hr = S_OK;
  227. #if !defined(WSB_NO_TEST)
  228. CComPtr<IWsbGuid> pGuid1;
  229. CComPtr<IWsbGuid> pGuid2;
  230. // CComPtr<IPersistFile> pFile1;
  231. // CComPtr<IPersistFile> pFile2;
  232. GUID value;
  233. SHORT result;
  234. WsbTraceIn(OLESTR("CWsbGuid::Test"), OLESTR(""));
  235. try {
  236. // Get the pGuid interface.
  237. hr = S_OK;
  238. try {
  239. WsbAffirmHr(((IUnknown*) (IWsbGuid*) this)->QueryInterface(IID_IWsbGuid, (void**) &pGuid1));
  240. // Set the bool to a value, and see if it is returned.
  241. hr = S_OK;
  242. try {
  243. WsbAffirmHr(pGuid1->SetGuid(CLSID_CWsbGuid));
  244. WsbAffirmHr(pGuid1->GetGuid(&value));
  245. WsbAssert(value == CLSID_CWsbGuid, E_FAIL);
  246. } WsbCatch(hr);
  247. if (hr == S_OK) {
  248. (*passed)++;
  249. } else {
  250. (*failed)++;
  251. }
  252. // Create another instance and test the comparisson methods:
  253. try {
  254. WsbAffirmHr(CoCreateInstance(CLSID_CWsbGuid, NULL, CLSCTX_ALL, IID_IWsbGuid, (void**) &pGuid2));
  255. // Check the default values.
  256. hr = S_OK;
  257. try {
  258. WsbAffirmHr(pGuid2->GetGuid(&value));
  259. WsbAssert(value == GUID_NULL, E_FAIL);
  260. } WsbCatch(hr);
  261. if (hr == S_OK) {
  262. (*passed)++;
  263. } else {
  264. (*failed)++;
  265. }
  266. // IsEqual()
  267. hr = S_OK;
  268. try {
  269. WsbAffirmHr(pGuid1->SetGuid(CLSID_CWsbGuid));
  270. WsbAffirmHr(pGuid2->SetGuid(CLSID_CWsbGuid));
  271. WsbAssert(pGuid1->IsEqual(pGuid2) == S_OK, E_FAIL);
  272. } WsbCatch(hr);
  273. if (hr == S_OK) {
  274. (*passed)++;
  275. } else {
  276. (*failed)++;
  277. }
  278. hr = S_OK;
  279. try {
  280. WsbAffirmHr(pGuid1->SetGuid(CLSID_CWsbGuid));
  281. WsbAffirmHr(pGuid2->SetGuid(IID_IWsbGuid));
  282. WsbAssert(pGuid1->IsEqual(pGuid2) == S_FALSE, E_FAIL);
  283. } WsbCatch(hr);
  284. if (hr == S_OK) {
  285. (*passed)++;
  286. } else {
  287. (*failed)++;
  288. }
  289. // CompareTo()
  290. hr = S_OK;
  291. try {
  292. WsbAffirmHr(pGuid1->SetGuid(CLSID_CWsbGuid));
  293. WsbAffirmHr(pGuid2->SetGuid(CLSID_CWsbGuid));
  294. WsbAssert((pGuid1->CompareTo(pGuid2, &result) == S_OK) && (0 == result), E_FAIL);
  295. } WsbCatch(hr);
  296. if (hr == S_OK) {
  297. (*passed)++;
  298. } else {
  299. (*failed)++;
  300. }
  301. hr = S_OK;
  302. try {
  303. WsbAffirmHr(pGuid1->SetGuid(CLSID_CWsbGuid));
  304. WsbAffirmHr(pGuid2->SetGuid(IID_IWsbGuid));
  305. WsbAssert((pGuid1->CompareTo(pGuid2, &result) == S_FALSE) && (1 == result), E_FAIL);
  306. } WsbCatch(hr);
  307. if (hr == S_OK) {
  308. (*passed)++;
  309. } else {
  310. (*failed)++;
  311. }
  312. hr = S_OK;
  313. try {
  314. WsbAffirmHr(pGuid1->SetGuid(IID_IWsbGuid));
  315. WsbAffirmHr(pGuid2->SetGuid(CLSID_CWsbGuid));
  316. WsbAssert((pGuid1->CompareTo(pGuid2, &result) == S_FALSE) && (-1 == result), E_FAIL);
  317. } WsbCatch(hr);
  318. if (hr == S_OK) {
  319. (*passed)++;
  320. } else {
  321. (*failed)++;
  322. }
  323. #ifdef GUID_PERSIST_FILE
  324. // TODO? Open the file and convert to a stream?
  325. // Try out the persistence stuff.
  326. hr = S_OK;
  327. try {
  328. WsbAffirmHr(pGuid1->QueryInterface(IID_IPersistFile, (void**) &pFile1));
  329. WsbAffirmHr(pGuid2->QueryInterface(IID_IPersistFile, (void**) &pFile2));
  330. // The item should be dirty.
  331. hr = S_OK;
  332. try {
  333. WsbAffirmHr(pGuid2->SetGuid(CLSID_CWsbGuid));
  334. WsbAssert(pFile2->IsDirty() == S_OK, E_FAIL);
  335. } WsbCatch(hr);
  336. if (hr == S_OK) {
  337. (*passed)++;
  338. } else {
  339. (*failed)++;
  340. }
  341. // Save the item, and remember.
  342. hr = S_OK;
  343. try {
  344. WsbAffirmHr(pFile2->Save(OLESTR("c:\\WsbTests\\WsbGuid.tst"), TRUE));
  345. } WsbCatch(hr);
  346. if (hr == S_OK) {
  347. (*passed)++;
  348. } else {
  349. (*failed)++;
  350. }
  351. // It shouldn't be dirty.
  352. hr = S_OK;
  353. try {
  354. WsbAssert(pFile2->IsDirty() == S_FALSE, E_FAIL);
  355. } WsbCatch(hr);
  356. if (hr == S_OK) {
  357. (*passed)++;
  358. } else {
  359. (*failed)++;
  360. }
  361. // Try reading it in to another object.
  362. hr = S_OK;
  363. try {
  364. WsbAffirmHr(pGuid1->SetGuid(IID_IWsbGuid));
  365. WsbAffirmHr(pFile1->Load(OLESTR("c:\\WsbTests\\WsbGuid.tst"), 0));
  366. WsbAssert(pGuid1->CompareToGuid(CLSID_CWsbGuid, NULL) == S_OK, E_FAIL);
  367. } WsbCatch(hr);
  368. if (hr == S_OK) {
  369. (*passed)++;
  370. } else {
  371. (*failed)++;
  372. }
  373. } WsbCatch(hr);
  374. if (hr == S_OK) {
  375. (*passed)++;
  376. } else {
  377. (*failed)++;
  378. }
  379. #endif
  380. } WsbCatch(hr);
  381. if (hr == S_OK) {
  382. (*passed)++;
  383. } else {
  384. (*failed)++;
  385. }
  386. } WsbCatch(hr);
  387. if (hr == S_OK) {
  388. (*passed)++;
  389. } else {
  390. (*failed)++;
  391. }
  392. // Tally up the results
  393. if (*failed) {
  394. hr = S_FALSE;
  395. } else {
  396. hr = S_OK;
  397. }
  398. } WsbCatch(hr);
  399. WsbTraceOut(OLESTR("CWsbGuid::Test"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  400. #endif
  401. return(hr);
  402. }