home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / ddeml / dmgmon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  4.8 KB  |  181 lines

  1. /****************************** Module Header ******************************\
  2. * Module Name: DMGMON.C
  3. *
  4. * This module contains functions used for DDE monitor control.
  5. *
  6. * Created:  8/2/88    sanfords
  7. *
  8. * Copyright (c) 1988, 1989  Microsoft Corporation
  9. \***************************************************************************/
  10. #include "ddemlp.h"
  11. #define MSGF_DDEPOSTMSG     3
  12.  
  13. #define freeMonStr(psz) MyFreeMem(hheapDmg, (NPBYTE)(USHORT)psz, MAX_MONITORSTR + 1)
  14.  
  15.  
  16. BOOL EXPENTRY DdeSendHookProc(hab, psmh, fInterTask)
  17. HAB hab;
  18. PSMHSTRUCT psmh;
  19. BOOL fInterTask;
  20. {
  21.     PSZ psz;
  22.     PSZ pszSave;
  23.     PSZ pszLast;
  24.  
  25.     UNUSED hab;
  26.     UNUSED fInterTask;
  27.     
  28.     if (psmh->msg == WM_DDE_INITIATE || psmh->msg == WM_DDE_INITIATEACK) {
  29.         if (allocMonStr(&pszSave, &pszLast)) {
  30.             psz = timestamp(pszSave, pszLast);
  31.             psz = lstrcat(psz, " ", pszLast);
  32.             psz = ltoa((ULONG)psmh->mp1, psz, pszLast);
  33.             psz = lstrcat(psz, " -> ", pszLast);
  34.             psz = ltoa((ULONG)psmh->hwnd, psz, pszLast);
  35.             psz = lstrcat(psz, "\n\r", pszLast);
  36.             psz = ddeMsgToPsz(psmh->msg, psz, pszLast);
  37.             psz = pddesToPsz(psmh->msg, (PDDESTRUCT)psmh->mp2, psz, pszLast);
  38.             psz = lstrcat(psz, ")\n\r", pszLast);
  39.             MonitorBroadcast(pszSave);
  40.             freeMonStr(pszSave);
  41.         }
  42.     }
  43.     return(FALSE);
  44. }
  45.  
  46.  
  47.  
  48.  
  49. BOOL EXPENTRY DdePostHookProc(hab, pqmsg, fs)
  50. HAB hab;
  51. PQMSG pqmsg;
  52. USHORT fs;
  53. {
  54.     PSZ psz;
  55.     PSZ pszSave;
  56.     PSZ pszLast;
  57.  
  58.     UNUSED hab;
  59.  
  60.     if (fs && pqmsg->msg >= WM_DDE_FIRST && pqmsg->msg <= WM_DDE_LAST) {
  61.         pszLast = psz + MAX_MONITORSTR;  
  62.         if (allocMonStr(&pszSave, &pszLast)) {
  63.             psz = timestamp(pszSave, pszLast);
  64.             psz = lstrcat(psz, " ", pszLast);
  65.             psz = ltoa((ULONG)pqmsg->mp1, psz, pszLast);
  66.             psz = lstrcat(psz, " -> ", pszLast);
  67.             psz = ltoa((ULONG)pqmsg->hwnd, psz, pszLast);
  68.             psz = lstrcat(psz, "\n\r", pszLast);
  69.             psz = ddeMsgToPsz(pqmsg->msg, psz, pszLast);
  70.             psz = pddesToPsz(pqmsg->msg, (PDDESTRUCT)pqmsg->mp2, psz, pszLast);
  71.             psz = lstrcat(psz, ")\n\r", pszLast);
  72.             MonitorBroadcast(pszSave);
  73.             freeMonStr(pszSave);
  74.         }
  75.     }
  76.     return(FALSE);
  77. }
  78.  
  79. /*
  80.  * This guy sends a UM_MONITOR to all the monitor windows (up to MAX_MONITOR)
  81.  * The cheap restriction is due to needing to not be in the semaphore
  82.  * while the monitor is in control yet needing to keep access to pai in
  83.  * the semaphore.
  84.  */
  85. void MonitorBroadcast(psz)
  86. PSZ psz;
  87. {
  88.     HWND hwnd[MAX_MONITORS];
  89.     PAPPINFO pai;
  90.     register USHORT i = 0;
  91.  
  92.     SemCheckOut();
  93.     SemEnter();
  94.     pai = pAppInfoList;
  95.     while (pai && i < cMonitor && i < MAX_MONITORS) {
  96.         if (pai->hwndMonitor) {
  97.             hwnd[i] = pai->hwndMonitor;
  98.             i++;
  99.         }
  100.         pai = pai->next;
  101.     }
  102.     SemLeave();
  103.     
  104.     for (i = 0; i < cMonitor; i++)
  105.         WinSendMsg(hwnd[i], UM_MONITOR, (MPARAM)psz, 0L);
  106. }
  107.  
  108.  
  109. /*
  110.  * We need to allocate the string buffer so that recursive calls will work.
  111.  * We also need to do this because the DLL DS is shared between all potential
  112.  * monitor processes.
  113.  *
  114.  * This also initializes the psz for us with a null terminator and checks
  115.  * cMonitor for us.  If this fails, no monitor action is done.
  116.  *
  117.  * ppsz will contain a pointer to the begining of the allocated buffer.
  118.  * ppszLast will contain a pointer to the end of the allocated buffer.
  119.  */
  120. BOOL allocMonStr(ppsz, ppszLast)
  121. PSZ far *ppsz;
  122. PSZ far *ppszLast;
  123. {
  124.     SemEnter();
  125.     if (cMonitor == 0 ||
  126.             ((*ppsz = FarAllocMem(hheapDmg, MAX_MONITORSTR + 1)) == NULL)) {
  127.         SemLeave();
  128.         return(FALSE);
  129.     }
  130.     *ppszLast = *ppsz + MAX_MONITORSTR;
  131.     **ppsz = '\0';
  132.     SemLeave();
  133.     return(TRUE);
  134. }
  135.  
  136.  
  137.  
  138. MRESULT EXPENTRY MonitorWndProc(hwnd, msg, mp1, mp2)
  139. HWND hwnd;
  140. USHORT msg;
  141. register MPARAM mp1;
  142. MPARAM mp2;
  143. {
  144.     PAPPINFO pai;
  145.     HDMGDATA hDmgData;
  146.  
  147.     pai = GetCurrentAppInfo(FALSE);
  148.  
  149.     switch (msg) {
  150.     case WM_CREATE:
  151.         mp1  = (PSZ)"\n\rMonitor Created\n\r\n\r";
  152.         goto MonOut;
  153.         break;
  154.  
  155.     case WM_DESTROY:
  156.         mp1 = (PSZ)"\n\r\n\rMonitor Destroyed\n\r";
  157.         /* fall through */
  158.  
  159.     case UM_MONITOR:
  160.         /*
  161.          * mp1 = psz to print
  162.          */
  163. MonOut:
  164.         hDmgData = DdePutData((PSZ)mp1, (ULONG)(lstrlen(mp1) + 1),
  165.                 0L, (HSZ)0L, DDEFMT_TEXT, 0);
  166.         pai->cInCallback++;
  167.         DoCallback(pai, 0, 0, 0, DDEFMT_TEXT, XTYP_MONITOR, hDmgData);
  168.         if (pai->cInCallback > 0)   /* test incase exitlist processing messed things up */
  169.             pai->cInCallback--;
  170.         break;
  171.  
  172.     default:
  173.         return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  174.         break;
  175.     }
  176.  
  177.     return(0);
  178. }
  179.  
  180.  
  181.