home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pc3270sa.zip / spl2file / spsdde.c < prev    next >
Text File  |  2002-02-28  |  23KB  |  499 lines

  1. //******************************************************************************
  2. //
  3. //  File name   : SPDDE.C
  4. //
  5. //  Description : 5250 Session DDE interface functions for Spl2File DDE application
  6. //
  7. //  FUNCTIONS:
  8. //
  9. //
  10. //  COMMENTS:
  11. //
  12. //  Copyright  (C) 1993, 1996 IBM Corporation
  13. //                        All rights reserved.
  14. //
  15. //******************************************************************************
  16.  
  17. #include <windows.h>                // required for all Windows applications
  18. #include <string.h>                 // C string functions
  19. #include <stdio.h>                  // C functions
  20.  
  21. #include "spl2file.h"               // specific to this program
  22. #include "spdata.h"                 // Global Data
  23. #include "spdde.h"                  // definitions of DDE data structures
  24. #include "dde_c.h"                  // required for PCOM DDE Appls
  25.  
  26. //******************************************************************************
  27. //
  28. // Local Data
  29. //
  30. //******************************************************************************
  31.  
  32. static uchar  zMenuServ[17];
  33. static uchar  zMenuBar[17];
  34. static uchar  zMenuMap[17];
  35. static uchar  zABOUT[17];
  36. static uchar  zSPOOL[17];
  37. static uchar  zZCONFG[17];
  38.  
  39. static uchar *zMenuStruct =
  40. {
  41. "POPUP \"Sp&l2File\"\r\n\
  42. BEGIN\r\n\
  43.   MENUITEM \"Convert a &spool file\", SPOOL\r\n\
  44.   MENUITEM SEPARATOR\r\n\
  45.   MENUITEM \"&Config...\", ZCONFG\r\n\
  46.   MENUITEM SEPARATOR\r\n\
  47.   MENUITEM \"&About Spl2File...\", ABOUT\r\n\
  48. END\r\n"
  49. };
  50.  
  51. //******************************************************************************
  52. //
  53. //  InitDDE - Initialize various items needed for DDE
  54. //
  55. //  PURPOSE:
  56. //
  57. //
  58. //  COMMENTS:
  59. //
  60. //******************************************************************************
  61. BOOL InitDDE( void )
  62. {
  63.   LoadString(hGlobalInst,  LS_16STR_FORMAT, zFmtBuffer, sizeof(zFmtBuffer));
  64.   LoadString(hGlobalInst,  LS_DDE_TOKENS,   zMsgBuffer, sizeof(zMsgBuffer));
  65.  
  66.   sscanf( zMsgBuffer,                          // Input String
  67.           zFmtBuffer,                          // sscanf format to use
  68.           zMenuServ,                           // MenuServ
  69.           zMenuBar,                            // MenuBar
  70.           zMenuMap,                            // MenuMap
  71.           zSPOOL,                              // SPOOL TO FILE
  72.           zZCONFG,                             // CONFIGURE MYPRINT
  73.           zABOUT   );                          // ABOUT
  74.                                                //
  75.   lstrcat(zMenuServ, zSessionID );             // Append Session ID
  76.  
  77.   return( TRUE );                              //
  78. }                                              //
  79.  
  80. //******************************************************************************
  81. //
  82. //  SpDDEInitiate - Process the DDE_INITIATE message
  83. //
  84. //  PURPOSE:
  85. //
  86. //
  87. //  COMMENTS:
  88. //
  89. //******************************************************************************
  90. BOOL FAR SpDDEInitiate(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
  91. {                                                   //
  92.                                                     //
  93.   ATOM aApplication  = LOWORD( lParam );            //
  94.   ATOM aTopic        = HIWORD( lParam );            //
  95.                                                     //
  96.   ATOM aMenuServ     = GlobalAddAtom( (LPSTR)zMenuServ );  // Register Atoms
  97.   ATOM aMenuBar      = GlobalAddAtom( (LPSTR)zMenuBar  );  //
  98.   ATOM aSPOOL        = GlobalAddAtom( (LPSTR)zSPOOL    );  //
  99.   ATOM aZCONFG       = GlobalAddAtom( (LPSTR)zZCONFG   );  //
  100.   ATOM aABOUT        = GlobalAddAtom( (LPSTR)zABOUT    );  //
  101.  
  102.   BOOL bRC           = FALSE;                       //
  103.  
  104.   if( aApplication == aMenuServ )                   //
  105.   {                                                 //
  106.     if( aTopic == aMenuBar )                        // Setup a menu bar?
  107.     {                                               // Yes,
  108.       {                                             //
  109.         hDDEClientWnd  = (HWND)wParam;              // Save away client window handle
  110.         bRC            = TRUE;                      // Signal success
  111.         if( !SendMessage((HWND)wParam, WM_DDE_ACK, (WPARAM)hWnd, MAKELONG( aApplication, aTopic )) )
  112.         {                                           //
  113.           GlobalDeleteAtom( aApplication );         // Error, delete client's ATOMS
  114.           GlobalDeleteAtom( aTopic    );            // Error, delete client's ATOMS
  115.         }                                           //
  116.       }                                             //
  117.     }                                               //
  118.     else if( aTopic == aSPOOL || aTopic == aZCONFG  //
  119.              || aTopic == aABOUT )                  //
  120.     {                                               //
  121.       HWND         hSessionWnd;                     //
  122.       WORD         uCMD;                            //
  123.       bRC        = TRUE;                            // Signal success
  124.       hDDEClientWnd  = (HWND)wParam;                // Save away client window handle
  125.       if( !SendMessage((HWND)wParam, WM_DDE_ACK, (WPARAM)hWnd, MAKELONG( aApplication, aTopic )) )
  126.       {                                             //
  127.         GlobalDeleteAtom( aApplication );           // Error, delete client's ATOMS
  128.         GlobalDeleteAtom( aTopic    );              // Error, delete client's ATOMS
  129.       }                                             //
  130.       if( aTopic == aSPOOL )                        // convert spool to file
  131.       {                                             //
  132.         uCMD = ID_SPOOL;                            //
  133.       }                                             //
  134.       else if( aTopic == aZCONFG )                  // config
  135.       {                                             //
  136.         uCMD = ID_ZCONFG;                           //
  137.       }                                             //
  138.       else if( aTopic == aABOUT )                   // about
  139.       {                                             //
  140.         uCMD = ID_ABOUT;                            //
  141.       }                                             //
  142.       hSessionWnd = FindWindow((LPSTR) zSessionClassName, (LPSTR) NULL);
  143.       PostMessage(hWnd, WM_COMMAND, uCMD, MAKELONG( wParam, hSessionWnd) );
  144.     }                                               //
  145.   }                                                 //
  146.  
  147.   GlobalDeleteAtom( aMenuServ );                    // Delete Atoms
  148.   GlobalDeleteAtom( aMenuBar  );                    //
  149.   GlobalDeleteAtom( aSPOOL    );                    //
  150.   GlobalDeleteAtom( aZCONFG   );                    //
  151.   GlobalDeleteAtom( aABOUT    );                    //
  152.  
  153.   return( bRC );                                    //
  154. }                                                   //
  155.  
  156. //******************************************************************************
  157. //
  158. //  SpDDEAck - Process the DDE_ACK message
  159. //
  160. //  PURPOSE:
  161. //
  162. //
  163. //  COMMENTS:
  164. //
  165. //******************************************************************************
  166. BOOL FAR SpDDEAck(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
  167. {                                                   //
  168.   BYTE Event;                                       //
  169.   BOOL bRC           = FALSE;                       // Error until proven otheriwse ...
  170.   UINT uiLo, uiHi;                                  //
  171.  
  172.   if ((LOWORD(lParam) == aIBM5250) &&               // Check Application and
  173.       (HIWORD(lParam) == aSession))                 //  topic name
  174.   {                                                 //
  175.     hSessWnd = (HWND)wParam;                        // Ack of WM_DDE_INITIATE
  176.     bRC = TRUE;                                     //
  177.   }                                                 //
  178.   else if ((HWND)wParam == hSessWnd)                // Ack of other messages
  179.   {                                                 //
  180.     UnpackDDElParam(WM_DDE_ACK, lParam, (PUINT)&uiLo, (PUINT)&uiHi);
  181.  
  182.     if (uiLo == DDE_ACK)                            // O.K.
  183.     {                                               //
  184.       Event = EV_ACK;                               //
  185.     }                                               //
  186.     else                                            // N.G.
  187.     {                                               //
  188.       Event = EV_NACK;                              //
  189.     }                                               //
  190.  
  191.     if (uLastMessage == WM_DDE_EXECUTE)             // process next step
  192.     {                                               //
  193.       bRC = FSM_Event( Event, (unsigned short)NULL, (HANDLE)uiHi ); //
  194.     }                                               //
  195.     else                                            //
  196.     {                                               //
  197.       bRC = FSM_Event( Event, (ATOM)uiHi, NULL );   //
  198.     }                                               //
  199.   }                                                 //
  200.   return( bRC  );                                   //
  201. }                                                   //
  202.  
  203. //******************************************************************************
  204. //
  205. //  SpDDEData - Process the DDE_DATA message
  206. //
  207. //  PURPOSE:
  208. //
  209. //
  210. //  COMMENTS:
  211. //
  212. //******************************************************************************
  213. BOOL FAR SpDDEData(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
  214. {                                                   //
  215.   BOOL bRC           = FALSE;                       // Error until proven otheriwse ...
  216.   UINT uiLo, uiHi;                                  //
  217.  
  218.   UnpackDDElParam(WM_DDE_DATA, lParam, (PUINT)&uiLo, (PUINT)&uiHi);
  219.   if ((HWND)wParam == hSessWnd)                     //
  220.   {                                                 //
  221.     bRC = FSM_Event( EV_DATA, (ATOM)uiHi, (HANDLE)uiLo );
  222.   }                                                 // process the next step
  223.   return( bRC  );                                   //
  224. }                                                   //
  225.  
  226. //******************************************************************************
  227. //
  228. //  SpDDERequest - Process the DDE_REQUEST message
  229. //
  230. //  PURPOSE:
  231. //
  232. //
  233. //  COMMENTS:
  234. //
  235. //******************************************************************************
  236. BOOL FAR SpDDERequest(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
  237. {
  238.   ATOM aMenuMap = GlobalAddAtom( zMenuMap );     // Holder Atom
  239.   ATOM aItem    = HIWORD( lParam );              // Requested aItem
  240.   WORD cfFormat = LOWORD( lParam );              // Requested Format
  241.   BOOL bRC      = FALSE;                         // Error until proven otheriwse ...
  242.   DDEDATA FAR *   lpData;                        // Long Pointer to DATA structure
  243.   HANDLE          hData;                         // Handle       to DATA structure
  244.   DWORD           dwSize = (sizeof(DDEDATA) + strlen(zMenuStruct) + 16);
  245.  
  246.   if( (aItem == aMenuMap) && (cfFormat == CF_TEXT) )
  247.   {                                              //
  248.     if( (hData = GlobalAlloc(DDE_GLOBAL, dwSize)) != NULL )
  249.     {                                            //
  250.       if( (lpData = (DDEDATA FAR *) GlobalLock(hData)) != NULL ) //
  251.       {                                          //
  252.         lpData->cfFormat = cfFormat;             // Format
  253.         lpData->fAckReq  = FALSE;                // No return ACK
  254.         lpData->fRelease = TRUE;                 // Client GlobalFree this buffer
  255.         lstrcpy(lpData->Value, zMenuStruct);     // Data
  256.         GlobalUnlock(hData);                     // Unlock the buffer
  257.         aItem = GlobalAddAtom( zMenuMap );       // Add item for call
  258.         if( !PostMessage((HWND)wParam, WM_DDE_DATA, (WPARAM)hWnd,
  259.                          PackDDElParam(WM_DDE_DATA, (UINT)hData, aItem)) )
  260.         {                                        //
  261.           GlobalDeleteAtom( aItem  );            // Delete the item
  262.           GlobalFree(hData);                     // and free the data
  263.           TerminateConversation();               //
  264.         }                                        //
  265.         else                                     //
  266.         {                                        //
  267.           bRC = TRUE;                            // Success!
  268.         }                                        //
  269.       }                                          //
  270.       else                                       // Couldn't lock it ...
  271.       {                                          //
  272.         GlobalFree(hData);                       // So free it .....
  273.       }                                          //
  274.     }                                            //
  275.   }                                              //
  276.   GlobalDeleteAtom( aMenuMap );                  // Delete the holder atom
  277.   return( bRC  );                                // return bRC
  278. }                                                //
  279.  
  280. //******************************************************************************
  281. //
  282. //  SpDDETerminate - Process the DDE_TERMINATE message
  283. //
  284. //  PURPOSE:
  285. //
  286. //
  287. //  COMMENTS:
  288. //
  289. //******************************************************************************
  290. BOOL FAR SpDDETerminate(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
  291. {                                                //
  292.   if ((HWND)wParam == hSessWnd)                  //
  293.   {                                              //
  294.     hSessWnd = NULL;                             //
  295.   }                                              //
  296.   else                                           //
  297.   {                                              //
  298.     if( hDDEClientWnd != NULL )                  // Is client alive?
  299.     {                                            // Terminate him
  300.       TerminateConversation();                   //
  301.     }                                            //
  302.   }                                              //
  303.  
  304.   return( TRUE );                                // return success
  305. }                                                //
  306.  
  307. //******************************************************************************
  308. //
  309. //  PrepGetPartialPs - Issues "Set Prsentation Space Service Condition".
  310. //
  311. //  PURPOSE:
  312. //
  313. //
  314. //  COMMENTS:
  315. //
  316. //******************************************************************************
  317. void PrepGetPartialPs(int nRow, int nCol, int nLength)
  318. {                                                //
  319.   HANDLE            hData;                       //
  320.   lpDDE_PSSERVCOND  lpPsCond;                    //
  321.  
  322.   hData = GlobalAlloc(WC_DDE_GLOBAL, (DWORD)(sizeof(DDE_PSSERVCOND)));
  323.   if (hData)                                     // allocate message buffer
  324.   {                                              //
  325.     lpPsCond = (lpDDE_PSSERVCOND)GlobalLock(hData);
  326.     if (HIWORD(lpPsCond) != (int)NULL)           //
  327.     {                                            //
  328.       lpPsCond->DDEpoke.fRelease            = TRUE;                    //
  329.       lpPsCond->DDEpoke.cfFormat            = CF_DSPTEXT;              // format
  330.       lpPsCond->DDEcond.uPSStart            = (nRow-1) * 80 + (nCol-1);// PS position
  331.       lpPsCond->DDEcond.uPSLength           = nLength;                 // Length
  332.       lpPsCond->DDEcond.szTargetString[0] = (char)NULL;                //
  333.       GlobalUnlock(hData);                       //
  334.       if (hSessWnd)                              //
  335.       {                                          //
  336.         PostMessage(hSessWnd,                    // Post message to DDE server
  337.                     WM_DDE_POKE,                 //  (PC/5250)
  338.                     (WPARAM)hMainWnd,            //
  339.                     PackDDElParam(WM_DDE_POKE, (UINT)hData, aEPSCOND));
  340.         uLastMessage = WM_DDE_POKE;              //
  341.       }                                          //
  342.       else                                       //
  343.       {                                          //
  344.         GlobalFree(hData);                       //
  345.       }                                          //
  346.     }                                            //
  347.     else                                         //
  348.     {                                            //
  349.       GlobalFree(hData);                         //
  350.     }                                            //
  351.   }                                              //
  352. }                                                //
  353.  
  354. //******************************************************************************
  355. //
  356. //  RequestGetPs - Issues "Get Prsentation Space" (CF_DSPTEXT).
  357. //
  358. //  PURPOSE:
  359. //
  360. //
  361. //  COMMENTS:
  362. //
  363. //******************************************************************************
  364. void  RequestGetPs( void )                       //
  365. {                                                //
  366.   if (hSessWnd)                                  //
  367.   {                                              //
  368.     PostMessage(hSessWnd,                        //
  369.                 WM_DDE_REQUEST,                  //
  370.                 (WPARAM)hMainWnd,                //
  371.                 MAKELONG(CF_DSPTEXT, aPS));      //
  372.     uLastMessage = WM_DDE_REQUEST;               //
  373.   }                                              //
  374. }                                                //
  375.  
  376. //******************************************************************************
  377. //
  378. //  RequestGetPartPs - Issues "Get Partial Prsentation Space" (CF_TEXT).
  379. //
  380. //  PURPOSE:
  381. //
  382. //
  383. //  COMMENTS:
  384. //
  385. //******************************************************************************
  386. void  RequestGetPartPs( void )                   //
  387. {                                                //
  388.   if (hSessWnd)                                  //
  389.   {                                              //
  390.     PostMessage(hSessWnd,                        //
  391.                 WM_DDE_REQUEST,                  //
  392.                 (WPARAM)hMainWnd,                //
  393.                 MAKELONG(CF_DSPTEXT, aEPS));     //
  394.     uLastMessage = WM_DDE_REQUEST;               //
  395.   }                                              //
  396. }                                                //
  397.  
  398. //******************************************************************************
  399. //
  400. //  ExecuteKeystrokes - Issues "Session Execute Macro" SENDKEY command
  401. //
  402. //  PURPOSE:
  403. //
  404. //
  405. //  COMMENTS:
  406. //
  407. //******************************************************************************
  408. void  ExecuteKeystrokes( LPSTR lpKeys )          //
  409. {                                                //
  410.   HANDLE   hData;                                //
  411.   LPSTR    lpData;                               //
  412.   BOOL     bRequest = FALSE;                     //
  413.  
  414.   hData = GlobalAlloc(WC_DDE_GLOBAL, (DWORD)256 );  //
  415.   if (hData)                                     //
  416.   {                                              //
  417.     lpData = GlobalLock(hData);                  //
  418.     if (HIWORD(lpData) != (int)NULL)             //
  419.     {                                            //
  420.       lstrcpy(lpData, "[SENDKEY(");              // command name
  421.       lstrcat(lpData, "wait inp inh");           // wait while input inhibit
  422.       lstrcat(lpData, ",");                      //
  423.       lstrcat(lpData, lpKeys);                   //
  424.       lstrcat(lpData, ",");                      //
  425.       lstrcat(lpData, "wait inp inh");           // wait while input inhibit
  426.       lstrcat(lpData, ")]");                     //
  427.       GlobalUnlock(hData);                       //
  428.  
  429.       if (hSessWnd)                              //
  430.       {                                          //
  431.         PostMessage(hSessWnd,                    // post to PC/5250
  432.                     WM_DDE_EXECUTE,              //
  433.                     (WPARAM)hMainWnd,            //
  434.                     (LPARAM)hData);              //
  435.         uLastMessage = WM_DDE_EXECUTE;           //
  436.         bRequest = TRUE;                         //
  437.       }                                          //
  438.     }                                            //
  439.     else                                         //
  440.     {                                            //
  441.       GlobalFree(hData);                         //
  442.     }                                            //
  443.   }                                              //
  444.   if (!bRequest)                                 //
  445.   {                                              //
  446.     PostMessage(hMainWnd,                        //
  447.                 WM_USER_ERROR,                   //
  448.                 (WPARAM)hMainWnd,                //
  449.                 MAKELONG(NULL, NULL) );          //
  450.   }                                              //
  451. }                                                //
  452.  
  453. //******************************************************************************
  454. //
  455. //  SetCursorPosition - Issues "Set Cursor Position".
  456. //
  457. //  PURPOSE:
  458. //
  459. //
  460. //  COMMENTS:
  461. //
  462. //******************************************************************************
  463. void SetCursorPosition(int nRow, int nCol)
  464. {                                                //
  465.   HANDLE            hData;                       //
  466.   lpDDE_SETCURSOR   lpCursor;                    //
  467.  
  468.   hData = GlobalAlloc(WC_DDE_GLOBAL, (DWORD)(sizeof(DDE_SETCURSOR)));
  469.   if (hData)                                     //
  470.   {                                              //
  471.     lpCursor = (lpDDE_SETCURSOR)GlobalLock(hData);
  472.     if (HIWORD(lpCursor) != (int)NULL)           //
  473.     {                                            //
  474.       lpCursor->DDEpoke.fRelease              = TRUE;       //
  475.       lpCursor->DDEpoke.cfFormat              = CF_DSPTEXT; //
  476.       lpCursor->DDEsetcursor.uSetCursorType   = 1;          // Row/Col option
  477.       lpCursor->DDEsetcursor.uSetCursor1 = nRow-1;          // Row number
  478.       lpCursor->DDEsetcursor.uSetCursor2 = nCol-1;          // Column number
  479.       GlobalUnlock(hData);                       //
  480.       if (hSessWnd)                              //
  481.       {                                          //
  482.         PostMessage(hSessWnd,                    //
  483.                     WM_DDE_POKE,                 //
  484.                     (WPARAM)hMainWnd,            //
  485.                     PackDDElParam(WM_DDE_POKE, (UINT)hData, aSETCURSOR));//
  486.         uLastMessage = WM_DDE_POKE;              //
  487.       }                                          //
  488.       else                                       //
  489.       {                                          //
  490.         GlobalFree(hData);                       //
  491.       }                                          //
  492.     }                                            //
  493.     else                                         //
  494.     {                                            //
  495.       GlobalFree(hData);                         //
  496.     }                                            //
  497.   }                                              //
  498. }                                                //
  499.