Reasons to Block an OLE Server (83021)



The information in this article applies to:

  • Microsoft OLE 1.0

This article was previously published under Q83021

SUMMARY

Windows implements OLE through dynamic data exchange (DDE); the OLE libraries send and post DDE messages to transmit information. Because DDE messages must be processed in a particular order, an OLE server application must not enter a message dispatch loop in an OLESERVER, OLESERVERDOC, or OLEOBJECT method.

MORE INFORMATION

When an application displays a message box or a modal dialog box, Windows enters a message dispatch loop. If such a loop occurs within an OLE method, it could allow the OLE libraries to receive additional DDE messages before the current method completed processing. These messages could cause problems; the OLE libraries might act as if the method in progress (that started the message dispatch loop) had completed its processing, and call another method. If this new method requires information provided by the incomplete method, an error will result.

To avoid this situation, the OLE Server Library provides the OleBlockServer function. When a server application blocks itself by calling this function, the OLE libraries queue all incoming requests to the server. When the server is ready to continue processing, it calls the OleUnblockServer function to unblock itself. A server application generally blocks during a lengthy operation where an interruption to processing will be difficult or impossible to handle. Additionally, a server should block itself any time it displays a dialog box or a message box.

The following code demonstrates blocking an OLE server during the OLESERVER::Create method:
BOOL  fBlocked = FALSE; // Global flag to indicate server blocked.

OLESTATUS FAR PASCAL ServerCreate(...)
{
   // The value of lhServer identifies the server and is provided by a
   // call to the OleRegisterServer function.
   retVal = OleBlockServer(lhServer);

   // Set the flag indicating server blocked.
   fBlocked = TRUE;

   if (retVal == OLE_OK)
      MessageBox(...);
   else
      return(OLE_ERROR_GENERIC);
}

    .
    .   // Other code.
    .

   // Main message loop in WinMain.
   while (TRUE)
   {
      // Loop until no more blocked OLE requests.
      while (fBlocked)
         OleUnblockServer(lhServer, &fBlocked);

      GetMessage(&msg, NULL, 0, 0)

      // WM_QUIT message received, fall out of loop.
      if (msg.message == WM_QUIT)
         break;

      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
				
Please note that OleUnblockServer unblocks only one request at a time. Therefore, it is necessary to call this function repeatedly until all requests have been unblocked.

Modification Type:MajorLast Reviewed:10/27/1999
Keywords:KB83021