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