PRB: Petzold DDE Sample Applications Contain Errors

Last reviewed: March 28, 1997
Article 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: 95
Keywords : kbcode kbprg UsrDde
Version : 3.51 4.0
Platform : NT WINDOWS
Issue type : kbprb


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.