home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 10.ddi / PROGTALK.ZIP / DDECLIEN.CPP next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.5 KB  |  109 lines

  1. #include "ddeclien.h"
  2.  
  3. /*
  4.     Initiate a DDE conversation. Bring up a
  5.     message box if the server does not respond to the
  6.     WM_DDE_INITIATE message.
  7. */
  8.  
  9. void TDDEClient::InitiateDDE( char* AppName, char* TopicName )
  10. {
  11.     ATOM AppAtom, TopicAtom;
  12.  
  13.     PendingMessage = WM_DDE_INITIATE;
  14.     AppAtom = GlobalAddAtom( AppName );
  15.     TopicAtom = GlobalAddAtom( TopicName );
  16.     SendMessage( HWND( 0xFFFF ), WM_DDE_INITIATE, (WPARAM)HWindow,
  17.         MAKELONG( AppAtom, TopicAtom ) );
  18.     GlobalDeleteAtom( AppAtom );
  19.     GlobalDeleteAtom( TopicAtom );
  20.     PendingMessage = 0;
  21.     if ( ServerWindow == 0 )
  22.         MessageBox( HWindow,
  23.             "Cannot establish DDE link to DDE Server.",
  24.             "Error",
  25.             MB_ICONEXCLAMATION | MB_OK );
  26. }
  27.  
  28. /*
  29.     Terminate the DDE conversation. Send the WM_DDE_TERMINATE message
  30.     only if the server window still exists.
  31. */
  32.  
  33. void TDDEClient::TerminateDDE( void )
  34. {
  35.     HWND W = ServerWindow;
  36.     ServerWindow = 0;
  37.     if ( IsWindow( W ) )
  38.         PostMessage( W, WM_DDE_TERMINATE, (WPARAM)HWindow, 0 );
  39. }
  40.  
  41. /*
  42.     WM_DDE_ACK message response method. If the current DDE message
  43.     is a WM_DDE_INITIATE, store off the window handle of the window
  44.     that responded. If more than one window responds, terminate all
  45.     conversations but the first. If the current DDE message is a
  46.     WM_DDE_EXECUTE, free the command string memory block, and focus our
  47.     window.
  48. */
  49.  
  50. void TDDEClient::WMDDEAck( TMessage& Msg )
  51. {
  52.     switch( PendingMessage )
  53.     {
  54.         case WM_DDE_INITIATE:
  55.  
  56.             if ( ServerWindow == 0 )
  57.                 ServerWindow = (HWND)Msg.WParam;
  58.             else
  59.                 PostMessage( (HWND)Msg.WParam,
  60.                     WM_DDE_TERMINATE,
  61.                     (WPARAM)HWindow,
  62.                     0L );
  63.             GlobalDeleteAtom( Msg.LP.Lo );
  64.             GlobalDeleteAtom( Msg.LP.Hi );
  65.             break;
  66.  
  67.         case WM_DDE_EXECUTE:
  68.  
  69.             GlobalFree( (HGLOBAL)Msg.LP.Hi );
  70.             PendingMessage = 0;
  71.             SetFocus( HWindow );
  72.             break;
  73.     }
  74. }
  75.  
  76. /*
  77.     WM_DDE_TERMINATE message response method. If the window signaling
  78.     termination is our server window, terminate
  79.     the DDE conversation. Otherwise ignore the WM_DDE_TERMINATE.
  80. */
  81.  
  82. void TDDEClient::WMDDETerminate( TMessage& Msg )
  83. {
  84.     if ( (HWND)Msg.WParam == ServerWindow )
  85.         TerminateDDE();
  86. }
  87.  
  88. /*
  89.     WM_CLOSE message response method. Terminate the DDE link and
  90.     call the inherited WMClose.
  91. */
  92.  
  93. void TDDEClient::WMClose( TMessage& Msg )
  94. {
  95.     TerminateDDE();
  96.  
  97.     DWORD dwTime = GetCurrentTime();
  98.     const DWORD DDE_TIMEOUT = 3000;
  99.     MSG msg;
  100.     while( GetCurrentTime() - dwTime < DDE_TIMEOUT )
  101.     {
  102.         if( PeekMessage( &msg, HWindow, WM_DDE_TERMINATE,
  103.                 WM_DDE_TERMINATE, PM_REMOVE ) )
  104.             break;
  105.     }
  106.  
  107.     TWindowsObject::WMClose( Msg );
  108. }
  109.