home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / COPYTE.ZIP / CDTEST1.C next >
C/C++ Source or Header  |  1993-09-09  |  5KB  |  140 lines

  1.  
  2. /*******************************************************************************
  3. *                                                                                                         *
  4. *   CDTEST1: First half of a pair of programs demonstrating how to transfer     *
  5. *      data between two consenting adult programs under Windows NT. These         *
  6. *      programs use the WM_COPYDATA message to directly pass data to and from     *
  7. *      each program.                                                                                 *
  8. *                                                                                                         *
  9. *         9/9/93 by Mark Gamber                                                                     *
  10. *                                                                                                         *
  11. *******************************************************************************/
  12.  
  13. #include "windows.h"
  14.  
  15. BOOL WINAPI MainDlgProc( HWND, UINT, WPARAM, LPARAM );       //  No include file
  16.  
  17. // -----------------------------------------------------------------------------
  18.  
  19. HINSTANCE hInst;
  20.  
  21. // -----------------------------------------------------------------------------
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmd, int nShow )
  24. {
  25.     hInst = hInstance;                     //  Save instance and start dialog box
  26.     DialogBox( hInstance, MAKEINTRESOURCE( 10000 ), NULL, MainDlgProc );
  27.     return( FALSE );
  28. }
  29.  
  30. // ---- Main Window (Dialog) of App #1 -----------------------------------------
  31.  
  32. BOOL WINAPI MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  33. {
  34.     switch( msg )
  35.     {
  36.         case WM_INITDIALOG:                                                //  And it was...
  37.         {
  38.             char szWinName[ 32 ];
  39.             HWND hOther;
  40.  
  41.             LoadString( hInst, 100, szWinName, 32 );          //  Title of other half
  42.             hOther = FindWindow( (LPSTR)0, szWinName );         //  Locate other half
  43.             if( ! hOther )                                          //  If window not found...
  44.             {
  45.                 LoadString( hInst, 101, szWinName, 32 );
  46.                 WinExec( szWinName, SW_SHOW );            //  Attempt to start program
  47.                 Sleep( 500 );                                                //  Wait a bit...
  48.                 LoadString( hInst, 100, szWinName, 32 );        //  Now try the window
  49.                 hOther = FindWindow( (LPSTR)0, szWinName );
  50.                 if( ! hOther )
  51.                 {                                              //  If still not there, forget it
  52.                     EndDialog( hDlg, FALSE );
  53.                     return( FALSE );
  54.                 }
  55.             }
  56.             SendMessage( GetDlgItem( hDlg, 102 ), EM_LIMITTEXT, 127, 0 );
  57.             return( TRUE );
  58.         }
  59.  
  60.         case WM_COPYDATA:                                 //  Incoming data from another app
  61.         {
  62.             COPYDATASTRUCT *cds;
  63.             char str[ 128 ];
  64.  
  65.            cds = (COPYDATASTRUCT *)lParam;              //  Point structure to address
  66.             if( cds->dwData == 1 )                                      //  Erase command?
  67.             {
  68.                 SetDlgItemText( hDlg, 103, "" );                   //  If so, easy enough
  69.                 return( TRUE );
  70.             }
  71.             if( cds->dwData == 0 )                                      //  Incoming text?
  72.             {                                                       //  Copy text to local buffer
  73.                 memcpy( str, cds->lpData, cds->cbData );
  74.                 str[ cds->cbData ] = '\0';                                //  Append a NULL
  75.                 SetDlgItemText( hDlg, 103, str );            //  Display text and exit
  76.                 return( TRUE );
  77.             }
  78.             return( FALSE );                                 //  Unknown command, bad exit
  79.         }
  80.  
  81.         case WM_COMMAND:
  82.         {
  83.             if( wParam == IDCANCEL )                      //  Cancel, of course, exits
  84.             {
  85.                 EndDialog( hDlg, TRUE );
  86.                 return( TRUE );
  87.             }
  88.             if( wParam == 100 )                         //  Copy from here to there...
  89.             {
  90.                 char szWinName[ 32 ];
  91.                 char szText[ 128 ];
  92.                 LONG lResult;
  93.                 HWND hOther;
  94.             COPYDATASTRUCT cds;
  95.                                                                      //  Get text in edit control
  96.                 if( ! GetDlgItemText( hDlg, 102, szText, 128 ) )
  97.                     break;                                   //  If none, what are you doing?
  98.                                                             //  Get name of other app's window
  99.                 LoadString( hInst, 100, szWinName, 32 );
  100.                 hOther = FindWindow( (LPSTR)0, szWinName );
  101.                 if( ! hOther )
  102.                     break;                                    //  Find other window or die
  103.  
  104.                 cds.dwData = 0;                                          //  Text command
  105.                 cds.lpData = (LPSTR)szText;                             //  Point to text
  106.                 cds.cbData = lstrlen( szText );                       //  Length of string
  107.                                                           //  Send to the other app.
  108.                 lResult = SendMessage( hOther, WM_COPYDATA, (WPARAM)hDlg,
  109.                                               (LPARAM)(COPYDATASTRUCT *)&cds );
  110.                 break;
  111.             }
  112.             if( wParam == 101 )            //  Tell other app to erase it's control
  113.             {
  114.                 char szWinName[ 32 ];
  115.                 char szText[ 128 ];
  116.                 LONG lResult;
  117.                 HWND hOther;
  118.             COPYDATASTRUCT cds;
  119.                                                                    //  Find other app's window
  120.                 LoadString( hInst, 100, szWinName, 32 );
  121.                 hOther = FindWindow( (LPSTR)0, szWinName );
  122.                 if( ! hOther )
  123.                     break;
  124.  
  125.                 cds.dwData = 1;                                        //  Erase command
  126.                 cds.lpData = (LPSTR)0;                                //  The rest is blank
  127.                 cds.cbData = 0;                                          //  Send to other app
  128.                 lResult = SendMessage( hOther, WM_COPYDATA, (WPARAM)hDlg,
  129.                                               (LPARAM)(COPYDATASTRUCT *)&cds );
  130.                 break;
  131.             }
  132.             break;
  133.         }
  134.  
  135.     }
  136.     return( FALSE );
  137. }
  138.  
  139.  
  140.