home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / iwftech.zip / samples / WFPeek / wfpeek.C next >
Text File  |  1994-08-05  |  12KB  |  336 lines

  1. /*+--------------------------------------------------------------------------+*/
  2. /*|WF/2 Message spy sample                                                   |*/
  3. /*|--------------------------------------------------------------------------|*/
  4. /*|                                                                          |*/
  5. /*| PROGRAM NAME: WFPEEK                                                     |*/
  6. /*| -------------                                                            |*/
  7. /*|                                                                          |*/
  8. /*| COPYRIGHT:                                                               |*/
  9. /*| ----------                                                               |*/
  10. /*|  Copyright (C) International Business Machines Corp., 1991,1992,1993.    |*/
  11. /*|                                                                          |*/
  12. /*| DISCLAIMER OF WARRANTIES:                                                |*/
  13. /*| -------------------------                                                |*/
  14. /*| The following [enclosed] code is sample code created by IBM              |*/
  15. /*| Corporation.  This sample code is not part of any standard IBM product   |*/
  16. /*| and is provided to you solely for the purpose of assisting you in the    |*/
  17. /*| development of your applications.  The code is provided "AS IS",         |*/
  18. /*| without warranty of any kind.  IBM shall not be liable for any damages   |*/
  19. /*| arising out of your use of the sample code, even if they have been       |*/
  20. /*| advised of the possibility of such damages.                              |*/
  21. /*|                                                                          |*/
  22. /*| REVISION LEVEL: 2.1                                                      |*/
  23. /*| -------------------                                                      |*/
  24. /*|                                                                          |*/
  25. /*|  This program illustrates registering a PM program with WF/2 and         |*/
  26. /*|  receiving messages from WF/2.                                           |*/
  27. /*|                                                                          |*/
  28. /*+--------------------------------------------------------------------------+*/
  29. #define INCL_DOS
  30. #define INCL_DOSERRORS
  31. #define INCL_WIN
  32.  
  33. #include <os2.h>
  34.  
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <stdarg.h>
  38. #include <stdio.h>
  39.  
  40. #include "wfpeek.h"
  41.  
  42. #define INCL_WKFMSG
  43. #include <wkf.h>
  44.  
  45. static char szTitle[255] = "";
  46.  
  47. MRESULT EXPENTRY DlgProc( HWND   hwndDlg,
  48.                           ULONG  msg,
  49.                           MPARAM mp1,
  50.                           MPARAM mp2 );
  51.  
  52. static void _Optlink OutList( HWND hwndDlg, PSZ pszTemplate, ... );
  53.  
  54. static void _Optlink AddTitle( HWND hwndDlg, PSZ pszTitle );
  55.  
  56. typedef struct _WFROUTER
  57.   {
  58.     LHANDLE           hRouter;
  59.   } WFROUTER;
  60.  
  61.  
  62. int main(int argc, char * argv[])
  63.    {
  64.    HAB               hAB;
  65.    HMQ               hMQ;
  66.    HWND              hwndDlg;
  67.    ULONG             ulLength;
  68.    HMODULE           hMod;
  69.    WFROUTER          wf;
  70.    char              szModError[CCHMAXPATH];
  71.  
  72.  
  73.    hAB = WinInitialize( 0 );
  74.    if ( hAB == NULLHANDLE )
  75.       {
  76.       puts( "Can't initialize anchor block" );
  77.       DosExit( EXIT_PROCESS, 255 );
  78.       }
  79.  
  80.    hMQ = WinCreateMsgQueue( hAB, 0 );
  81.  
  82.    ulLength = WinLoadString( hAB,
  83.                              NULLHANDLE,
  84.                              IDS_WFPEEK_TITLE,
  85.                              sizeof( szTitle ),
  86.                              szTitle );
  87.    szTitle[ulLength] = '\0';
  88.  
  89.    hwndDlg = WinLoadDlg( HWND_DESKTOP,
  90.                          HWND_DESKTOP,
  91.                          DlgProc,
  92.                          0,
  93.                          IDD_WFPEEK_MAIN,
  94.                          (PVOID)&wf );
  95.  
  96.    if ( hwndDlg != NULLHANDLE )
  97.       WinProcessDlg( hwndDlg );
  98.  
  99.    WinDestroyMsgQueue( hMQ );
  100.    WinTerminate( hAB );
  101.    return( 0 );
  102.    }
  103.  
  104. /*----------------------------------------------------------------------------*/
  105. /*- main dialog procedure                                                    -*/
  106. /*----------------------------------------------------------------------------*/
  107. MRESULT EXPENTRY DlgProc( HWND   hwndDlg,
  108.                           ULONG  msg,
  109.                           MPARAM mp1,
  110.                           MPARAM mp2 )
  111.    {
  112.    WFROUTER * pwf;
  113.    BOOL fSuccess;
  114.  
  115.    switch( msg )
  116.       {
  117.       case WKFM_CLIENTRENAMED:
  118.       case WKFM_CLIENTMOVED:
  119.            {
  120.            PWKF_RENAMEINFO pMsgData;
  121.            PSZ             pszNewName;
  122.            PSZ             pszOldName;
  123.  
  124.            pwf = (WFROUTER *)WinQueryWindowULong( hwndDlg, QWL_USER );
  125.  
  126.            /* -------------------------------------------------------------- */
  127.            /* make sure we can access the message data before mucking about  */
  128.            /* -------------------------------------------------------------- */
  129.            if ( WkfAccessMsgData (mp2) )
  130.               {
  131.               pMsgData   = (PWKF_RENAMEINFO)mp2;
  132.               pszOldName = pMsgData->szParm + strlen( pMsgData->szParm) + 1;
  133.               pszNewName = pszOldName + strlen( pszOldName ) + 1;
  134.  
  135.               if ( msg==WKFM_CLIENTRENAMED )
  136.                  OutList( hwndDlg,
  137.                           "Client (%s) renamed to (%s)",
  138.                           pszOldName,
  139.                           pszNewName );
  140.               else
  141.                  OutList( hwndDlg,
  142.                           "Client (%s) moved to (%s)",
  143.                           pszOldName,
  144.                           pszNewName );
  145.               }
  146.  
  147.            return( (MRESULT)TRUE );
  148.            }
  149.  
  150.       case WKFM_CLIENTDELETED:
  151.            {
  152.            PWKF_DELETEINFO pMsgData;
  153.  
  154.            pwf = (WFROUTER *)WinQueryWindowULong( hwndDlg, QWL_USER );
  155.  
  156.            /* -------------------------------------------------------------- */
  157.            /* make sure we can access the message data before mucking about  */
  158.            /* -------------------------------------------------------------- */
  159.            if ( WkfAccessMsgData( mp2 ) )
  160.               {
  161.               pMsgData = (PWKF_RENAMEINFO)mp2;
  162.  
  163.               OutList( hwndDlg, "Client (%s) deleted", pMsgData->szParm );
  164.               }
  165.  
  166.            return( (MRESULT)TRUE );
  167.            }
  168.  
  169.       case WKFM_CLIENTCHANGED:
  170.            {
  171.            PWKF_CHANGEINFO pMsgData;
  172.  
  173.            pwf = (WFROUTER *)WinQueryWindowULong( hwndDlg, QWL_USER );
  174.  
  175.            /* -------------------------------------------------------------- */
  176.            /* make sure we can access the message data before mucking about  */
  177.            /* -------------------------------------------------------------- */
  178.            if ( WkfAccessMsgData( mp2 ) )
  179.               {
  180.               pMsgData = (PWKF_CHANGEINFO)mp2;
  181.  
  182.               OutList( hwndDlg,
  183.                        "Client (%s) changed:0x%08x",
  184.                        pMsgData->szParm,
  185.                        pMsgData->flChanged );
  186.               }
  187.  
  188.            return( (MRESULT)TRUE );
  189.            }
  190.  
  191.       case WKFM_INITIALIZE:
  192.            {
  193.            PWKF_INITIALIZEINFO pMsgData;
  194.  
  195.            pwf = (WFROUTER *)WinQueryWindowULong( hwndDlg, QWL_USER );
  196.  
  197.            /* -------------------------------------------------------------- */
  198.            /* make sure we can access the message data before mucking about  */
  199.            /* -------------------------------------------------------------- */
  200.            if ( WkfAccessMsgData( mp2 ) )
  201.               {
  202.               pMsgData = (PWKF_INITIALIZEINFO)mp2;
  203.  
  204.               OutList( hwndDlg,
  205.                        "Client (%s) active",
  206.                        pMsgData->szClientTitle );
  207.               }
  208.  
  209.            return( (MRESULT)TRUE );
  210.            }
  211.  
  212.       case WKFM_APPSTARTNOTIFY:
  213.       case WKFM_APPTERMNOTIFY:
  214.       case WKFM_ACTIONSTART:
  215.       case WKFM_ACTIONEND:
  216.       case WKFM_FILECHANGED:
  217.       case WKFM_FILERENAMED:
  218.       case WKFM_FILEDELETED:
  219.       case WKFM_EXECUTEACTION:
  220.       case WKFM_EXECUTECOMMAND:
  221.       case WKFM_TERMINATE:
  222.            /* -------------------------------------------------------------- */
  223.            /* currently nothing is done with these messages                  */
  224.            /* -------------------------------------------------------------- */
  225.            break;
  226.  
  227.       case WM_COMMAND:
  228.            switch( LOUSHORT( mp1 ))
  229.               {
  230.               case DID_CANCEL:
  231.                    return( MRFROMLONG( FALSE ));
  232.               }
  233.            break;
  234.  
  235.       case WM_SYSCOMMAND:
  236.  
  237.  
  238.            switch( LOUSHORT( mp1 ))
  239.               {
  240.               case SC_CLOSE:
  241.                    pwf = (WFROUTER *)WinQueryWindowULong( hwndDlg, QWL_USER );
  242.                    /* ------------------------------------------------------ */
  243.                    /* de-register with WF/2                                  */
  244.                    /* ------------------------------------------------------ */
  245.                    if ( pwf->hRouter != NULLHANDLE )
  246.                       fSuccess = WkfTerminate( pwf->hRouter, hwndDlg );
  247.                    WinDismissDlg( hwndDlg, 0 );
  248.                    return( MRFROMLONG( TRUE ));
  249.               }
  250.            break;
  251.  
  252.       case WM_INITDLG:
  253.            AddTitle( hwndDlg, szTitle );
  254.  
  255.            WinSetWindowULong( hwndDlg, QWL_USER, LONGFROMMP(mp2) );
  256.            pwf = (WFROUTER *)PVOIDFROMMP(mp2);
  257.  
  258.            /* -------------------------------------------------------------- */
  259.            /* register with WF/2                                             */
  260.            /* -------------------------------------------------------------- */
  261.            pwf->hRouter = WkfInitialize( WKF_CONNECT_PM,
  262.                                          hwndDlg,
  263.                                          WKF_TYPE_UNKNOWN,
  264.                                          szTitle,
  265.                                          WKF_TYPE_PROJECT   |
  266.                                          WKF_TYPE_FOLDER    |
  267.                                          WKF_TYPE_PROFILE   |
  268.                                          WKF_TYPE_MAKEMAKE  |
  269.                                          WKF_TYPE_LIBRARY   |
  270.                                          WKF_TYPE_MONITOR   |
  271.                                          WKF_TYPE_FILETOOL  |
  272.                                          WKF_TYPE_PROJTOOL,
  273.                                          WKF_OPT_NONE );
  274.            return( MRFROMLONG( FALSE ));
  275.       }
  276.  
  277.    return( WinDefDlgProc( hwndDlg, msg, mp1, mp2 ) );
  278.    }
  279.  
  280.  
  281. /*----------------------------------------------------------------------------*/
  282. /*- write a line out to the top of the listbox                               -*/
  283. /*----------------------------------------------------------------------------*/
  284. static void _Optlink OutList( HWND hwndDlg, PSZ pszTemplate, ... )
  285.    {
  286.    char     szBuffer[2048];
  287.    DATETIME dtStamp;
  288.    va_list  va;
  289.  
  290.    DosGetDateTime( &dtStamp );
  291.    sprintf( szBuffer,
  292.             "%02d/%02d/%04d %02d:%02d> ",
  293.             dtStamp.month, dtStamp.day, dtStamp.year,
  294.             dtStamp.hours, dtStamp.minutes );
  295.  
  296.  
  297.    va_start( va, pszTemplate );
  298.    vsprintf( szBuffer+strlen(szBuffer), pszTemplate, va );
  299.    va_end( va );
  300.  
  301.  
  302.    WinInsertLboxItem( WinWindowFromID(hwndDlg, IDC_WFPEEK_LISTBOX),
  303.                       0,
  304.                       szBuffer );
  305.  
  306.    return;
  307.    }
  308.  
  309.  
  310. /*----------------------------------------------------------------------------*/
  311. /*- add this program to the "Windows's List"                                 -*/
  312. /*----------------------------------------------------------------------------*/
  313. static void _Optlink AddTitle( HWND hwndDlg, PSZ pszTitle )
  314.    {
  315.    SWCNTRL scEntry;
  316.    PID     idProcess;
  317.    TID     idThread;
  318.  
  319.  
  320.    WinQueryWindowProcess( hwndDlg, &idProcess, &idThread );
  321.  
  322.    scEntry.hwnd          = hwndDlg,
  323.    scEntry.hwndIcon      = (HWND) 0;
  324.    scEntry.hprog         = (HPROGRAM) 0;
  325.    scEntry.idProcess     = idProcess;
  326.    scEntry.idSession     = 0;
  327.    scEntry.uchVisibility = SWL_VISIBLE;
  328.    scEntry.fbJump        = SWL_JUMPABLE;
  329.  
  330.    strncpy( scEntry.szSwtitle, pszTitle, sizeof(scEntry.szSwtitle) - 1 );
  331.  
  332.    WinAddSwitchEntry( &scEntry );
  333.  
  334.    return;
  335.    }
  336.