Source code of Windows XP (NT5)
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.

654 lines
15 KiB

  1. //**********************************************************************
  2. // File name: ips.cpp
  3. //
  4. // Implementation file for the CSimpSvrApp Class
  5. //
  6. // Functions:
  7. //
  8. // See ips.h for a list of member functions.
  9. //
  10. // Copyright (c) 1993 Microsoft Corporation. All rights reserved.
  11. //**********************************************************************
  12. #include "pre.h"
  13. #include "obj.h"
  14. #include "ips.h"
  15. #include "app.h"
  16. #include "doc.h"
  17. DEFINE_GUID(GUID_SIMPLE, 0xBCF6D4A0, 0xBE8C, 0x1068, 0xB6, 0xD4, 0x00, 0xDD, 0x01, 0x0C, 0x05, 0x09);
  18. //**********************************************************************
  19. //
  20. // CPersistStorage::QueryInterface
  21. //
  22. // Purpose:
  23. // Used for interface negotiation
  24. //
  25. // Parameters:
  26. //
  27. // REFIID riid - Interface being queried for.
  28. //
  29. // LPVOID FAR *ppvObj - Out pointer for the interface.
  30. //
  31. // Return Value:
  32. //
  33. // S_OK - Success
  34. // E_NOINTERFACE - Failure
  35. //
  36. // Function Calls:
  37. // Function Location
  38. //
  39. // CSimpSvrObj::QueryInterface OBJ.CPP
  40. //
  41. //
  42. //********************************************************************
  43. STDMETHODIMP CPersistStorage::QueryInterface ( REFIID riid, LPVOID FAR* ppvObj)
  44. {
  45. TestDebugOut(TEXT("In CPersistStorage::QueryInterface\r\n"));
  46. // need to NULL the out parameter
  47. return m_lpObj->QueryInterface(riid, ppvObj);
  48. }
  49. //**********************************************************************
  50. //
  51. // CPersistStorage::AddRef
  52. //
  53. // Purpose:
  54. //
  55. // Increments the reference count on CSimpSvrObj. Since CPersistStorage
  56. // is a nested class of CSimpSvrObj, we don't need an extra reference
  57. // count for CPersistStorage. We can safely use the reference count of
  58. // CSimpSvrObj.
  59. //
  60. //
  61. // Parameters:
  62. //
  63. // None
  64. //
  65. // Return Value:
  66. //
  67. // The new reference count of CSimpSvrObj
  68. //
  69. // Function Calls:
  70. // Function Location
  71. //
  72. // OuputDebugString Windows API
  73. // CSimpSvrObj::AddRef OBJ.CPP
  74. //
  75. //
  76. //********************************************************************
  77. STDMETHODIMP_(ULONG) CPersistStorage::AddRef ()
  78. {
  79. TestDebugOut(TEXT("In CPersistStorage::AddRef\r\n"));
  80. return m_lpObj->AddRef();
  81. }
  82. //**********************************************************************
  83. //
  84. // CPersistStorage::Release
  85. //
  86. // Purpose:
  87. //
  88. // Decrements the reference count on CSimpSvrObj. Since CPersistStorage
  89. // is a nested class of CSimpSvrObj, we don't need an extra reference
  90. // count for CPersistStorage. We can safely use the reference count of
  91. // CSimpSvrObj.
  92. //
  93. // Parameters:
  94. //
  95. // None
  96. //
  97. // Return Value:
  98. //
  99. // The new reference count of CSimpSvrObj
  100. //
  101. // Function Calls:
  102. // Function Location
  103. //
  104. // TestDebugOut Windows API
  105. // CSimpSvrObj::Release OBJ.CPP
  106. //
  107. //
  108. //********************************************************************
  109. STDMETHODIMP_(ULONG) CPersistStorage::Release ()
  110. {
  111. TestDebugOut(TEXT("In CPersistStorage::Release\r\n"));
  112. return m_lpObj->Release();
  113. }
  114. //**********************************************************************
  115. //
  116. // CPersistStorage::InitNew
  117. //
  118. // Purpose:
  119. //
  120. // Used to give a new OLE object a ptr to its storage.
  121. //
  122. // Parameters:
  123. //
  124. // LPSTORAGE pStg - Pointer to the storage
  125. //
  126. // Return Value:
  127. // S_OK
  128. //
  129. // Function Calls:
  130. // Function Location
  131. //
  132. // TestDebugOut Windows API
  133. // IStorage::Release OLE
  134. // IStorage::AddRef OLE
  135. //
  136. //
  137. //********************************************************************
  138. STDMETHODIMP CPersistStorage::InitNew (LPSTORAGE pStg)
  139. {
  140. TestDebugOut(TEXT("In CPersistStorage::InitNew\r\n"));
  141. // release any streams and storages that may be open
  142. ReleaseStreamsAndStorage();
  143. m_lpObj->m_lpStorage = pStg;
  144. // AddRef the new Storage
  145. if (m_lpObj->m_lpStorage)
  146. m_lpObj->m_lpStorage->AddRef();
  147. CreateStreams(m_lpObj->m_lpStorage);
  148. return ResultFromScode(S_OK);
  149. }
  150. //**********************************************************************
  151. //
  152. // CPersistStorage::GetClassID
  153. //
  154. // Purpose:
  155. //
  156. // Returns the CLSID of this object.
  157. //
  158. // Parameters:
  159. //
  160. // LPCLSID lpClassID - Out ptr in which to return the CLSID
  161. //
  162. // Return Value:
  163. //
  164. // S_OK
  165. //
  166. // Function Calls:
  167. // Function Location
  168. //
  169. // TestDebugOut Windows API
  170. //
  171. //
  172. //********************************************************************
  173. STDMETHODIMP CPersistStorage::GetClassID ( LPCLSID lpClassID)
  174. {
  175. TestDebugOut(TEXT("In CPersistStorage::GetClassID\r\n"));
  176. *lpClassID = GUID_SIMPLE;
  177. return ResultFromScode( S_OK );
  178. }
  179. //**********************************************************************
  180. //
  181. // CPersistStorage::Save
  182. //
  183. // Purpose:
  184. //
  185. // Instructs the object to save itself into the storage.
  186. //
  187. // Parameters:
  188. //
  189. // LPSTORAGE pStgSave - Storage in which the object should be saved
  190. //
  191. // BOOL fSameAsLoad - TRUE if pStgSave is the same as the storage
  192. // that the object was originally created with.
  193. //
  194. // Return Value:
  195. //
  196. // S_OK
  197. //
  198. // Function Calls:
  199. // Function Location
  200. //
  201. // TestDebugOut Windows API
  202. // CPersistStorage::InitNew IPS.CPP
  203. // CSimpSvrObj::SaveToStorage OBJ.CPP
  204. //
  205. //
  206. // Comments:
  207. //
  208. // A real app will want better error checking in this method.
  209. //
  210. //********************************************************************
  211. STDMETHODIMP CPersistStorage::Save ( LPSTORAGE pStgSave, BOOL fSameAsLoad)
  212. {
  213. TestDebugOut(TEXT("In CPersistStorage::Save\r\n"));
  214. // save the data
  215. m_lpObj->SaveToStorage (pStgSave, fSameAsLoad);
  216. m_lpObj->m_fSaveWithSameAsLoad = fSameAsLoad;
  217. m_lpObj->m_fNoScribbleMode = TRUE;
  218. return ResultFromScode( S_OK );
  219. }
  220. //**********************************************************************
  221. //
  222. // CPersistStorage::SaveCompleted
  223. //
  224. // Purpose:
  225. //
  226. // Called when the container is finished saving the object
  227. //
  228. // Parameters:
  229. //
  230. // LPSTORAGE pStgNew - ptr to the new storage
  231. //
  232. // Return Value:
  233. //
  234. // S_OK
  235. //
  236. // Function Calls:
  237. // Function Location
  238. //
  239. // TestDebugOut Windows API
  240. //
  241. //
  242. //********************************************************************
  243. STDMETHODIMP CPersistStorage::SaveCompleted ( LPSTORAGE pStgNew)
  244. {
  245. TestDebugOut(TEXT("In CPersistStorage::SaveCompleted\r\n"));
  246. if (pStgNew)
  247. {
  248. ReleaseStreamsAndStorage();
  249. m_lpObj->m_lpStorage = pStgNew;
  250. m_lpObj->m_lpStorage->AddRef();
  251. OpenStreams(pStgNew);
  252. }
  253. /* OLE2NOTE: it is only legal to perform a Save or SaveAs operation
  254. ** on an embedded object. if the document is a file-based document
  255. ** then we can not be changed to a IStorage-base object.
  256. **
  257. ** fSameAsLoad lpStgNew Type of Save Send OnSave
  258. ** ---------------------------------------------------------
  259. ** TRUE NULL SAVE YES
  260. ** TRUE ! NULL SAVE * YES
  261. ** FALSE ! NULL SAVE AS YES
  262. ** FALSE NULL SAVE COPY AS NO
  263. **
  264. ** * this is a strange case that is possible. it is inefficient
  265. ** for the caller; it would be better to pass lpStgNew==NULL for
  266. ** the Save operation.
  267. */
  268. if ( pStgNew || m_lpObj->m_fSaveWithSameAsLoad)
  269. {
  270. if (m_lpObj->m_fNoScribbleMode)
  271. if (
  272. m_lpObj->GetOleAdviseHolder()->SendOnSave()!=S_OK
  273. // normally would clear a
  274. // dirty bit
  275. )
  276. TestDebugOut(TEXT("SendOnSave fails\n"));
  277. m_lpObj->m_fSaveWithSameAsLoad = FALSE;
  278. }
  279. m_lpObj->m_fNoScribbleMode = FALSE;
  280. return ResultFromScode( S_OK );
  281. }
  282. //**********************************************************************
  283. //
  284. // CPersistStorage::Load
  285. //
  286. // Purpose:
  287. //
  288. // Instructs the object to be loaded from storage.
  289. //
  290. // Parameters:
  291. //
  292. // LPSTORAGE pStg - Ptr to the storage in which to be loaded
  293. //
  294. // Return Value:
  295. //
  296. // S_OK
  297. //
  298. // Function Calls:
  299. // Function Location
  300. //
  301. // TestDebugOut Windows API
  302. // CSimpSvrObj::LoadFromStorage OBJ.CPP
  303. //
  304. //
  305. // Comments:
  306. //
  307. // A real app will want better error checking in this method.
  308. //
  309. //********************************************************************
  310. STDMETHODIMP CPersistStorage::Load ( LPSTORAGE pStg)
  311. {
  312. TestDebugOut(TEXT("In CPersistStorage::Load\r\n"));
  313. // remember the storage
  314. if (m_lpObj->m_lpStorage)
  315. {
  316. m_lpObj->m_lpStorage->Release();
  317. m_lpObj->m_lpStorage = NULL;
  318. }
  319. m_lpObj->m_lpStorage = pStg;
  320. m_lpObj->m_lpStorage->AddRef();
  321. OpenStreams(m_lpObj->m_lpStorage);
  322. m_lpObj->LoadFromStorage();
  323. return ResultFromScode( S_OK );
  324. }
  325. //**********************************************************************
  326. //
  327. // CPersistStorage::IsDirty
  328. //
  329. // Purpose:
  330. //
  331. // Returns whether or not the object is dirty w/respect to its
  332. // Storage
  333. //
  334. // Parameters:
  335. //
  336. // None
  337. //
  338. // Return Value:
  339. // S_OK
  340. //
  341. // Function Calls:
  342. // Function Location
  343. //
  344. // TestDebugOut Windows API
  345. //
  346. //
  347. // Comments:
  348. //
  349. // This sample does not implement this function, although a
  350. // real application should.
  351. //
  352. //********************************************************************
  353. STDMETHODIMP CPersistStorage::IsDirty()
  354. {
  355. TestDebugOut(TEXT("In CPersistStorage::IsDirty\r\n"));
  356. return ResultFromScode( S_OK );
  357. }
  358. //**********************************************************************
  359. //
  360. // CPersistStorage::HandsOffStorage
  361. //
  362. // Purpose:
  363. //
  364. // Forces the object to release its handle to its storage.
  365. //
  366. // Parameters:
  367. //
  368. // None
  369. //
  370. // Return Value:
  371. //
  372. // S_OK
  373. //
  374. // Function Calls:
  375. // Function Location
  376. //
  377. // TestDebugOut Windows API
  378. // IStorage::Release OLE
  379. //
  380. //********************************************************************
  381. STDMETHODIMP CPersistStorage::HandsOffStorage ()
  382. {
  383. TestDebugOut(TEXT("In CPersistStorage::HandsOffStorage\r\n"));
  384. ReleaseStreamsAndStorage();
  385. return ResultFromScode( S_OK );
  386. }
  387. //**********************************************************************
  388. //
  389. // CPersistStorage::CreateStreams
  390. //
  391. // Purpose:
  392. //
  393. // Creates the streams that are held open for the object's lifetime.
  394. //
  395. // Parameters:
  396. //
  397. // LPSTORAGE lpStg - Storage in which to create the streams
  398. //
  399. // Return Value:
  400. //
  401. // none
  402. //
  403. // Function Calls:
  404. // Function Location
  405. //
  406. // TestDebugOut Windows API
  407. // IStorage::Release OLE
  408. // IStream::Release OLE
  409. // IStorage::CreateStream OLE
  410. //
  411. //
  412. //********************************************************************
  413. void CPersistStorage::CreateStreams(LPSTORAGE lpStg)
  414. {
  415. if (m_lpObj->m_lpColorStm)
  416. m_lpObj->m_lpColorStm->Release();
  417. if (m_lpObj->m_lpSizeStm)
  418. m_lpObj->m_lpSizeStm->Release();
  419. // create a stream to save the colors
  420. if (
  421. lpStg->CreateStream ( OLESTR("RGB"),
  422. STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE,
  423. 0,
  424. 0,
  425. &m_lpObj->m_lpColorStm)
  426. !=S_OK
  427. )
  428. TestDebugOut(TEXT("CreateStreams fails\n"));
  429. // create a stream to save the size
  430. if (
  431. lpStg->CreateStream ( OLESTR("size"),
  432. STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE,
  433. 0,
  434. 0,
  435. &m_lpObj->m_lpSizeStm)
  436. !=S_OK
  437. )
  438. TestDebugOut(TEXT("CreateStreams fails\n"));
  439. }
  440. //**********************************************************************
  441. //
  442. // CPersistStorage::OpenStreams
  443. //
  444. // Purpose:
  445. //
  446. // Opens the streams in a storage.
  447. //
  448. // Parameters:
  449. //
  450. // LPSTORAGE lpStg - Storage in which to open the streams.
  451. //
  452. // Return Value:
  453. //
  454. // None
  455. //
  456. // Function Calls:
  457. // Function Location
  458. //
  459. // TestDebugOut Windows API
  460. // IStream::Release OLE
  461. // IStorage::OpenStream OLE
  462. //
  463. //
  464. //********************************************************************
  465. void CPersistStorage::OpenStreams(LPSTORAGE lpStg)
  466. {
  467. if (m_lpObj->m_lpColorStm)
  468. m_lpObj->m_lpColorStm->Release();
  469. if (m_lpObj->m_lpSizeStm)
  470. m_lpObj->m_lpSizeStm->Release();
  471. // open the color stream
  472. if (
  473. lpStg->OpenStream ( OLESTR("RGB"),
  474. 0,
  475. STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  476. 0,
  477. &m_lpObj->m_lpColorStm)
  478. !=S_OK
  479. )
  480. TestDebugOut(TEXT("OpenStream fails\n"));
  481. // open the color stream
  482. if (
  483. lpStg->OpenStream ( OLESTR("size"),
  484. 0,
  485. STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  486. 0,
  487. &m_lpObj->m_lpSizeStm)
  488. !=S_OK
  489. )
  490. TestDebugOut(TEXT("OpenStream fails\n"));
  491. }
  492. //**********************************************************************
  493. //
  494. // CPersistStorage::ReleaseStreamsAndStorage
  495. //
  496. // Purpose:
  497. //
  498. // Releases the stream and storage ptrs
  499. //
  500. // Parameters:
  501. //
  502. // None
  503. //
  504. // Return Value:
  505. // None
  506. //
  507. // Function Calls:
  508. // Function Location
  509. //
  510. // TestDebugOut Windows API
  511. // IStream::Release OLE
  512. // IStorage::Release OLE
  513. //
  514. //********************************************************************
  515. void CPersistStorage::ReleaseStreamsAndStorage()
  516. {
  517. if (m_lpObj->m_lpColorStm)
  518. {
  519. m_lpObj->m_lpColorStm->Release();
  520. m_lpObj->m_lpColorStm = NULL;
  521. }
  522. if (m_lpObj->m_lpSizeStm)
  523. {
  524. m_lpObj->m_lpSizeStm->Release();
  525. m_lpObj->m_lpSizeStm = NULL;
  526. }
  527. if (m_lpObj->m_lpStorage)
  528. {
  529. m_lpObj->m_lpStorage->Release();
  530. m_lpObj->m_lpStorage = NULL;
  531. }
  532. }
  533. //**********************************************************************
  534. //
  535. // CPersistStorage::CreateStreams
  536. //
  537. // Purpose:
  538. //
  539. // Creates temporary streams in a storage.
  540. //
  541. // Parameters:
  542. //
  543. // LPSTORAGE lpStg - Pointer to the storage
  544. //
  545. // LPSTREAM FAR* lplpTempColor - Color Stream
  546. //
  547. // LPSTREAM FAR* lplpTempSize - Size Stream
  548. //
  549. // Return Value:
  550. //
  551. // None
  552. //
  553. // Function Calls:
  554. // Function Location
  555. //
  556. // TestDebugOut Windows API
  557. // IStorage::Release OLE
  558. //
  559. //
  560. //********************************************************************
  561. void CPersistStorage::CreateStreams(LPSTORAGE lpStg,
  562. LPSTREAM FAR* lplpTempColor,
  563. LPSTREAM FAR* lplpTempSize)
  564. {
  565. // create a stream to save the colors
  566. if (
  567. lpStg->CreateStream ( OLESTR("RGB"),
  568. STGM_READWRITE | STGM_SHARE_EXCLUSIVE |
  569. STGM_CREATE,
  570. 0,
  571. 0,
  572. lplpTempColor)
  573. !=S_OK
  574. )
  575. TestDebugOut(TEXT("CreateStreams fails\n"));
  576. // create a stream to save the size
  577. if (
  578. lpStg->CreateStream ( OLESTR("size"),
  579. STGM_READWRITE | STGM_SHARE_EXCLUSIVE |
  580. STGM_CREATE,
  581. 0,
  582. 0,
  583. lplpTempSize)
  584. !=S_OK
  585. )
  586. TestDebugOut(TEXT("CreateStreams fails\n"));
  587. }