home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / winsock / wsock / dialogs.c next >
C/C++ Source or Header  |  1997-10-05  |  10KB  |  261 lines

  1.  
  2. //-----------------------------------------------------------------------------
  3. // This is a part of the Microsoft Source Code Samples. 
  4. // Copyright (C) 1993-1997 Microsoft Corporation.
  5. // All rights reserved. 
  6. //  
  7. // This source code is only intended as a supplement to 
  8. // Microsoft Development Tools and/or WinHelp documentation.
  9. // See these sources for detailed information regarding the 
  10. // Microsoft samples programs.
  11. //-----------------------------------------------------------------------------
  12.  
  13. /****************************************************************************\
  14. *  dialogs.c -- sample program demonstrating Windows Sockets APIs.
  15. *
  16. *  Handles the dialog boxes for the WSOCK sample.
  17. *
  18. ****************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h> /* atoi */
  22. #include <windows.h>
  23. #include "wsock.h"
  24.  
  25. extern u_short portno; /* Which tcp port are we going to use? */
  26. extern char szBuff[80];
  27.  
  28. /****************************************************************************
  29. *
  30. *    FUNCTION: GetTcpPort(HWND, UINT, UINT, LONG)
  31. *
  32. *    PURPOSE: dialog callback procedure.  Allows the user to enter the
  33. *             tcp port number, or a service name.
  34. *
  35. *\***************************************************************************/
  36.  
  37. LRESULT APIENTRY GetTcpPort(
  38.         HWND hDlg,                /* window handle of the dialog box */
  39.         UINT message,             /* type of message                 */
  40.         UINT wParam,              /* message-specific information    */
  41.         LONG lParam)
  42. {
  43.  
  44.    switch (message) {
  45.       case WM_INITDIALOG:                     /* message: initialize dialog box */
  46.         SetFocus( GetDlgItem( hDlg, IDD_EDIT));
  47.         return (TRUE);
  48.  
  49.       case WM_COMMAND: {                    /* message: received a command */
  50.         switch(LOWORD(wParam)) {
  51.             case IDOK:                      /* "OK" box selected?          */
  52.             {
  53.                GetDlgItemText( hDlg, IDD_EDIT, szBuff, 80);
  54.                if ((portno = atoi(szBuff)) == 0)
  55.                   EndDialog( hDlg, 2 );
  56.                else
  57.                   EndDialog( hDlg, 1 );
  58.             }
  59.             break;
  60.  
  61.          case IDCANCEL: /* System menu close command? */
  62.             EndDialog(hDlg, 0);           /* Exits the dialog box        */
  63.             break;
  64.  
  65.          default:
  66.            return FALSE;
  67.          }
  68.                 return (TRUE);
  69.       }
  70.     }   /* switch message */
  71.  
  72.     return (FALSE);                           /* Didn't process a message    */
  73.         UNREFERENCED_PARAMETER(lParam);
  74. }
  75.  
  76. /****************************************************************************
  77. *
  78. *    FUNCTION: GetHostName(HWND, UINT, UINT, LONG)
  79. *
  80. *    PURPOSE: dialog callback procedure.  Allows the user to enter the
  81. *             the host name.  Used for menu item "Check Host Name" and
  82. *             also for menu item "Connect".
  83. *
  84. *\***************************************************************************/
  85.  
  86. LRESULT APIENTRY GetHostName(
  87.         HWND hDlg,                /* window handle of the dialog box */
  88.         UINT message,             /* type of message                 */
  89.         UINT wParam,              /* message-specific information    */
  90.         LONG lParam)
  91. {
  92.  
  93.     switch (message) {
  94.        case WM_INITDIALOG:                     /* message: initialize dialog box */
  95.          SetFocus( GetDlgItem( hDlg, IDD_EDIT));
  96.          return (TRUE);
  97.        case WM_COMMAND: {                     /* message: received a command */
  98.           switch(LOWORD(wParam)) {
  99.             case IDOK:          /* "OK" box selected?        */
  100.              {
  101.               GetDlgItemText( hDlg, IDD_EDIT, szBuff, 80);
  102.               EndDialog(hDlg, TRUE);          /* Exits the dialog box        */
  103.              }
  104.              break;
  105.              case IDCANCEL:     /* System menu close command? */
  106.               EndDialog(hDlg, FALSE);         /* Exits the dialog box        */
  107.               break;
  108.              default:
  109.               return FALSE;
  110.            }
  111.         return (TRUE);
  112.       }
  113.     }   /* switch message */
  114.  
  115.     return (FALSE);                           /* Didn't process a message    */
  116.         UNREFERENCED_PARAMETER(lParam);
  117. }
  118. /****************************************************************************
  119. *
  120. *    FUNCTION: About(HWND, UINT, UINT, LONG)
  121. *
  122. *    PURPOSE: dialog callback procedure for "about" box.
  123. *
  124. *\***************************************************************************/
  125.  
  126. LRESULT APIENTRY About(
  127.         HWND hDlg,                /* window handle of the dialog box */
  128.         UINT message,             /* type of message                 */
  129.         UINT wParam,              /* message-specific information    */
  130.         LONG lParam)
  131. {
  132.      switch (message) {
  133.         case WM_COMMAND:                         /* message: received a command */
  134.           if (LOWORD(wParam) == IDOK             /* "OK" box selected?          */
  135.                 || LOWORD(wParam) == IDCANCEL) { /* System menu close command?  */
  136.                         EndDialog(hDlg, TRUE);   /* Exits the dialog box        */
  137.           return (TRUE);        /* WM_COMMAND */
  138.      }
  139.      break;
  140.    } /* End switch message */
  141.    return (FALSE);                            /* Didn't process a message    */
  142.         UNREFERENCED_PARAMETER(lParam);
  143. }
  144.  
  145. /****************************************************************************
  146. *
  147. *    FUNCTION: DisplayHostEnt(HWND, UINT, UINT, LONG)
  148. *
  149. *    PURPOSE: dialog callback procedure.  Displays the information
  150. *             returned by gethostbyname() (HOSTENT structure).
  151. *
  152. *\***************************************************************************/
  153.  
  154. LRESULT APIENTRY DisplayHostEnt(
  155.         HWND hDlg,                /* window handle of the dialog box */
  156.         UINT message,             /* type of message                 */
  157.         UINT wParam,              /* message-specific information    */
  158.         LONG lParam)
  159. {
  160.  
  161.    DWORD ret;
  162.    switch (message) {
  163.         /*
  164.         *   Initialize dialog box
  165.         */
  166.         case WM_INITDIALOG:
  167.                 {
  168.                 int count = 0;
  169.  
  170.                 SetWindowText( GetDlgItem( hDlg, IDD_HOSTNAME),  (LPCTSTR)phe->h_name);
  171.  
  172.                 while (phe->h_aliases[count] != NULL) {
  173.                         SendDlgItemMessage(hDlg, IDD_LBALIAS, LB_ADDSTRING, 0, (LPARAM)(phe->h_aliases[count]));
  174.                         count++;
  175.                         }
  176.  
  177.                 count = 0;
  178.  
  179.                 /*
  180.                 *   Enumerate all the hosts IP addresses.
  181.                 */
  182.                 while (phe->h_addr_list[count] != NULL) {
  183.                     sprintf( szBuff, "%u.%u.%u.%u",
  184.                     (unsigned char) phe->h_addr_list[count][0],
  185.                     (unsigned char) phe->h_addr_list[count][1],
  186.                     (unsigned char) phe->h_addr_list[count][2],
  187.                     (unsigned char) phe->h_addr_list[count][3]);
  188.  
  189.                     count++;
  190.  
  191.                     /*
  192.                     *   Fill the dialog box..
  193.                     */
  194.                     if ((ret = SendDlgItemMessage(hDlg, IDD_LBADDR, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBuff)) == LB_ERR)
  195.                        MessageBox(hDlg, szBuff, "Couldn't add address..", MB_OK);
  196.                     }
  197.  
  198.                 } /* while( more IP addresses ) */
  199.            return (TRUE);       /* WM_INITDIALOG */
  200.  
  201.         /*
  202.         *   Received a command message
  203.         */
  204.         case WM_COMMAND:
  205.                 if (LOWORD(wParam) == IDOK) {
  206.                    EndDialog(hDlg, TRUE);             /* Exits the dialog box        */
  207.                    return TRUE;
  208.                 }
  209.                 return FALSE;   /* WM_COMMAND */
  210.     }   /* switch message */
  211.  
  212.     return (FALSE);                                   /* Didn't process a message    */
  213.         UNREFERENCED_PARAMETER(lParam);
  214. } /* DisplayHostEnt */
  215.  
  216. /****************************************************************************
  217. *
  218. *    FUNCTION: GetSendString(HWND, UINT, UINT, LONG)
  219. *
  220. *    PURPOSE: dialog callback procedure.  Allows the user to enter a
  221. *             string to be sent to the connected remote host.
  222. *
  223. *\***************************************************************************/
  224.  
  225. LRESULT APIENTRY GetSendString(
  226.         HWND hDlg,                /* window handle of the dialog box */
  227.         UINT message,             /* type of message                 */
  228.         UINT wParam,              /* message-specific information    */
  229.         LONG lParam)
  230. {
  231.  
  232.     switch (message) {
  233.     case WM_INITDIALOG:                     /* message: initialize dialog box */
  234.         SetFocus( GetDlgItem( hDlg, IDD_EDIT));
  235.         return (TRUE);
  236.     case WM_COMMAND: {                     /* message: received a command */
  237.           switch(LOWORD(wParam)) {
  238.             case IDOK:          /* "OK" box selected?        */
  239.              {
  240.  
  241.               /*
  242.               *   Store string in szBuff (global buffer)
  243.               */
  244.               GetDlgItemText( hDlg, IDD_EDIT, szBuff, 80);
  245.               EndDialog(hDlg, TRUE);          /* Exits the dialog box        */
  246.              }
  247.              break;
  248.              case IDCANCEL:     /* System menu close command? */
  249.               EndDialog(hDlg, FALSE);         /* Exits the dialog box        */
  250.               break;
  251.              default:
  252.               return FALSE;
  253.            }
  254.         return (TRUE);
  255.       }
  256.     }   /* switch message */
  257.  
  258.     return (FALSE);                           /* Didn't process a message    */
  259.         UNREFERENCED_PARAMETER(lParam);
  260. } /* GetString */
  261.