home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dde.zip / PM / PMDDE.C < prev    next >
Text File  |  1994-05-19  |  13KB  |  440 lines

  1. /*----------------------------------------------------------------------------*/
  2. /* PM DDE side                                                                */
  3. /* (c) Larry Morley 1993, 1994                                                */
  4. /*                                                                            */
  5. /* Intended for use with "WinDDE" - Demonstrates passing data using a private */
  6. /* (binary) format between a PM application and a Win-OS/2 application.       */
  7. /*                                                                            */
  8. /* This code was constructed from a variety of sources.  Trial and error,     */
  9. /* code written for OS/2 1.2 and 1.3, and some bits and pieces that I can't   */
  10. /* honestly remember.  It took me a LONG time to get it to work correctly     */
  11. /* for transferring binary data blocks unaltered.  I honestly don't remember  */
  12. /* where some pieces of this came from.  They work, though, and are available */
  13. /* for use.                                                                   */
  14. /*----------------------------------------------------------------------------*/
  15.  
  16. #define INCL_WIN
  17. #define INCL_GPI
  18. #define INCL_DOS
  19. #include <os2.h>
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25.  
  26. #include "pmdde.h"
  27. #include "dialog.h"
  28.  
  29. /*----------------------------------------------------------------------------*/
  30.  
  31. int     main(void);
  32. #define CF_LARRYS_PRIVATE_FMT 20
  33.  
  34. MRESULT EXPENTRY ClientWndProc(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2);
  35. MRESULT EXPENTRY dlgAbout     (HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2);
  36. MRESULT EXPENTRY dlgDDE       (HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2);
  37.  
  38. PDDESTRUCT EXPENTRY MakeDDEDataSeg(     /* Make A Memory Block For DDE       */
  39.    USHORT usSegSize,                    /* Requested segment size            */
  40.    USHORT usFormat,                     /* Format of data in the segment     */
  41.    PSZ    pszItemName,                  /* Size of DDE item name             */
  42.    PVOID  pvData,                       /* Pointer to data to encapsulate    */
  43.    USHORT cbData,                       /* Size of above data                */
  44.    USHORT fsStatus);                    /* Status flags                      */
  45.  
  46. VOID PrintToWindow(PSZ s);
  47.  
  48. /*----------------------------------------------------------------------------*/
  49.  
  50. CHAR    szClientClass[] = "ClientWindow";
  51. ULONG   ulFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU  |
  52.                        FCF_SIZEBORDER    | FCF_MINMAX   |
  53.                        FCF_SHELLPOSITION | FCF_TASKLIST |
  54.                        FCF_MENU          | FCF_ICON;
  55. HAB     hab;
  56.  
  57. FONTMETRICS fm;
  58.  
  59. int     cyChar = 0;
  60. int     cyDesc = 0;
  61. int     cxChar = 0;
  62. int     cxCaps = 0;
  63.  
  64. int     cxClient;
  65. int     cyClient;
  66.  
  67. HWND    hwndFrame;
  68. HWND    hwndClient;
  69. HWND    hwndDDE;
  70. HWND    hwndMLE;
  71.  
  72. PSZ     pDataBuffer;
  73.  
  74. /*----------------------------------------------------------------------------*/
  75. /* Ordinary startup code                                                      */
  76. /*----------------------------------------------------------------------------*/
  77.  
  78. int main(void)
  79. {
  80.    HMQ  hmq;
  81.    QMSG qmsg;
  82.  
  83.    pDataBuffer = (PSZ)malloc(1024);
  84.    if (!pDataBuffer)
  85.       return 1;
  86.  
  87.    hab = WinInitialize((USHORT)0);
  88.    hmq = WinCreateMsgQueue(hab,(SHORT)0);
  89.  
  90.    WinRegisterClass(
  91.       hab,szClientClass,
  92.       (PFNWP)ClientWndProc,
  93.       CS_SIZEREDRAW,
  94.       (USHORT)0);
  95.  
  96.    hwndFrame = WinCreateStdWindow(
  97.       HWND_DESKTOP,
  98.       WS_VISIBLE,
  99.       &ulFrameFlags,
  100.       szClientClass,
  101.       (PSZ)0,
  102.       (ULONG)0L,
  103.       (HMODULE)0,
  104.       (USHORT)ID_RESOURCE,
  105.       &hwndClient);
  106.  
  107.    WinSetWindowText(hwndFrame,"DDE Sample: PM <--> Win-OS/2");
  108.  
  109.    while (WinGetMsg(hab,&qmsg,(HWND)0,(USHORT)0,(USHORT)0))
  110.       WinDispatchMsg(hab,&qmsg);
  111.  
  112.    WinDestroyWindow(hwndFrame);
  113.    WinDestroyMsgQueue(hmq);
  114.    WinTerminate(hab);
  115.  
  116.    return 0;
  117. }
  118.  
  119. /*----------------------------------------------------------------------------*/
  120. /* Display info in the MLE on the DDE dialog.                                 */
  121. /*----------------------------------------------------------------------------*/
  122.  
  123. void PrintToWindow(PSZ s)
  124. {
  125.    int len;
  126.    PSZ p;
  127.  
  128.    if (hwndMLE)
  129.    {
  130.       len = WinQueryWindowTextLength(hwndMLE) + 1;
  131.       p = (PSZ)malloc(len+strlen(s)+16);
  132.       if(p)
  133.       {
  134.          WinQueryWindowText(hwndMLE,(LONG)(len+strlen(s)),p);
  135.          strcat(p,"\n");
  136.          strcat(p,s);
  137.          WinSetWindowText(hwndMLE,p);
  138.          free(p);
  139.       }
  140.    }
  141.    return;
  142. }
  143.  
  144. /*----------------------------------------------------------------------------*/
  145. /* Client area window proc                                                    */
  146. /*----------------------------------------------------------------------------*/
  147.  
  148. MRESULT EXPENTRY ClientWndProc(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
  149. {
  150.  
  151.    HPS   hPS;
  152.    RECTL rcl;
  153.  
  154.    PSZ        s;
  155.    PDDESTRUCT pDdes;
  156.  
  157.    switch (msg)
  158.    {
  159.  
  160.       case WM_CREATE:
  161.  
  162.          hPS = WinGetPS(hwnd);
  163.          GpiQueryFontMetrics(hPS,(LONG)sizeof(fm),&fm);
  164.          cyChar = (int)fm.lMaxBaselineExt;
  165.          cyDesc = (int)fm.lMaxDescender;
  166.          cxChar = (int)fm.lEmInc;
  167.          cxCaps = (int)fm.lAveCharWidth;
  168.          WinReleasePS(hPS);
  169.  
  170.          break;
  171.  
  172.       case WM_SIZE:
  173.  
  174.          cxClient = SHORT1FROMMP(mp2);
  175.          cyClient = SHORT2FROMMP(mp2);
  176.  
  177.          break;
  178.  
  179.       /*-------------------------------------------*/
  180.       /* Another window is making a DDE request.   */
  181.       /*-------------------------------------------*/
  182.  
  183.       case WM_DDE_INITIATE:
  184.  
  185.          /*-------------------------------------------*/
  186.          /* mp1 is hwnd of window making DDE request. */
  187.          /*                                           */
  188.          /* mp2 points to a PDDEINIT structure        */
  189.          /*    USHORT cb                              */
  190.          /*    PSZ    pszAppName                      */
  191.          /*    PSZ    pszTopic                        */
  192.          /*-------------------------------------------*/
  193.  
  194.          if (!(strcmpi(((PDDEINIT)mp2)->pszAppName,"LARRYS_APP")))
  195.          {
  196.             char szMsg[128];
  197.  
  198.             sprintf(szMsg,"Received INITIATE hwnd=%8.8X",(HWND)mp1);
  199.             PrintToWindow(szMsg);
  200.  
  201.             hwndDDE = (HWND)mp1;
  202.  
  203.             WinDdeRespond(
  204.                hwndDDE,
  205.                hwnd,
  206.                "LARRYS_APP",
  207.                "ANY_TOPIC",
  208.                (PCONVCONTEXT)0);
  209.          }
  210.  
  211.          break;
  212.  
  213.       /*-------------------------------------------*/
  214.       /* Send some data                            */
  215.       /*-------------------------------------------*/
  216.  
  217.       case WM_DDE_REQUEST:
  218.       {
  219.          s = "DDE response";
  220.  
  221.          pDdes = MakeDDEDataSeg(
  222.             1024,
  223.             CF_TEXT,
  224.             "ANY_ITEM",
  225.             s,
  226.             strlen(s)+1,
  227.             0);
  228.  
  229.          WinDdePostMsg((HWND)mp1,hwnd,WM_DDE_DATA,pDdes,TRUE);
  230.  
  231.          PrintToWindow("Got request, sent response");
  232.  
  233.          break;
  234.       }
  235.  
  236.       /*-------------------------------------------*/
  237.  
  238.       case WM_DDE_DATA:
  239.       case WM_DDE_POKE:
  240.  
  241.          /*--------------------------------------------------*/
  242.          /* We're being sent data.  These two cases could be */
  243.          /* handled separately; POKE is unsolicited, DATA is */
  244.          /* solicited.                                       */
  245.          /*                                                  */
  246.          /* In both cases, the actual data starts at:        */
  247.          /*    DDES_PABDATA[0], of PDDESTRUCT mp2.           */
  248.          /*--------------------------------------------------*/
  249.  
  250.          PrintToWindow(DDES_PABDATA((PDDESTRUCT)mp2));
  251.  
  252.          break;
  253.  
  254.       /*-------------------------------------------*/
  255.  
  256.       case WM_PAINT:
  257.  
  258.          hPS = WinBeginPaint(hwnd,(HPS)0,&rcl);
  259.          GpiErase(hPS);
  260.          WinEndPaint(hPS);
  261.          break;
  262.  
  263.       case WM_COMMAND:
  264.  
  265.          switch (SHORT1FROMMP(mp1))
  266.          {
  267.             case IDM_FILE_ABOUT:
  268.  
  269.                WinDlgBox(
  270.                   HWND_DESKTOP,
  271.                   hwnd,
  272.                   (PFNWP)dlgAbout,
  273.                   (HMODULE)0,
  274.                   DLG_ABOUT,
  275.                   (PVOID)0);
  276.  
  277.                return (MRESULT) 0;
  278.  
  279.             case IDM_FILE_EXIT:
  280.  
  281.                WinPostMsg(hwnd,WM_CLOSE,(MPARAM)0,(MPARAM)0);
  282.                return (MRESULT) 0;
  283.  
  284.             case IDM_FILE_DDE:
  285.  
  286.                WinDlgBox(
  287.                   HWND_DESKTOP,
  288.                   hwnd,
  289.                   (PFNWP)dlgDDE,
  290.                   (HMODULE)0,
  291.                   DLG_DDE,
  292.                   (PVOID)0);
  293.  
  294.                return (MRESULT) 0;
  295.  
  296.          }
  297.  
  298.          break;
  299.  
  300.       default:
  301.  
  302.          return WinDefWindowProc(hwnd,msg,mp1,mp2);
  303.  
  304.    }
  305.  
  306.    return (MRESULT) FALSE;
  307.  
  308. }
  309.  
  310. /*----------------------------------------------------------------------------*/
  311.  
  312. MRESULT EXPENTRY dlgAbout(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
  313. {
  314.  
  315.    switch (msg)
  316.    {
  317.       case WM_COMMAND:
  318.  
  319.          switch (SHORT1FROMMP(mp1))
  320.          {
  321.             case DID_OK:
  322.  
  323.                WinDismissDlg(hwnd,TRUE);
  324.                return (MRESULT) 0;
  325.          }
  326.    }
  327.  
  328.    return WinDefDlgProc(hwnd,msg,mp1,mp2);
  329. }
  330.  
  331. /*----------------------------------------------------------------------------*/
  332.  
  333. MRESULT EXPENTRY dlgDDE(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
  334. {
  335.  
  336.    PDDESTRUCT pDDE;
  337.  
  338.    switch (msg)
  339.    {
  340.       case WM_INITDLG:
  341.  
  342.          hwndMLE = WinWindowFromID(hwnd,EF_REPLY);
  343.          break;
  344.  
  345.       case WM_COMMAND:
  346.  
  347.          switch (SHORT1FROMMP(mp1))
  348.          {
  349.             case PB_TRANSACT:
  350.             {
  351.                WinQueryDlgItemText(hwnd,EF_SEND,128,pDataBuffer);
  352.  
  353.                if (pDataBuffer[0])
  354.                {
  355.                   pDDE = MakeDDEDataSeg(
  356.                             sizeof(pDataBuffer),
  357.                             CF_LARRYS_PRIVATE_FMT,
  358.                             "ANY_ITEM",
  359.                             (PVOID)pDataBuffer,
  360.                             sizeof(pDataBuffer),
  361.                             0);
  362.  
  363.                   WinDdePostMsg(hwndDDE,hwndClient,WM_DDE_DATA,pDDE,TRUE);
  364.                   PrintToWindow("Message sent...");
  365.                }
  366.                return (MRESULT)0;
  367.             }
  368.  
  369.             case DID_CANCEL:
  370.  
  371.                hwndMLE = (HWND)0;
  372.                WinDismissDlg(hwnd,TRUE);
  373.                return (MRESULT) 0;
  374.          }
  375.    }
  376.  
  377.    return WinDefDlgProc(hwnd,msg,mp1,mp2);
  378. }
  379.  
  380. /*----------------------------------------------------------------------------*/
  381.  
  382. PDDESTRUCT EXPENTRY MakeDDEDataSeg(USHORT usSegSize,
  383.                                    USHORT usFormat,
  384.                                    PSZ pszItemName,
  385.                                    PVOID pvData,
  386.                                    USHORT cbData,
  387.                                    USHORT fsStatus)
  388. {
  389.   PDDESTRUCT pdde = NULL;              // ptr to dde data structure
  390.   ULONG rc = 0;                        // api return code holder
  391.   PULONG pulSharedObj = (PULONG)0;     // pointer to shared object
  392.   PCHAR psz = NULL;                    // string ptr for text format
  393.   USHORT cbObjSize = 0;                // count of bytes to request
  394.  
  395.   /*------------------------------------------------------------------------*/
  396.   /* Allocate a givable memory block                                        */
  397.   /*------------------------------------------------------------------------*/
  398.  
  399.    cbObjSize = (USHORT)(strlen(pszItemName) + 1);
  400.    rc = DosAllocSharedMem(
  401.       ((PPVOID)&pulSharedObj),
  402.       (PSZ)0,
  403.       cbObjSize,
  404.       PAG_COMMIT | PAG_READ | PAG_WRITE | OBJ_GIVEABLE);
  405.  
  406.    if (rc && pulSharedObj)          // check api return code and ptr
  407.      return (PDDESTRUCT)0;
  408.  
  409.   /*------------------------------------------------------------------------*/
  410.   /* Fill in the new DDE structure                                          */
  411.   /*------------------------------------------------------------------------*/
  412.  
  413.    pdde = (PDDESTRUCT) pulSharedObj;
  414.    pdde->usFormat = usFormat;
  415.    pdde->offszItemName = (USHORT)sizeof(DDESTRUCT);
  416.    strcpy(DDES_PSZITEMNAME(pdde), pszItemName);
  417.    pdde->cbData = cbData;
  418.    pdde->offabData = (USHORT) pdde->offszItemName +
  419.                      (USHORT)strlen(pszItemName)  + (USHORT)1;
  420.    pdde->fsStatus = fsStatus;
  421.  
  422.    if (usFormat == CF_TEXT)
  423.    {
  424.      if (pvData == NULL || cbData == 0)
  425.         psz = '\0';
  426.      else
  427.         psz = pvData;
  428.      strcpy(DDES_PABDATA(pdde), psz);
  429.    }
  430.    else
  431.    {
  432.       if (pvData != (VOID *)0 || cbData != 0)
  433.          memcpy(DDES_PABDATA(pdde), pvData, (size_t)cbData);
  434.    }
  435.  
  436.    return  pdde;
  437.  
  438. }
  439. /*----------------------------------------------------------------------------*/
  440.