home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 6.ddi / DDEML31.ZIP / DDESRVR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  11.2 KB  |  376 lines

  1. /* Borland C++ - (C) Copyright 1992 by Borland International               */
  2.  
  3. /***************************************************************************
  4.  
  5.       Program Name      DDESrvr.c
  6.  
  7.       Purpose           A simple DDE client application, which communicates
  8.                         to a DDE server using the new 3.1 api DDEML calls.
  9.  
  10.                         To use this program, build DDEClnt and DDESrvr. There
  11.                         are project files for this.
  12.  
  13. ****************************************************************************/
  14.  
  15. #define STRICT
  16.  
  17. #include <windows.h>
  18. #include <ddeml.h>
  19. #include <dde.h>
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. #include "ddesrvr.h"
  25.  
  26.  
  27. HANDLE         hInst;                  /*  Current instance of application */
  28. HWND           hWnd;                   /*  Handle of Main window           */
  29.  
  30. short          xScreen;                /* Screen metrics                   */
  31. short          yScreen;                /*  ...                             */
  32. short          yFullScreen;            /*  ...                             */
  33. short          xFrame;                 /*  ...                             */
  34. short          yMenu;                  /*  ...                             */
  35. TEXTMETRIC     tm;                     /* Text metrics                     */
  36. short          cxChar;                 /* Character metrics                */
  37. short          cyChar;                 /*  ...                             */
  38.  
  39. char           szScreenText[10][80];   /* Contains 10 lines of display data*/
  40. short          cCurrentLine;           /* Index into szScreenText          */
  41. short          cTotalLines;            /* Total lines in szScreenText      */
  42.  
  43. /*
  44.          The DDE variables
  45. */
  46.  
  47. DWORD          idInst = 0L;            /*  Instance of app for DDEML       */
  48. FARPROC        lpDdeProc;              /*  DDE callback function           */
  49. HSZ            hszService;
  50. HSZ            hszTopic;
  51. HSZ            hszItem;
  52. HCONV          hConvApp = NULL;        /*Handle of established conversation*/
  53. char           szDDEData[80];          /*  Local receive buffer            */
  54. char           szDDEString[80];        /*  Local send buffer               */
  55. int            iServerCount = 0;       /*  Send message counter            */
  56. char           tbuf[5];                /*  Temporary buffer for count      */
  57.  
  58. char szAppName[] = "DDEServerApplication";
  59.  
  60.  
  61. /***************************************************************************/
  62.  
  63. #pragma argsused
  64. int PASCAL WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  65.                      LPSTR lpszCmdLine, int nCmdShow )
  66. {
  67.     MSG         msg;
  68.  
  69.  
  70.    if ( !hPrevInstance )               /* Other instances of app running?  */
  71.       if ( !InitApplication ( hInstance ) ) /* Initialize shared things    */
  72.          return ( FALSE );             /* Exits if unable to initialize    */
  73.  
  74.    if ( !InitInstance ( hInstance, nCmdShow ) )
  75.       return ( FALSE );
  76.  
  77.     while ( GetMessage ( &msg, NULL, NULL, NULL ) )
  78.     {
  79.         TranslateMessage ( &msg );
  80.         DispatchMessage ( &msg );
  81.     }
  82.  
  83.    DdeUninitialize ( idInst );
  84.  
  85.     return ( msg.wParam );
  86. }
  87.  
  88. /***************************************************************************/
  89.  
  90. BOOL FAR PASCAL InitApplication ( HANDLE hInstance )
  91. {
  92.     WNDCLASS    wc;
  93.  
  94.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  95.    wc.lpfnWndProc   = MainWndProc;
  96.    wc.cbClsExtra    = 0;
  97.    wc.cbWndExtra    = 0;
  98.    wc.hInstance     = hInstance;
  99.    wc.hIcon         = LoadIcon ( hInstance, "DDEServerIcon" );
  100.    wc.hCursor       = LoadCursor ( NULL, IDC_ARROW );
  101.    wc.hbrBackground = GetStockObject ( WHITE_BRUSH );
  102.    wc.lpszMenuName  = "DDEServerMenu";
  103.    wc.lpszClassName = szAppName;
  104.  
  105.    if ( !RegisterClass ( &wc ) )
  106.       return ( FALSE );
  107.  
  108.    return ( TRUE );
  109. }
  110.  
  111. /***************************************************************************/
  112.  
  113. BOOL InitInstance ( HANDLE hInstance, short int nCmdShow )
  114. {
  115.    hInst = hInstance;
  116.  
  117.    xScreen     = GetSystemMetrics ( SM_CXSCREEN );
  118.    yScreen     = GetSystemMetrics ( SM_CYSCREEN );
  119.  
  120.     hWnd = CreateWindow ( szAppName,
  121.                          "DDE Server Window",
  122.                          WS_OVERLAPPEDWINDOW,
  123.                          190,                   /* These co-ordinates look */
  124.                          yScreen / 2 - 20,      /* good on a VGA monitor   */
  125.                          xScreen - 200,         /* running in 640x480.  No */
  126.                          yScreen / 2 - 50,      /* combination was tried.  */
  127.                          NULL,
  128.                          NULL,
  129.                          hInstance,
  130.                          NULL );
  131.  
  132. /*
  133.       If window could not be created, return "failure"
  134. */
  135.  
  136.    if ( !hWnd )
  137.       return ( FALSE );
  138.  
  139. /*
  140.       Make the window visible; update its client area; and return "success"
  141. */
  142.  
  143.    ShowWindow ( hWnd, nCmdShow );      /* Show the window                  */
  144.    UpdateWindow ( hWnd );              /* Sends WM_PAINT message           */
  145.    return ( TRUE );              /* Returns the value from PostQuitMessage */
  146.  
  147. }
  148.  
  149. /***************************************************************************/
  150.  
  151. LRESULT CALLBACK MainWndProc ( HWND hWnd, UINT message,
  152.                                WPARAM wParam, LPARAM lParam )
  153. {
  154.    HDC            hDC;
  155.    PAINTSTRUCT    ps;
  156.    DLGPROC        dlgProcAbout;
  157.    int            i;
  158.    int            j;
  159.    short          y;
  160.  
  161.  
  162.    switch ( message )
  163.    {
  164.       case WM_CREATE:
  165.          hDC = GetDC ( hWnd );
  166.  
  167.          GetTextMetrics ( hDC, &tm );
  168.          cxChar = tm.tmAveCharWidth;
  169.          cyChar = tm.tmHeight + tm.tmExternalLeading;
  170.  
  171.          ReleaseDC ( hWnd, hDC );
  172.  
  173.          lpDdeProc = MakeProcInstance ( (FARPROC) DDECallback, hInst );
  174.          if ( DdeInitialize ( (LPDWORD)&idInst, (PFNCALLBACK)lpDdeProc,
  175.                               APPCLASS_STANDARD, 0L ) )
  176.          {
  177.             HandleOutput ( "DDE initialization failure." );
  178.             return ( FALSE );
  179.          }
  180.          else
  181.          {
  182.             hszService = DdeCreateStringHandle ( idInst, "Borland", CP_WINANSI );
  183.             hszTopic = DdeCreateStringHandle ( idInst, "DDEExample", CP_WINANSI );
  184.             hszItem = DdeCreateStringHandle ( idInst, "DDEData", CP_WINANSI );
  185.  
  186.             DdeNameService ( idInst, hszService, (HSZ) NULL, DNS_REGISTER );
  187.          }
  188.  
  189.          cCurrentLine = 0;
  190.          cTotalLines = 0;
  191.  
  192.          strcpy ( szDDEString, "Server application message number:  " );
  193.          break;
  194.  
  195.       case WM_COMMAND:
  196.          switch ( wParam )
  197.          {
  198.             case IDM_EXIT:
  199.                DestroyWindow ( hWnd );
  200.                break;
  201.  
  202.             case IDM_SHOW_CONNECTIONS:
  203.                if ( hConvApp != NULL )
  204.                {
  205.                   HandleOutput ( "Connection established." );
  206.                }
  207.                else
  208.                {
  209.                   HandleOutput ( "No connection established." );
  210.                }
  211.  
  212.                break;
  213.  
  214.             case IDM_ABOUT:
  215.                dlgProcAbout = (DLGPROC) MakeProcInstance ( (FARPROC)About, hInst );
  216.                DialogBox ( hInst, "AboutBox", hWnd, dlgProcAbout );
  217.                FreeProcInstance ( (FARPROC) dlgProcAbout );
  218.                break;
  219.  
  220.             default:
  221.                return ( DefWindowProc ( hWnd, message, wParam, lParam ) );
  222.          }
  223.          break;
  224.  
  225.       case WM_PAINT:
  226.          hDC = BeginPaint ( hWnd, &ps );
  227.  
  228.          y = 0;
  229.  
  230.          for ( i = 0; i < cTotalLines; i ++ )
  231.          {
  232.             if ( cTotalLines == 8 )
  233.                j = ( (cCurrentLine + 1 + i) % 9 );
  234.             else
  235.                j = i;
  236.  
  237.             TextOut ( hDC, 0, y, (LPSTR)(szScreenText[j]),
  238.                                  lstrlen ( szScreenText[j] ) );
  239.             y = y + cyChar;
  240.          }
  241.  
  242.          EndPaint ( hWnd, &ps );
  243.          break;
  244.  
  245.       case WM_DESTROY:
  246.          if ( hConvApp != NULL )
  247.          {
  248.             DdeDisconnect ( hConvApp );
  249.             hConvApp = NULL;
  250.          }
  251.  
  252.          DdeFreeStringHandle ( idInst, hszService );
  253.          DdeFreeStringHandle ( idInst, hszTopic );
  254.          DdeFreeStringHandle ( idInst, hszItem );
  255.  
  256.          FreeProcInstance ( lpDdeProc );
  257.  
  258.          PostQuitMessage ( 0 );
  259.          break;
  260.  
  261.       default:
  262.          return ( DefWindowProc ( hWnd, message, wParam, lParam ) );
  263.    }
  264.  
  265.    return ( FALSE );
  266. }
  267.  
  268. /***************************************************************************/
  269.  
  270. #pragma argsused
  271. BOOL CALLBACK About ( HWND hDlg, UINT message,
  272.                         WPARAM wParam, LPARAM lParam )
  273. {
  274.    switch  ( message )
  275.    {
  276.       case WM_INITDIALOG:           /* message: initialize dialog box */
  277.          return ( TRUE );
  278.  
  279.       case WM_COMMAND:              /* message: received a command */
  280.          if ( wParam == IDOK || wParam == IDCANCEL )
  281.          {
  282.             EndDialog ( hDlg, TRUE );          /* Exits the dialog box         */
  283.             return ( TRUE );
  284.          }
  285.          break;
  286.    }
  287.    return ( FALSE );                  /* Didn't process a message    */
  288. }
  289.  
  290. /***************************************************************************/
  291.  
  292. #pragma argsused
  293. HDDEDATA EXPENTRY DDECallback ( WORD wType, WORD wFmt, HCONV hConv, HSZ hsz1,
  294.                                 HSZ hsz2, HDDEDATA hData, DWORD dwData1,
  295.                                 DWORD dwData2 )
  296. {
  297.    switch ( wType )
  298.    {
  299.       case XTYP_CONNECT:
  300.          if ( hsz2 == hszService )
  301.          {
  302.             return ( (HDDEDATA) TRUE );
  303.          }
  304.          else
  305.          {
  306.             HandleOutput ( "XTYP_CONNECT: hsz2 != hszService" );
  307.             return ( (HDDEDATA) FALSE );
  308.          }
  309.  
  310.       case XTYP_REQUEST:
  311.          iServerCount ++;
  312.          sprintf ( tbuf, "%3d.", iServerCount );
  313.          strncpy ( &szDDEString[36], tbuf, 5 );
  314.  
  315.          hData = DdeCreateDataHandle ( idInst, &szDDEString,
  316.                   sizeof ( szDDEString ), 0L, hszItem, wFmt, 0 );
  317.  
  318.          if ( hData != NULL )
  319.          {
  320.             HandleOutput ( szDDEString );
  321.             return ( hData );
  322.          }
  323.          else
  324.          {
  325.             HandleOutput ( "Could not create data handle." );
  326.             return ( NULL );
  327.          }
  328.  
  329.       case XTYP_EXECUTE:
  330.          break;
  331.  
  332.       case XTYP_POKE:
  333.          if ( hsz1 == hszTopic )
  334.          {
  335.             DdeGetData ( hData, (LPBYTE) szDDEData, 80L, 0L );
  336.  
  337.             if ( szDDEData != NULL )
  338.             {
  339.                HandleOutput ( szDDEData );
  340.                return ( (HDDEDATA) DDE_FACK );
  341.             }
  342.          }
  343.          else
  344.             return ( (HDDEDATA) NULL );
  345.          break;
  346.  
  347.       case XTYP_CONNECT_CONFIRM:
  348.          HandleOutput ( "DDE connection confirmed." );
  349.          hConvApp = hConv;
  350.          break;
  351.  
  352.       case XTYP_DISCONNECT:
  353.          hConvApp = NULL;
  354.          HandleOutput ( "The client has disconnected." );
  355.          break;
  356.  
  357.       case XTYP_ERROR:
  358.          break;
  359.    }
  360.    
  361.    return ( (HDDEDATA) NULL );
  362. }
  363.  
  364. /***************************************************************************/
  365.  
  366. void HandleOutput ( char *szOutputString )
  367. {
  368.    strcpy ( szScreenText[cCurrentLine], szOutputString );
  369.    cCurrentLine = ( cCurrentLine + 1 ) % 9;
  370.    if ( cTotalLines < 8 )
  371.       cTotalLines++;
  372.  
  373.    InvalidateRect ( hWnd, NULL, TRUE );
  374.    UpdateWindow ( hWnd );
  375. }
  376.