home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 6.ddi / WEXAMPLE.ZIP / MSGPROC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.2 KB  |  143 lines

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