home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_7 / 2.ddi / WEXAMPLE.ZIP / MSGPROC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  4.2 KB  |  143 lines

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. //*******************************************************************
  4. //
  5. // program - MsgProc.c
  6. // purpose - A windows program to send messages to MsgWnd application.
  7. //           This file is part of the TSTAPP project.
  8. //
  9. //*******************************************************************
  10.  
  11.  
  12. #include <windows.h>
  13. #include <dde.h>
  14. #include <string.h>
  15.  
  16. # include "msgproc.h"
  17. # include "ddeclnt.h"
  18.  
  19. static HWND hChannel;
  20. static HWND hMsgWnd;
  21.  
  22. //*******************************************************************
  23. // SendMsgWndMsg - send a message to the MsgWnd application
  24. //
  25. // paramaters:
  26. //             hWnd          - The window handle of caller
  27. //             command       - The command to send to msgwnd application
  28. //             msg           - The message to send to msgwnd application
  29. //
  30. //*******************************************************************
  31. void SendMsgWndMsg(HWND hWnd, char *command, char *msg)
  32. {
  33.     HANDLE      hmsg;
  34.     LPSTR       lpmsg;
  35.     LONG        lParam;
  36.  
  37.     // If the msgwnd application is running and we have a conversation
  38.     // channel established with it, lets send our text message to it.
  39.     if (GetMsgLink(hWnd))
  40.     {
  41.         // Allocate some shared memory to put message into.
  42.         hmsg = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
  43.                            (DWORD) (strlen(command) + strlen(msg) + 8));
  44.         if (hmsg)
  45.         {
  46.             // Lock it down to get addres of memory.
  47.             lpmsg = GlobalLock(hmsg);
  48.             if (lpmsg)
  49.             {
  50.                 // Build print command to send to msgwnd application.
  51.                 _fstrcpy(lpmsg, "[");
  52.                 _fstrcat(lpmsg, command);
  53.                 if (msg)
  54.                 {
  55.                     _fstrcat(lpmsg, " (\"");
  56.             _fstrcat(lpmsg, msg);
  57.                     _fstrcat(lpmsg, "\")");
  58.                 }
  59.                 _fstrcat(lpmsg, "]");
  60.  
  61.                 // Send the command.
  62.                 DDEExecute(hChannel, lpmsg, 0);
  63.  
  64.                 GlobalUnlock(hmsg);
  65.             }
  66.  
  67.             GlobalFree(hmsg);
  68.         }
  69.     }
  70. }
  71.  
  72. //*******************************************************************
  73. // GetLines - get number of lines printed by msgwnd
  74. //
  75. // paramaters:
  76. //             hWnd          - The window handle of caller
  77. //
  78. // returns:
  79. //          handle to 'lines' data item.
  80. //            or
  81. //          NULL if it was not returned.
  82. //
  83. //*******************************************************************
  84. HANDLE GetLines(HWND hWnd)
  85. {
  86.     HANDLE hTmp;
  87.  
  88.     hTmp = 0;
  89.  
  90.     // If the msgwnd application is running and we have a conversation
  91.     // channel established with it, lets send our text message to it.
  92.     if (GetMsgLink(hWnd))
  93.     {
  94.         // Go ask for the 'lines' data item from msgwnd application.
  95.         hTmp = DDERead(hChannel, "lines", 5);
  96.     }
  97.  
  98.     // Return handle to 'lines' data item or NULL if it was not returned.
  99.     return(hTmp);
  100. }
  101.  
  102. //*******************************************************************
  103. // CloseMsgWnd - close dde message link to the MsgWnd application
  104. //
  105. //*******************************************************************
  106. void CloseMsgWnd()
  107. {
  108.     // Tell msgwnd application we want to terminate our conversation.
  109.     DDEClientClose(hChannel);
  110.     hChannel = 0;
  111. }
  112.  
  113. //*******************************************************************
  114. // GetMsgLink - establish link to msgwnd application
  115. //
  116. //*******************************************************************
  117. int GetMsgLink(HWND hWnd)
  118. {
  119.     char msgbuf[80];
  120.  
  121.     // See if msgwnd application is already running.
  122.     hMsgWnd = FindWindow("msgwnd", NULL);
  123.     if (!hMsgWnd)
  124.     {
  125.     // If not, try to activate it.
  126.         strcpy(msgbuf, "MSGWND.EXE");
  127.         WinExec(msgbuf, SW_SHOWNORMAL);
  128.     hMsgWnd = FindWindow("msgwnd", NULL);
  129.         hChannel = 0;
  130.     }
  131.  
  132.     // Have we started a conversation with the msgwnd application yet?
  133.     if (!hChannel)
  134.     {
  135.         // No, try to start one.
  136.         hChannel = DDEClientOpen(hWnd, "msgwnd", "screen");
  137.     }
  138.  
  139.     return((int) hChannel);
  140. }
  141.  
  142. //*******************************************************************
  143.