5.7.5 Deleting Objects

To permanently delete an object from a document, the client application should call the OleDelete function. OleDelete closes the specified object, if necessary, before deleting it.

The following code example shows how CLIDEMO.EXE deletes an object:

From CLIDEMO.C:

.

.

.

case WM_DELETE: // delete object

pItem = (APPITEMPTR) wParam;

WaitForObject(pItem); // wait for async operation to

// finish

ObjDelete(pItem,DELETE);

if (lParam)

cOleWait--;

break;

/*

* ObjDelete()

*

* Delete an OLE object. For this application, all OLE objects

* are associated with a child window; therefore the window must be

* destroyed.

*

* NOTE: There is one case when we call OleRelease and the other when

* we call OleDelete. We call OleRelease when we unregister

* a document and OleDelete when removing an object from a document.

*/

void FAR ObjDelete // ENTRY:

(APPITEMPTR pItem, // pointer to application item

BOOL fDelete) // delete or release flag

{ // LOCAL:

if (pItem->lpObjectUndo)

{

Error(OleDelete(pItem->lpObjectUndo));

// wait for asynchronous

// operation

WaitForObject(pItem);

}

if (fDelete ? Error(OleDelete(pItem->lpObject))

: Error(OleRelease(pItem->lpObject)))

{

ErrorMessage(E_FAILED_TO_DELETE_OBJECT);

return; //* ERROR return

}

if (pItem->fVisible)

{

ShowWindow(pItem->hwnd, SW_HIDE);

pItem->fVisible = FALSE;

}

// the operation has to

// complete before the

WaitForObject(pItem); // application structure

FreeAppItem(pItem);

iObjects--;

}