RASBERRY.C
Upload User: bangxh
Upload Date: 2007-01-31
Package Size: 42235k
Code Size: 15k
Category:

Windows Develop

Development Platform:

Visual C++

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