home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / tutsamp / stoclien / papfile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-05  |  8.6 KB  |  261 lines

  1. /*+==========================================================================
  2.   File:      PAPFILE.CPP
  3.  
  4.   Summary:   Implementation file for the CPapFile C++ class. A CPapFile is
  5.              a C++ object that encapsulates load and save operations on an
  6.              COM structured storage compound file.  CPapFile knows about
  7.              an underlying server-based COM object, COPaper, that manages
  8.              the actual paper drawing data and uses an IStorage provided
  9.              to store retrieve that paper data.
  10.  
  11.              For a comprehensive tutorial code tour of PapFile's contents
  12.              and offerings see the tutorial STOCLIEN.HTM file. For more
  13.              specific technical details on the internal workings see the
  14.              comments dispersed throughout the PAPFILE source code.
  15.  
  16.   Classes:   CPapFile.
  17.  
  18.   Functions: .
  19.  
  20.   Origin:    6-13-96: atrent - Created for STOCLIEN Code Sample.
  21.  
  22. ----------------------------------------------------------------------------
  23.   This file is part of the Microsoft COM Tutorial Code Samples.
  24.  
  25.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  26.  
  27.   This source code is intended only as a supplement to Microsoft
  28.   Development Tools and/or on-line documentation.  See these other
  29.   materials for detailed information regarding Microsoft code samples.
  30.  
  31.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  32.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  33.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  34.   PARTICULAR PURPOSE.
  35. ==========================================================================+*/
  36.  
  37. /*--------------------------------------------------------------------------
  38.   We include WINDOWS.H for all Win32 applications.
  39.   We include OLE2.H because we will be calling the COM/OLE Libraries.
  40.   We include APPUTIL.H because we will be building this application using
  41.     the convenient Virtual Window and Dialog classes and other
  42.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  43.   We include IPAPER.H and PAPGUIDS.H for the common paper-related Interface
  44.     class, GUID, and CLSID specifications.
  45.   We include PAPFILE.H because it has the C++ class declaration used
  46.     for CPapFile.
  47. ---------------------------------------------------------------------------*/
  48. #include <windows.h>
  49. #include <ole2.h>
  50. #include <apputil.h>
  51. #include <ipaper.h>
  52. #include <papguids.h>
  53. #include "papfile.h"
  54.  
  55.  
  56. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  57.   Method:   CPapFile::CPapFile
  58.  
  59.   Summary:  CPapFile Constructor.
  60.  
  61.   Args:     .
  62.  
  63.   Modifies: .
  64.  
  65.   Returns:  .
  66. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  67. CPapFile::CPapFile()
  68. {
  69.   m_szCurFileName[0] = 0;
  70.   m_pIPaper = NULL;
  71.   m_pIStorage = NULL;
  72. }
  73.  
  74.  
  75. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  76.   Method:   CPapFile::~CPapFile
  77.  
  78.   Summary:  CPapFile Destructor.
  79.  
  80.   Args:     .
  81.  
  82.   Modifies: .
  83.  
  84.   Returns:  .
  85. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  86. CPapFile::~CPapFile()
  87. {
  88.   RELEASE_INTERFACE(m_pIPaper);
  89. }
  90.  
  91.  
  92. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  93.   Method:   CPapFile::Init
  94.  
  95.   Summary:  Initializes the CPapFile C++ object.
  96.  
  97.   Args:     TCHAR* pszAppFileName
  98.               Name/Path of the app's default compound file to open.
  99.             IPaper* pIPaper
  100.               Interface pointer for a COPaper COM object that manages
  101.               a collection of Ink Data in memory and has facilities
  102.               for Loading and Saving that data using an IStorage to
  103.               a client-provided compound file.
  104.  
  105.   Modifies: ...
  106.  
  107.   Returns:  HRESULT
  108.               Standard result code. NOERROR for success.
  109. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  110. HRESULT CPapFile::Init(
  111.                     TCHAR* pszAppFileName,
  112.                     IPaper* pIPaper)
  113. {
  114.   HRESULT hr = E_FAIL;
  115.  
  116.   if (NULL != pIPaper)
  117.   {
  118.     // Keep CPapFile copy of pIPaper. Thus AddRef it. Released in Destructor.
  119.     m_pIPaper = pIPaper;
  120.     m_pIPaper->AddRef();
  121.  
  122.     if (NULL != pszAppFileName)
  123.     {
  124.       // Set the default current file name.
  125.       lstrcpy(m_szCurFileName, pszAppFileName);
  126.  
  127.       hr = NOERROR;
  128.     }
  129.   }
  130.  
  131.   return (hr);
  132. }
  133.  
  134.  
  135. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  136.   Method:   CPapFile::Load
  137.  
  138.   Summary:  Load paper data using the specified compound file name.
  139.  
  140.   Args:     SHORT nLockKey
  141.               The lock key previously obtained in a IPaper::Lock call.
  142.             TCHAR* pszFileName
  143.               Name/Path of the compound file to open. If NULL then use
  144.               CPapFile's m_szCurFileName as the compound file to load.
  145.  
  146.   Modifies: ...
  147.  
  148.   Returns:  HRESULT
  149.               Standard result code. NOERROR for success.
  150. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  151. HRESULT CPapFile::Load(
  152.                     SHORT nLockKey,
  153.                     TCHAR* pszFileName)
  154. {
  155.   HRESULT hr = E_FAIL;
  156.   BOOL bNewFile = (NULL != pszFileName);
  157.   TCHAR* pszFile;
  158.  
  159.   // If NULL file name passed then use current file name.
  160.   pszFile = bNewFile ? pszFileName : m_szCurFileName;
  161.  
  162.   // First check if the file is out there using APPUTIL function.
  163.   if (FileExist(pszFile))
  164.   {
  165.     // Use COM service to next check if the file is actually a valid
  166.     // compound file.
  167.     hr = StgIsStorageFile(pszFile);
  168.     if (SUCCEEDED(hr))
  169.     {
  170.       // We're go. Use COM service to open the compound file and
  171.       // obtain a IStorage interface.
  172.       hr = StgOpenStorage(
  173.              pszFile,
  174.              NULL,
  175.              STGM_DIRECT | STGM_READ | STGM_SHARE_EXCLUSIVE,
  176.              NULL,
  177.              0,
  178.              &m_pIStorage);
  179.       if (SUCCEEDED(hr))
  180.       {
  181.         // We have an IStorage. Now ask the COPaper object on the server
  182.         // side to load into itself the file's paper data using the
  183.         // IStorage for our client-provided compound file.
  184.         hr = m_pIPaper->Load(nLockKey, m_pIStorage);
  185.         if (SUCCEEDED(hr))
  186.         {
  187.           // The paper data was loaded and we have a current compound
  188.           // file name. Copy it for later use in a saves and loads.
  189.           if (bNewFile)
  190.             lstrcpy(m_szCurFileName, pszFileName);
  191.         }
  192.  
  193.         // We are done with the Storage for now. We don't hold the file
  194.         // open. We re-obtain the IStorage again later (and thus re-open
  195.         // the compound file) when we need it for another save or load.
  196.         RELEASE_INTERFACE(m_pIStorage);
  197.       }
  198.     }
  199.   }
  200.  
  201.   return (hr);
  202. }
  203.  
  204.  
  205. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  206.   Method:   CPapFile::Save
  207.  
  208.   Summary:  Save current paper data using the specified compound file name.
  209.  
  210.   Args:     SHORT nLockKey
  211.               The lock key previously obtained in a IPaper::Lock call.
  212.             TCHAR* pszFileName
  213.               Name/Path of the compound file to open. If NULL then use
  214.               CPapFile's m_szCurFileName as the compound file to save.
  215.  
  216.   Modifies: ...
  217.  
  218.   Returns:  HRESULT
  219.               Standard result code. NOERROR for success.
  220. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  221. HRESULT CPapFile::Save(
  222.                     SHORT nLockKey,
  223.                     TCHAR* pszFileName)
  224. {
  225.   HRESULT hr = E_FAIL;
  226.   BOOL bNewFile = (NULL != pszFileName);
  227.   TCHAR* pszFile;
  228.  
  229.   // If NULL file name passed then use current file name.
  230.   pszFile = bNewFile ? pszFileName : m_szCurFileName;
  231.  
  232.   // Use COM service to re-open (or newly create) the compound file.
  233.   hr = StgCreateDocfile(
  234.          pszFile,
  235.          STGM_CREATE | STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  236.          0,
  237.          &m_pIStorage);
  238.   if (SUCCEEDED(hr))
  239.   {
  240.     // We've got the IStorage and the compound file is open.
  241.     // Now tell the COPaper object on the server side to save its
  242.     // paper data into the compound file using the IStorage of our
  243.     // client-provided compound file.
  244.     hr = m_pIPaper->Save(nLockKey, m_pIStorage);
  245.     if (SUCCEEDED(hr))
  246.     {
  247.       // The paper data was saved and we have a new current compound
  248.       // file name. Copy it for later use in a saves and loads.
  249.       if (bNewFile)
  250.         lstrcpy(m_szCurFileName, pszFileName);
  251.     }
  252.  
  253.     // We are done with the Storage for now. We don't hold the file
  254.     // open. We re-obtain the IStorage again later (and thus re-open
  255.     // the compound file) when we need it for another save or load.
  256.     RELEASE_INTERFACE(m_pIStorage);
  257.   }
  258.  
  259.   return (hr);
  260. }
  261.