To close a document, a client application should do the following:
Prompt the user to save changes to the document; if the user chooses to save the document, call the OleSavedClientDoc function.
For each object in the document, call the OleRelease function.
When the client DLL confirms that all objects have been closed, call the OleRevokeClientDoc function.
In the CLIDEMO.EXE program, if the user chooses to close the document, CLIDEMO.EXE first calls SaveAsNeeded, which checks to see if the document has been changed. If the document has been changed, SaveFile (see preceding code example) is called to initiate the saving of the document. After saving, DeregDoc unregisters the document with the client DLL by calling OleRevokeClientDoc.
case WM_CLOSE:
ANY_OBJECT_BUSY;
if (!SaveAsNeeded(szFileName, lhcDoc, lpStream))
break;
DeregDoc(lhcDoc);
DestroyWindow(hwnd);
break;/*
* SaveAsNeeded()
*
* This function will have the file saved if and only
* if the document has been modified. If the fDirty flag has
* been set to TRUE, then the document needs to be saved.
*
* Returns: BOOL - TRUE if document doesn't need saving or if the
* document has been saved successfully.
*/
static BOOL SaveAsNeeded( // ENTRY:
PSTR pFileName, // file to save
LHCLIENTDOC lhcDoc, // OLE doc handle
LPAPPSTREAM lpStream) // pointer to OLE stream vtbl
{ // LOCAL:
char sz[CBMESSAGEMAX]; // work strings
char sz2[CBMESSAGEMAX + CBPATHMAX];
if (Dirty(DOC_QUERY)) // if doc is clean don't bother
{
LoadString(hInst, IDS_MAYBESAVE, sz, CBMESSAGEMAX);
wsprintf(sz2, sz, (LPSTR)pFileName );
switch (MessageBox(hwndFrame, sz2, szAppName, MB_YESNOCANCEL |
MB_ICONQUESTION))
{
case IDCANCEL:
return FALSE; // CANCEL return
case IDYES:
return (SaveFile(pFileName,lhcDoc,lpStream));
default:
break;
}
}
return TRUE; // SUCCESS return
}
/*
* DeregDoc()
*
* This function initiates the removal of all OLE objects from the
* current document and unregisters the document with the OLE
* library.
*/
static void DeregDoc // ENTRY:
(LHCLIENTDOC lhcDoc) // client document handle
{
if (lhcDoc)
{ // release all OLE objects and
ClearAll(lhcDoc,RELEASE); // remove them from the screen
WaitForAllObjects();
if (Error(OleRevokeClientDoc(lhcDoc)))
ErrorMessage(W_FAILED_TO_NOTIFY);
}
} // SUCCESS return