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.

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