home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / ras / rasberry / rasberry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  14.5 KB  |  538 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1997  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   rasberry.c
  9. //
  10. //  PURPOSE:  Implement the windows procedure for the main application
  11. //            windows.  
  12. //
  13. //  FUNCTIONS:
  14. //    WndProc      - Processes messages for the main window.
  15. //    MsgCreate    - Handle the WM_CREATE messages for the main window.
  16. //    MsgSize      - Handle the WM_SIZE messages for the main window.
  17. //    MsgCommand   - Handle the WM_COMMAND messages for the main window.
  18. //    MsgDestroy   - Handles the WM_DESTROY message by calling 
  19. //                   PostQuitMessage().
  20. //    CmdPhbkDlg   - Displays entries in default phonebook.
  21. //    CmdStatDlg   - Displays status of the selected connection.
  22. //    CmdRefresh   - Refreshes list of connections in listbox.         
  23. //    CmdHangup    - Hangs up the selected connection.
  24. //    CmdConnNotify- Handles notification messages for connection listbox.
  25. //    CmdExit      - Handles the file exit command by calling destory 
  26. //                   window on the main window.
  27. //
  28. //  COMMENTS:
  29. //
  30. //
  31.  
  32. #include <windows.h>            // required for all Windows applications
  33. #include <windowsx.h>
  34. #ifdef WIN16
  35. #include "win16ext.h"           // required only for win16 applications
  36. #endif
  37.  
  38. #include "globals.h"            // prototypes specific to this application
  39. #include <stdlib.h>
  40. #include <malloc.h>
  41. #include <ras.h>
  42. #include <raserror.h>
  43. #include "rasutil.h"
  44. #include "phbkdlg.h"
  45. #include "statdlg.h"
  46.  
  47. // Main window message table definition.
  48. MSD rgmsd[] =
  49. {
  50.     {WM_CREATE,   MsgCreate},
  51.     {WM_SIZE,     MsgSize},
  52.     {WM_COMMAND,  MsgCommand},
  53.     {WM_DESTROY,  MsgDestroy}
  54. };
  55.  
  56. MSDI msdiMain =
  57. {
  58.     sizeof(rgmsd) / sizeof(MSD),
  59.     rgmsd,
  60.     edwpWindow
  61. };
  62.  
  63.  
  64. // Main window command table definition.
  65. CMD rgcmd[] =
  66. {
  67.     {IDM_PHONEBOOK, CmdPhbkDlg},
  68.     {IDM_STATUS,    CmdStatDlg},
  69.     {IDM_EXIT,      CmdExit},
  70.     {IDM_REFRESH,   CmdRefresh},
  71.     {IDM_HANGUP,    CmdHangUp},
  72.     {IDM_ABOUT,     CmdAbout},
  73.     {IDL_CONN,      CmdConnNotify}
  74. };
  75.  
  76. CMDI cmdiMain =
  77. {
  78.     sizeof(rgcmd) / sizeof(CMD),
  79.     rgcmd,
  80.     edwpWindow
  81. };
  82.  
  83. // Application "globals"
  84. char g_szUserName[UNLEN+1];
  85. char g_szPassword[PWLEN+1];
  86. char g_szDomain[DNLEN+1];
  87.  
  88. LPHRASCONN g_lphRasConn = NULL;
  89.  
  90. // Module specific "globals"  Used when a variable needs to be
  91. // accessed in more than on handler function.
  92.  
  93. HWND hwndConn;  // handle of listbox of connections
  94.  
  95. //
  96. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  97. //
  98. //  PURPOSE:  Processes messages for the main window.
  99. //
  100. //  PARAMETERS:
  101. //    hwnd     - window handle
  102. //    uMessage - message number
  103. //    wparam   - additional information (dependant on message number)
  104. //    lparam   - additional information (dependant on message number)
  105. //
  106. //  RETURN VALUE:
  107. //    The return value depends on the message number.  If the message
  108. //    is implemented in the message dispatch table, the return value is
  109. //    the value returned by the message handling function.  Otherwise,
  110. //    the return value is the value returned by the default window procedure.
  111. //
  112. //  COMMENTS:
  113. //    Call the DispMessage() function with the main window's message dispatch
  114. //    information (msdiMain) and the message specific information.
  115. //
  116.  
  117. LRESULT CALLBACK WndProc(HWND   hwnd, 
  118.                          UINT   uMessage, 
  119.                          WPARAM wparam, 
  120.                          LPARAM lparam)
  121. {
  122.     return DispMessage(&msdiMain, hwnd, uMessage, wparam, lparam);
  123. }
  124.  
  125.  
  126. //
  127. //  FUNCTION: MsgCreate(HWND, UINT, WPARAM, LPARAM)
  128. //
  129. //  PURPOSE: Initialized main window
  130. //
  131. //  PARAMETERS:
  132. //
  133. //    hwnd      - Window handle
  134. //    uMessage  - Message number (Unused)
  135. //    wparam    - Extra data     (Unused)
  136. //    lparam    - Extra data     (Unused)
  137. //
  138. //  RETURN VALUE:
  139. //
  140. //    Always returns 0 - Message handled
  141. //
  142. //  COMMENTS:
  143. //    Creates a listbox the size of the window and fills it
  144. //    with a list of RAS connections.  
  145. //
  146.  
  147. LRESULT MsgCreate(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  148. {
  149.     RECT rc;
  150.     
  151.     GetClientRect( hwnd, &rc );
  152.     
  153.     hwndConn  = CreateWindow( "LISTBOX",
  154.                              "Connections",
  155.                              WS_CHILD | WS_VISIBLE | WS_HSCROLL |
  156.                              WS_VSCROLL | LBS_USETABSTOPS | LBS_NOINTEGRALHEIGHT | LBS_SORT,
  157.                              0,   
  158.                              0,
  159.                              rc.right,
  160.                              rc.bottom,
  161.                              hwnd,
  162.                              (HMENU) IDL_CONN,
  163.                              hInst,
  164.                              NULL );
  165.  
  166.     SendMessage( hwnd, WM_COMMAND, IDM_REFRESH, 0L );
  167.  
  168.     // initialize global variables   
  169.     g_szUserName[0] = '\0';
  170.     g_szPassword[0] = '\0';
  171.     g_szDomain[0] = '*';
  172.     g_szDomain[1] = '\0';
  173.     
  174.     return 0;
  175. }
  176.  
  177.  
  178. //
  179. //  FUNCTION: MsgSize(HWND, UINT, WPARAM, LPARAM)
  180. //
  181. //  PURPOSE: Resize listbox to client area size
  182. //
  183. //  PARAMETERS:
  184. //
  185. //    hwnd      - Window handle
  186. //    uMessage  - Message number (Unused)
  187. //    wparam    - Extra data     (Unused)
  188. //    lparam    - Extra data     (Unused)
  189. //
  190. //  RETURN VALUE:
  191. //
  192. //    Always returns 0 - Message handled
  193. //
  194. //  COMMENTS:
  195. //
  196. //
  197.  
  198. LRESULT MsgSize(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  199. {
  200.     MoveWindow(hwndConn, 0, 0, LOWORD(lparam), HIWORD(lparam), TRUE);
  201.     return 0;
  202. }
  203.  
  204.  
  205.  
  206. //
  207. //  FUNCTION: MsgCommand(HWND, UINT, WPARAM, LPARAM)
  208. //
  209. //  PURPOSE: Handle the WM_COMMAND messages for the main window.
  210. //
  211. //  PARAMETERS:
  212. //    hwnd     - window handle
  213. //    uMessage - WM_COMMAND (Unused)
  214. //    GET_WM_COMMAND_ID(wparam, lparam)   - Command identifier
  215. //    GET_WM_COMMAND_HWND(wparam, lparam) - Control handle
  216. //
  217. //  RETURN VALUE:
  218. //    The return value depends on the message number.  If the message
  219. //    is implemented in the message dispatch table, the return value is
  220. //    the value returned by the message handling function.  Otherwise,
  221. //    the return value is the value returned by the default window procedure.
  222. //
  223. //  COMMENTS:
  224. //    Call the DispCommand() function with the main window's command dispatch
  225. //    information (cmdiMain) and the command specific information.
  226. //
  227.  
  228. LRESULT MsgCommand(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  229. {
  230.     return DispCommand(&cmdiMain, hwnd, wparam, lparam);
  231. }
  232.  
  233.  
  234. //
  235. //  FUNCTION: MsgDestroy(HWND, UINT, WPARAM, LPARAM)
  236. //
  237. //  PURPOSE: Calls PostQuitMessage().
  238. //
  239. //  PARAMETERS:
  240. //
  241. //    hwnd      - Window handle  (Unused)
  242. //    uMessage  - Message number (Unused)
  243. //    wparam    - Extra data     (Unused)
  244. //    lparam    - Extra data     (Unused)
  245. //
  246. //  RETURN VALUE:
  247. //
  248. //    Always returns 0 - Message handled
  249. //
  250. //  COMMENTS:
  251. //
  252. //
  253.  
  254. LRESULT MsgDestroy(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  255. {
  256.     if ( g_lphRasConn != NULL )
  257.     {
  258.         _ffree((LPVOID) g_lphRasConn );
  259.         g_lphRasConn = NULL;
  260.     }
  261.  
  262.     PostQuitMessage(0);
  263. #ifdef WIN32
  264.     Sleep(3000);  // let RAS state machine clean up
  265. #endif
  266.     return 0;
  267. }
  268.  
  269.  
  270.  
  271. //
  272. //  FUNCTION: CmdPhbkDlg(HWND, WORD, WORD, HWND)
  273. //
  274. //  PURPOSE: Displays the "PhbkDlg" dialog box
  275. //
  276. //  PARAMETERS:
  277. //    hwnd      - Window handle
  278. //    wCommand  - IDM_ENTRY (unused)
  279. //    wNotify   - Notification number (unused)
  280. //    hwndCtrl  - NULL (unused)
  281. //
  282. //  RETURN VALUE:
  283. //
  284. //    Always returns 0 - Message handled
  285. //
  286. //  COMMENTS:
  287. //    To process the IDM_ENTRY message, call DialogBox() to display the
  288. //    phbkdlg dialog box.
  289.  
  290. LRESULT CmdPhbkDlg(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  291. {
  292.     DialogBox(hInst, "PhbkDlgBox", hwnd, (DLGPROC)PhbkDlg);
  293.     SendMessage( hwnd, WM_COMMAND, IDM_REFRESH, 0L );
  294.  
  295.     return 0;
  296. }
  297.  
  298. //
  299. //  FUNCTION: CmdStatDlg(HWND, WORD, WORD, HWND)
  300. //
  301. //  PURPOSE: Displays the "StatDlg" dialog box
  302. //
  303. //  PARAMETERS:
  304. //    hwnd      - Window handle
  305. //    wCommand  - IDM_STATDLG (unused)
  306. //    wNotify   - Notification number (unused)
  307. //    hwndCtrl  - NULL (unused)
  308. //
  309. //  RETURN VALUE:
  310. //
  311. //    Always returns 0 - Message handled
  312. //
  313. //  COMMENTS:
  314. //    To process the IDM_STATDLG message, call DialogBox() to display the
  315. //    statdlg dialog box.
  316.  
  317. LRESULT CmdStatDlg(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  318. {
  319.     DialogBox(hInst, "StatDlgBox", hwnd, (DLGPROC)StatDlg);
  320.     SendMessage( hwnd, WM_COMMAND, IDM_REFRESH, 0L );
  321.     return 0;
  322. }
  323.  
  324.  
  325.  
  326. //
  327. //  FUNCTION: CmdRefesh(HWND, WORD, WORD, HWND)
  328. //
  329. //  PURPOSE: Refreshes the connection list
  330. //
  331. //  PARAMETERS:
  332. //    hwnd     - The window.
  333. //    wCommand - IDM_REFRESH (unused)
  334. //    wNotify  - Notification number (unused)
  335. //    hwndCtrl - NULL (unused)
  336. //
  337. //  RETURN VALUE:
  338. //    Always returns 0 - command handled.
  339. //
  340. //  COMMENTS:
  341. //     Calls RasEnumConnections to get a list of current connections
  342. //
  343.  
  344. LRESULT CmdRefresh(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  345. {
  346.     LPRASCONN  lpRasConn = NULL;
  347.     LPRASCONN  lpTemp = NULL;
  348.     LPHRASCONN lphTemp = NULL;
  349.     DWORD cbBuf = 0;
  350.     DWORD cConn = 0;
  351.     DWORD dwRet = 0;
  352.     UINT  ndx;
  353.     char  szMessage[256];
  354.     HMENU hmenu = GetMenu( hwnd );
  355.  
  356.     // remove any connections currently in listbox                                 
  357.     SendMessage( hwndConn, LB_RESETCONTENT, 0, 0L );
  358.  
  359.     if ( g_lphRasConn != NULL )
  360.     {
  361.         _ffree( (LPVOID) g_lphRasConn );
  362.         g_lphRasConn = NULL;
  363.     }
  364.  
  365.     // enumerate connections
  366.     cbBuf = sizeof(RASCONN);
  367.     if ( ((lpRasConn = ( LPRASCONN ) _fmalloc((UINT)cbBuf)) != NULL ) &&
  368.          ((g_lphRasConn = ( LPHRASCONN ) _fmalloc(sizeof(HRASCONN))) != NULL ) )
  369.     {            
  370.         lpRasConn->dwSize = sizeof( RASCONN );                      
  371.         dwRet = RasEnumConnections( lpRasConn, &cbBuf, &cConn );
  372.         if ( dwRet == ERROR_BUFFER_TOO_SMALL )
  373.         {
  374.             if ( (lpTemp = ( LPRASCONN ) _frealloc(lpRasConn, (UINT)cbBuf) ) != NULL )
  375.             {
  376.                 lpRasConn = lpTemp;
  377.                 dwRet = RasEnumConnections( lpRasConn, &cbBuf, &cConn );
  378.                 
  379.                 if ( NULL != (lphTemp = ( LPHRASCONN )
  380.                               _frealloc((LPVOID)g_lphRasConn, (UINT)(sizeof(HRASCONN)*cConn))) )
  381.                     g_lphRasConn = lphTemp;
  382.                 else
  383.                     dwRet = ERROR_NOT_ENOUGH_MEMORY;
  384.  
  385.             }
  386.             else
  387.             {
  388.                 dwRet = ERROR_NOT_ENOUGH_MEMORY;
  389.             }
  390.         }
  391.         else if ( dwRet != 0 ) // other error
  392.         {
  393.             if ( RasGetErrorString( (UINT)dwRet, szMessage, 256 ) != 0 )
  394.                 wsprintf( (LPSTR)szMessage, (LPSTR)"Undefined RAS Dial Error." );
  395.  
  396.             MessageBox(hwnd, szMessage, szAppName, MB_OK | MB_ICONSTOP );                            
  397.         }
  398.  
  399.         if ( dwRet == 0 )  // No errors, so populate listbox
  400.         {
  401.             for ( ndx = 0; ndx < cConn; ndx++ ) 
  402.             {
  403.                 g_lphRasConn[ndx] = lpRasConn[ndx].hrasconn;
  404.                 SendMessage( hwndConn,
  405.                              LB_ADDSTRING,
  406.                              0,
  407.                              (LPARAM)(LPCSTR) lpRasConn[ndx].szEntryName);
  408.             }
  409.  
  410.             // update states of menu items
  411.             if ( ndx > 0 ) // at least one item was added
  412.             {             
  413.                 SendMessage( hwndConn, LB_SETCURSEL, 0, 1L );
  414.                 EnableMenuItem( hmenu, IDM_STATUS, MF_ENABLED );
  415.                 EnableMenuItem( hmenu, IDM_HANGUP, MF_ENABLED );
  416.             }
  417.             else 
  418.             {
  419.                 EnableMenuItem( hmenu, IDM_STATUS, MF_GRAYED );
  420.                 EnableMenuItem( hmenu, IDM_HANGUP, MF_GRAYED );
  421.             }
  422.         }
  423.         else
  424.         {
  425.             EnableMenuItem( hmenu, IDM_STATUS, MF_GRAYED );
  426.             EnableMenuItem( hmenu, IDM_HANGUP, MF_GRAYED );
  427.         }
  428.  
  429.         _ffree( lpRasConn );
  430.     }
  431.     else
  432.     {
  433.         if ( g_lphRasConn != NULL )
  434.         {
  435.             _ffree((LPVOID) g_lphRasConn );
  436.             g_lphRasConn = NULL;
  437.         }
  438.  
  439.         if ( lpRasConn != NULL )
  440.         {
  441.             _ffree( lpRasConn );
  442.         }
  443.     }                
  444.  
  445.  
  446.     return 0;
  447. }
  448.  
  449. //
  450. //  FUNCTION: CmdHangUp(HWND, WORD, WORD, HWND)
  451. //
  452. //  PURPOSE: Hangs up the selected connection
  453. //
  454. //  PARAMETERS:
  455. //    hwnd     - The window.
  456. //    wCommand - IDM_HANGUP (unused)
  457. //    wNotify  - Notification number (unused)
  458. //    hwndCtrl - NULL (unused)
  459. //
  460. //  RETURN VALUE:
  461. //    Always returns 0 - command handled.
  462. //
  463. //  COMMENTS:
  464. //
  465. //
  466.  
  467. LRESULT CmdHangUp(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  468. {
  469.     CHAR  szBuf[256];
  470.     CHAR  szBuf2[256];
  471.     UINT  ndx;
  472.  
  473.     // get the connection text from previous window
  474.     ndx = (UINT) SendMessage( hwndConn, LB_GETCURSEL, 0,  0L );
  475.     SendMessage( hwndConn, LB_GETTEXT, ndx, (LPARAM)(LPSTR)szBuf );
  476.  
  477.     wsprintf( (LPSTR) szBuf2, "HangUp %s?", (LPSTR) szBuf );
  478.     if ( IDYES == MessageBox( hwnd, (LPSTR)szBuf2, szAppName, MB_ICONQUESTION | MB_YESNO ) )
  479.     {
  480.         RasHangUp( g_lphRasConn[ndx] );
  481.  
  482.         SendMessage( hwnd, WM_COMMAND, IDM_REFRESH, 0L );
  483.     }
  484.     return 0;
  485. }
  486.  
  487. //
  488. //  FUNCTION: CmdConnNotify(HWND, WORD, WORD, HWND)
  489. //
  490. //  PURPOSE: notfication from Connection Listbox
  491. //
  492. //  PARAMETERS:
  493. //    hwnd - The window handling the command.
  494. //    wCommand - The command to be handled (unused).
  495. //    wNotify  - The notification code to be handled (LBN_DBLCLK).
  496. //    hwndCtrl - NULL (unused).
  497. //
  498. //  RETURN VALUE:
  499. //    0 if message is processed.
  500. //
  501. //  COMMENTS:
  502. //    Catches DBL_CLK to cause connection status dialog to appear
  503. //
  504.  
  505. LRESULT CmdConnNotify(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  506. {
  507.     if ( wNotify == LBN_DBLCLK )
  508.     {
  509.         SendMessage( hwnd, WM_COMMAND, IDM_STATUS, 0 );
  510.         return 0;
  511.     }
  512.     return 1;
  513. }
  514.  
  515. //
  516. //  FUNCTION: CmdExit(HWND, WORD, WORD, HWND)
  517. //
  518. //  PURPOSE: Exit the application.
  519. //
  520. //  PARAMETERS:
  521. //    hwnd     - The window.
  522. //    wCommand - IDM_EXIT
  523. //    wNotify  - Notification number (unused)
  524. //    hwndCtrl - NULL (unused)
  525. //
  526. //  RETURN VALUE:
  527. //    Always returns 0 - command handled.
  528. //
  529. //  COMMENTS:
  530. //
  531. //
  532.  
  533. LRESULT CmdExit(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  534. {
  535.     DestroyWindow(hwnd);
  536.     return 0;
  537. }
  538.