PRB: Petzold DDE Sample Applications Contain Errors

ID: Q165887


The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API), included with:
    • Microsoft Windows NT, versions 3.51, 4.0
    • Microsoft Windows 95


SYMPTOMS

The sample applications Ddepop1.exe and Showpop1.exe, found in Chapter 17 of "Programming Windows 95" by Charles Petzold, contain errors that prevent them from running properly under 32-bit Windows. When these applications are run together, the client may pop up a message box

Failure on WM_DDE_ADVISE!
and may neglect to establish links on all of the requested items. You may also experience memory leakage.


CAUSE

Specifically, when WM_DDE_ACK messages are pulled off the message queue via calls to PeekMessage(), the author neglects to invoke UnpackDDElParam() prior to using the data referenced by the lParam member of the MSG structure. Depending on the value of the handle returned from PackDDElParam, this may cause a successful ACK to return failure instead. The memory leakage invariably occurs as the calls to GlobalDeleteAtom uniformly fail. This appears to have been a simple oversight when porting the 16-bit samples from the author's previous book.


RESOLUTION

Replace the code beginning at line 575 of DDEPOP1.C with the following:


      if (PeekMessage (&msg, hwndServer,
                       WM_DDE_ACK, WM_DDE_ACK, PM_REMOVE))
      {
         // Must unpack lParam
    UINT uiLow, uiHi;

         UnpackDDElParam (WM_DDE_ACK, msg.lParam, &uiLow, &uiHi) ;
    FreeDDElParam (WM_DDE_ACK, msg.lParam) ;

         //wStatus = LOWORD (msg.lParam) ;
         wStatus = uiLow ;

         DdeAck = *((DDEACK *) &wStatus) ;

         //aItem  = HIWORD (msg.lParam) ;
         aItem  = (ATOM)uiHi ;

    GlobalDeleteAtom (aItem) ;
         break ;
      } 
You will also need to substitute the following corrected code for the code that begins at line 161 of SHOWPOP1.C:

      if (PeekMessage (&msg, hwnd,
                       WM_DDE_ACK, WM_DDE_ACK, PM_REMOVE))
      {
         // Must unpack lParam
    UINT uiLow, uiHi;

    UnpackDDElParam (WM_DDE_ACK, msg.lParam, &uiLow, &uiHi) ;
    FreeDDElParam (WM_DDE_ACK, msg.lParam) ;

         //GlobalDeleteAtom (HIWORD (msg.lParam)) ;
    GlobalDeleteAtom ((ATOM)uiHi) ;

         //wStatus = LOWORD (msg.lParam) ;
         wStatus = uiLow ;

         DdeAck = *((DDEACK *) &wStatus) ;

         if (DdeAck.fAck == FALSE) {
            GlobalFree (hDdeAdvise) ;
         }

         break ;
      } 


REFERENCES

"Programming Windows 95," Charles Petzold, Microsoft Press, 1996.

Additional query words:

Keywords : kbcode kbprg kbDDE kbGrpUser kbUser
Version : winnt:3.51,4.0
Platform : winnt
Issue type : kbprb


Last Reviewed: December 7, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.