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 / dialdlg.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  9KB  |  311 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:   dialdlg.c
  9. //
  10. //  PURPOSE:   Displays the "DialDlg" dialog box
  11. //
  12. //  FUNCTIONS:
  13. //    DialDlg           - Processes messages for "DialDlg" dialog box.
  14. //    MsgDialDlgInit    - To initialize the dialdlg box and make the
  15. //                        RasDial call.
  16. //    MsgDialDlgEvent   - Process WM_RASDIALEVENT message.
  17. //    MsgDialDlgCommand - Process WM_COMMAND message.
  18. //    CmdDialDlgCancel  - Free the dialdlg box and related data.
  19. //    RasDialFunc       - RasDial callback function for Win32.
  20. //
  21. //  COMMENTS:
  22. //    Dialog box to display status of connection attempt
  23. //
  24. //
  25.  
  26. #include <windows.h>            // required for all Windows applications
  27. #include <windowsx.h>
  28. #ifdef WIN16
  29. #include "win16ext.h"           // required only for win16 applications
  30. #endif
  31. #include "globals.h"            // prototypes specific to this application
  32. #include <ras.h>
  33. #include <raserror.h>
  34. #include "rasutil.h"
  35. #include "dialdlg.h"
  36. #include "phbkdlg.h"
  37.  
  38. LRESULT MsgDialDlgInit(HWND, UINT, WPARAM, LPARAM);
  39. LRESULT MsgDialDlgEvent(HWND, UINT, WPARAM, LPARAM);
  40. LRESULT MsgDialDlgCommand(HWND, UINT, WPARAM, LPARAM);
  41. LRESULT CmdDialDlgCancel(HWND, WORD, WORD, HWND);
  42.  
  43.  
  44.  
  45. #ifdef WIN32
  46. VOID WINAPI RasDialFunc( UINT unMsg, RASCONNSTATE rasconnstate, DWORD dwError );
  47. #endif
  48.  
  49. // DialDlg dialog message table definition.
  50. MSD rgmsdDialDlg[] =
  51. {
  52.     {WM_RASDIALEVENT, MsgDialDlgEvent},
  53.     {WM_COMMAND,      MsgDialDlgCommand},
  54.     {WM_INITDIALOG,   MsgDialDlgInit}
  55. };
  56.  
  57. MSDI msdiDialDlg =
  58. {
  59.     sizeof(rgmsdDialDlg) / sizeof(MSD),
  60.     rgmsdDialDlg,
  61.     edwpNone
  62. };
  63.  
  64. // DialDlg dialog command table definition.
  65. CMD rgcmdDialDlg[] =
  66. {
  67.     {IDCANCEL,  CmdDialDlgCancel}
  68. };
  69.  
  70. CMDI cmdiDialDlg =
  71. {
  72.     sizeof(rgcmdDialDlg) / sizeof(CMD),
  73.     rgcmdDialDlg,
  74.     edwpNone
  75. };
  76.  
  77. // Module specific "globals"  Used when a variable needs to be
  78. // accessed in more than on handler function.
  79. HRASCONN hRasConn;
  80.  
  81. #ifdef WIN32
  82. HWND   g_hDlg;
  83. #endif
  84.  
  85. //
  86. //  FUNCTION: DialDlg(HWND, UINT, WPARAM, LPARAM)
  87. //
  88. //  PURPOSE:  Processes messages for "DialDlg" dialog box.
  89. //
  90. //  PARAMETERS:
  91. //    hdlg - window handle of the dialog box
  92. //    wMessage - type of message
  93. //    wparam - message-specific information
  94. //    lparam - message-specific information
  95. //
  96. //  RETURN VALUE:
  97. //    TRUE - message handled
  98. //    FALSE - message not handled
  99. //
  100. //  COMMENTS:
  101. //
  102. //
  103.  
  104. LRESULT CALLBACK DialDlg(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  105. {
  106.     return DispMessage(&msdiDialDlg, hdlg, uMessage, wparam, lparam);
  107. }
  108.  
  109.  
  110. //
  111. //  FUNCTION: MsgDialDlgInit(HWND, UINT, WPARAM, LPARAM)
  112. //
  113. //  PURPOSE: To initialize the dialdlg box and dial
  114. //
  115. //  PARAMETERS:
  116. //    hwnd - The window handing the message.
  117. //    uMessage - WM_INITDLG.
  118. //    wparam - Message specific data (unused).
  119. //    lparam - Message specific data (unused).
  120. //
  121. //  RETURN VALUE:
  122. //    Always returns TRUE - message handled.
  123. //
  124. //  COMMENTS:
  125. //    Registers window message for RAS event.
  126. //    Makes call to RasDial with appropriate params.
  127. //
  128.  
  129. LRESULT MsgDialDlgInit(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  130. {
  131.     RASDIALPARAMS rdParams;
  132.     char szEntry[RAS_MaxEntryName+1];
  133.     DWORD dwRet;
  134.     char  szBuf[256];
  135.     HWND  hwndEntry;
  136.     UINT  ndx;
  137.  
  138.     hwndEntry = GetDlgItem( GetParent(hdlg), IDL_ENTRY);
  139.     ndx = (UINT) SendMessage( hwndEntry, LB_GETCURSEL, 0,  0L );
  140.     SendMessage( hwndEntry, LB_GETTEXT, ndx, (LPARAM)(LPSTR)szEntry );
  141.     wsprintf( (LPSTR) szBuf, "Dialing %s...", (LPSTR) szEntry );
  142.     SetWindowText( hdlg, (LPSTR)szBuf );
  143.  
  144.     // setup RAS Dial Parameters
  145.     rdParams.dwSize = sizeof(RASDIALPARAMS);
  146.     lstrcpy(rdParams.szEntryName, szEntry );
  147.     rdParams.szPhoneNumber[0] = '\0';
  148.     rdParams.szCallbackNumber[0] = '*';
  149.     rdParams.szCallbackNumber[0] = '\0';
  150.  
  151.     if ( g_bUseCurrent )
  152.     {
  153.         rdParams.szUserName[0] = '\0';
  154.         rdParams.szPassword[0] = '\0';
  155.         rdParams.szDomain[0] = '*';
  156.         rdParams.szDomain[1] = '\0';
  157.     }
  158.     else
  159.     {  
  160.         lstrcpy(rdParams.szUserName, g_szUserName );
  161.         lstrcpy(rdParams.szPassword, g_szPassword );
  162.         lstrcpy(rdParams.szDomain,   g_szDomain );
  163.     }
  164.  
  165.     hRasConn = NULL;
  166.  
  167. #ifdef WIN16
  168.     rgmsdDialDlg[0].uMessage = RegisterWindowMessage(RASDIALEVENT);
  169.     dwRet = RasDial( NULL, NULL, &rdParams, NULL, hdlg, &hRasConn );    
  170. #else
  171.     g_hDlg = hdlg;
  172.     dwRet = RasDial( NULL, NULL, &rdParams, 0L, (RASDIALFUNC) RasDialFunc, &hRasConn );
  173. #endif
  174.  
  175.     if ( dwRet )
  176.     {
  177.         if ( RasGetErrorString( (UINT)dwRet, (LPSTR)szBuf, 256 ) != 0 )
  178.             wsprintf( (LPSTR)szBuf, "Undefined RAS Dial Error (%ld).", dwRet );
  179.  
  180.         MessageBox(hdlg, (LPSTR)szBuf, szAppName, MB_OK | MB_ICONSTOP );
  181.         EndDialog(hdlg, FALSE);
  182.         return TRUE;
  183.     }
  184.     // Center the dialog over the application window
  185.     CenterWindow(hdlg, GetWindow(hdlg, GW_OWNER));
  186.  
  187.     return TRUE;
  188. }
  189.  
  190. //
  191. //  FUNCTION: MsgDialDlgEvent(HWND, UINT, WPARAM, LPARAM)
  192. //
  193. //  PURPOSE: To display messages during dialdlg
  194. //
  195. //  PARAMETERS:
  196. //    hwnd - The window handing the message.
  197. //    uMessage - WM_RASDIALEVENT.
  198. //    wparam - Message specific data (unused).
  199. //    lparam - Message specific data (unused).
  200. //
  201. //  RETURN VALUE:
  202. //    Always returns TRUE - message handled.
  203. //
  204. //  COMMENTS:
  205. //    Updates the text of IDT_MESSAGE to indicate the current
  206. //    state in the connection.  The dialog is closed upon
  207. //    connect or disconnect status.
  208. //
  209.  
  210. LRESULT MsgDialDlgEvent(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  211. {
  212.     char szMessage[256];
  213.         
  214.     LoadString(hInst, GetRasConnState( (RASCONNSTATE) wparam ), szMessage, 64 );
  215.     SetDlgItemText( hdlg, IDT_MESSAGE, (LPCSTR) szMessage );
  216.                       
  217.     if ( lparam )  // error occurred
  218.     {
  219.         if ( RasGetErrorString( (UINT)lparam, szMessage, 256 ) != 0 )
  220.             wsprintf( (LPSTR)szMessage, "Undefined RAS Dial Error." );
  221.  
  222.         MessageBox(hdlg, (LPSTR)szMessage, szAppName, MB_OK | MB_ICONSTOP );
  223.         PostMessage( hdlg, WM_COMMAND, (WPARAM) IDCANCEL, 0L );
  224.     }                                                                                                               
  225.     else if ( RASCS_DONE & wparam )
  226.     {
  227.         EndDialog(hdlg, TRUE);          // Exit the dialog
  228.     }
  229.  
  230.     return TRUE;
  231. }
  232.  
  233. //
  234. //  FUNCTION: MsgDialDlgCommand(HWND, UINT, WPARAM, LPARAM)
  235. //
  236. //  PURPOSE: Process WM_COMMAND message sent to the dialdlg box.
  237. //
  238. //  PARAMETERS:
  239. //    hwnd - The window handing the message.
  240. //    uMessage - The message number. (unused).
  241. //    wparam - Message specific data (unused).
  242. //    lparam - Message specific data (unused).
  243. //
  244. //  RETURN VALUE:
  245. //    Always returns TRUE - message handled.
  246. //
  247. //  COMMENTS:
  248. //    Uses this DispCommand function defined in wndproc.c combined
  249. //    with the cmdiDialDlg structure defined in this file to handle
  250. //    the command messages for the dialdlg dialog box.
  251. //
  252.  
  253. LRESULT MsgDialDlgCommand(HWND   hwnd, 
  254.                         UINT   uMessage, 
  255.                         WPARAM wparam, 
  256.                         LPARAM lparam)
  257. {
  258.     return DispCommand(&cmdiDialDlg, hwnd, wparam, lparam);
  259. }
  260.  
  261. //
  262. //  FUNCTION: CmdDialDlgCancel(HWND, WORD, WORD,  HWND)
  263. //
  264. //  PURPOSE: Free the dialdlg box and related data.
  265. //
  266. //  PARAMETERS:
  267. //    hwnd - The window handling the command.
  268. //    wCommand - IDCANCEL.
  269. //    wNotify - The notification to be handled (unused).
  270. //    hwndCtrl - NULL (unused).
  271. //
  272. //  RETURN VALUE:
  273. //    Always returns TRUE.
  274. //
  275. //  COMMENTS:    
  276. //    Closes current connection attempt and calls EndDialog
  277. //    to finish the dialog session.
  278. //
  279.  
  280. LRESULT CmdDialDlgCancel(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  281. {
  282.     RasHangUp( hRasConn );
  283.     EndDialog(hdlg, FALSE);          // Exit the dialog
  284.     return TRUE;
  285. }
  286.  
  287.  
  288. #ifdef WIN32
  289. //  FUNCTION: RasDialFunc( UINT, RASCONNSTATE, DWORD )
  290. //
  291. //  PURPOSE: Called by RasDial, passes params onto dialog box
  292. //
  293. //  PARAMETERS:
  294. //    unMsg        - Yype of RAS event that occurred.
  295. //    rasconnstate - Connection state about to be entered. 
  296. //    dwError      - Error code that may have occurred.
  297. //
  298. //  COMMENTS:
  299. //    appropriate action is taken in the WM_RASDIALMESSAGE handler
  300.  
  301. VOID WINAPI RasDialFunc( UINT unMsg, RASCONNSTATE rasconnstate, DWORD dwError )
  302. {
  303.     PostMessage( g_hDlg,
  304.                  rgmsdDialDlg[0].uMessage,
  305.                  (WPARAM) rasconnstate,
  306.                  (LPARAM) dwError );
  307.  
  308. }
  309.  
  310. #endif
  311.