Step Seven: Implement the IPersistFile Interface

To implement the IPersistFile interface, the file viewer must implement the following member functions: GetClassID, Load, GetCurFile, IsDirty, Save, and SaveCompleted.

GetClassID

This function returns the document's CLSID.

STDMETHODIMP CFileView::XPersistFile::GetClassID (LPCLSID pClsID)
{
METHOD_PROLOGUE (CFileView, PersistFile);

*pClsID = pThis->m_clsID;
return NOERROR;
}

Load

This function loads the document contained in the given filename. Load only stores the filename; ShowInitialize actually opens the file.

STDMETHODIMP CFileView::XPersistFile::Load (
LPCOLESTR pszFileName, DWORD dwMode)
{
METHOD_PROLOGUE (CFileView, PersistFile);

// No modifications are necessary to this code; it simply
// copies the parameters into the CFileView::m_pszPath and
// m_grfMode members for use in IFileViewer::ShowInitialize
// and IFileViewer::Show later on.

// You should never be called twice.
if (pThis->m_pszPath)
pThis->m_fLoadCalled = FALSE; // handle error case

if (pszFileName == NULL)
return E_INVALIDARG;

// Copy the ANSI filename and the mode to use in opening it.
lstrcpy (pThis->m_pszPath, pszFileName);

// Remember that this function has been called.
pThis->m_fLoadCalled = TRUE;
return NOERROR;
}

GetCurFile

This function returns either the absolute path of the document's currently associated file or the default filename prompt if there is no currently associated file. GetCurFile returns E_UNEXPECTED if the Load function has not yet been called; otherwise, it copies the path and returns NOERROR.

STDMETHODIMP CFileView::XPersistFile::GetCurFile (
LPOLESTR __RPC_FAR *ppszFileName)
{
LPOLESTR psz;
ULONG cb;

METHOD_PROLOGUE (CFileView, PersistFile);

// No modifications are necessary to this code; it simply
// copies the CFileView::m_pszPath string into a piece
// of memory and stores the pointer at *ppszFile.

// Load must be called, of course.
if (pThis->m_fLoadCalled)
return E_UNEXPECTED;

if (ppszFileName == NULL)
return E_INVALIDARG;

cb = (lstrlen (pThis->m_pszPath) + 1) * sizeof (OLECHAR);
psz = (LPOLESTR) malloc (cb);

if (NULL == psz)
return E_OUTOFMEMORY;

return NOERROR;
}

IsDirty

This function checks a document object for changes that might have been made since the object was last saved. IsDirty can simply return S_FALSE because a file viewer does not modify the file.

STDMETHODIMP CFileView::XPersistFile::IsDirty (void)
{
return S_FALSE;
}

Save

The Save function saves a copy of the object to the specified filename. Both the Save and the SaveCompleted member functions should return E_NOTIMPL.

STDMETHODIMP CFileView::XPersistFile::Save (
LPCOLESTR pszFileName, BOOL fRemember)
{
return E_NOTIMPL;
}

SaveCompleted

This function signals that the caller has saved the file with a call to IPersistFile::Save and has finished working with it.

STDMETHODIMP CFileView::XPersistFile::SaveCompleted (
LPCOLESTR pszFileName)
{
return E_NOTIMPL;
}