OPTIONS.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:8k
源码类别:

Windows编程

开发平台:

Visual C++

  1. //+---------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //  Copyright 1992 - 1997 Microsoft Corporation.
  5. //
  6. //  File:       options.c
  7. //
  8. //  Contents:
  9. //
  10. //  Classes:
  11. //
  12. //  Functions:
  13. //
  14. //  History:    5-02-95   RichardW   Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "gina.h"
  18. #pragma hdrstop
  19. typedef
  20. NET_API_STATUS (NET_API_FUNCTION * NUCP)(LPWSTR, LPWSTR, LPWSTR, LPWSTR);
  21. NUCP    NetUserChangePasswordFn = NULL;
  22. HMODULE hNetApi32;
  23. BOOL
  24. LoadNetapi(HWND hDlg)
  25. {
  26.     hNetApi32 = LoadLibrary(TEXT("netapi32.dll"));
  27.     if (hNetApi32)
  28.     {
  29.         NetUserChangePasswordFn = (NUCP) GetProcAddress(hNetApi32, "NetUserChangePassword");
  30.         if (NetUserChangePasswordFn)
  31.         {
  32.             return(TRUE);
  33.         }
  34.     }
  35.     ErrorMessage(hDlg, TEXT("Change Password"), MB_ICONSTOP | MB_OK);
  36.     return(FALSE);
  37. }
  38. PWSTR
  39. AllocAndCaptureText(
  40.     HWND    hDlg,
  41.     int     Id)
  42. {
  43.     WCHAR   szTemp[MAX_PATH];
  44.     PWSTR   New;
  45.     DWORD   cb;
  46.     cb = GetDlgItemText(hDlg, Id, szTemp, MAX_PATH);
  47.     New = LocalAlloc(LMEM_FIXED, (cb + 1) * sizeof(WCHAR));
  48.     if (New)
  49.     {
  50.         wcscpy(New, szTemp);
  51.     }
  52.     return(New);
  53. }
  54. PWSTR
  55. DupString(
  56.     PWSTR   pszText)
  57. {
  58.     PWSTR   New;
  59.     DWORD   cb;
  60.     cb = (wcslen(pszText) + 1) * sizeof(WCHAR);
  61.     New = LocalAlloc(LMEM_FIXED, cb);
  62.     if (New)
  63.     {
  64.         wcscpy(New, pszText);
  65.     }
  66.     return(New);
  67. }
  68. BOOL
  69. TryToChangePassword(
  70.     HWND        hDlg,
  71.     PGlobals    pGlobals)
  72. {
  73.     PWSTR   pszUsername;
  74.     PWSTR   pszDomain;
  75.     PWSTR   pszOld;
  76.     PWSTR   pszNew;
  77.     PWSTR   pszTemp;
  78.     NET_API_STATUS  NetStatus;
  79.     if (!NetUserChangePasswordFn)
  80.     {
  81.         if (!LoadNetapi(hDlg))
  82.         {
  83.             return(FALSE);
  84.         }
  85.     }
  86.     pszUsername = AllocAndCaptureText(hDlg, IDD_USER_NAME);
  87.     pszDomain = AllocAndCaptureText(hDlg, IDD_DOMAIN);
  88.     pszOld = AllocAndCaptureText(hDlg, IDD_OLD_PASSWORD);
  89.     pszNew = AllocAndCaptureText(hDlg, IDD_NEW_PASSWORD);
  90.     if (!pszUsername || !pszDomain || !pszOld || !pszNew)
  91.     {
  92.         goto clean_exit;
  93.     }
  94.     pszTemp = AllocAndCaptureText(hDlg, IDD_CONFIRM_PASSWORD);
  95.     if (wcscmp(pszNew, pszTemp))
  96.     {
  97.         LocalFree(pszTemp);
  98.         MessageBox(hDlg, TEXT("Your passwords did not match."), TEXT("Change Password"),
  99.                         MB_ICONSTOP | MB_OK);
  100.         goto clean_exit;
  101.     }
  102.     NetStatus = NetUserChangePasswordFn(pszDomain, pszUsername, pszOld, pszNew);
  103.     if (NetStatus != NERR_Success)
  104.     {
  105.         SetLastError(NetStatus);
  106.         ErrorMessage(hDlg, TEXT("Change Password"), MB_ICONSTOP | MB_OK);
  107.     }
  108.     else
  109.         MessageBox(hDlg, TEXT("Your password was changed successfully"),
  110.                     TEXT("Change Password"), MB_ICONINFORMATION | MB_OK);
  111. clean_exit:
  112.     if (pszUsername)
  113.     {
  114.         LocalFree(pszUsername);
  115.     }
  116.     if (pszDomain)
  117.     {
  118.         LocalFree(pszDomain);
  119.     }
  120.     if (pszOld)
  121.     {
  122.         LocalFree(pszOld);
  123.     }
  124.     if (pszNew)
  125.     {
  126.         LocalFree(pszNew);
  127.     }
  128.     return(NetStatus == NERR_Success);
  129. }
  130. int
  131. CALLBACK
  132. ChangePasswordDlgProc(
  133.     HWND        hDlg,
  134.     UINT        Message,
  135.     WPARAM      wParam,
  136.     LPARAM      lParam)
  137. {
  138.     PGlobals    pGlobals;
  139.     pGlobals = (PGlobals) GetWindowLong(hDlg, GWL_USERDATA);
  140.     switch (Message)
  141.     {
  142.         case WM_INITDIALOG:
  143.             CenterWindow(hDlg);
  144.             SetWindowLong(hDlg, GWL_USERDATA, lParam);
  145.             return(TRUE);
  146.         case WM_COMMAND:
  147.             if (LOWORD(wParam) == IDOK)
  148.             {
  149.                 if (TryToChangePassword(hDlg, pGlobals))
  150.                 {
  151.                     SetWindowText(GetDlgItem(hDlg, IDCANCEL), TEXT("Done"));
  152.                 }
  153.                 SetDlgItemText(hDlg, IDD_OLD_PASSWORD, TEXT(""));
  154.                 SetDlgItemText(hDlg, IDD_NEW_PASSWORD, TEXT(""));
  155.                 SetDlgItemText(hDlg, IDD_CONFIRM_PASSWORD, TEXT(""));
  156.                 SetFocus(GetDlgItem(hDlg, IDD_OLD_PASSWORD));
  157.                 return(TRUE);
  158.             }
  159.             if (LOWORD(wParam) == IDCANCEL)
  160.             {
  161.                 EndDialog(hDlg, 0);
  162.                 return(TRUE);
  163.             }
  164.     }
  165.     return(FALSE);
  166. }
  167. int
  168. CALLBACK
  169. ConfigDlgProc(
  170.     HWND        hDlg,
  171.     UINT        Message,
  172.     WPARAM      wParam,
  173.     LPARAM      lParam)
  174. {
  175.     PGlobals    pGlobals;
  176.     pGlobals = (PGlobals) GetWindowLong(hDlg, GWL_USERDATA);
  177.     switch (Message)
  178.     {
  179.         case WM_INITDIALOG:
  180.             pGlobals = (PGlobals) lParam;
  181.             CenterWindow(hDlg);
  182.             SetWindowLong(hDlg, GWL_USERDATA, lParam);
  183.             CheckDlgButton(hDlg, IDD_NO_NEW_USERS, !pGlobals->fAllowNewUser);
  184.             CheckDlgButton(hDlg, IDD_AUTO_LOGON,
  185.                     (pGlobals->pAccount->Flags & MINI_AUTO_LOGON) ? 1 : 0 );
  186.             return(TRUE);
  187.         case WM_COMMAND:
  188.             switch (LOWORD(wParam))
  189.             {
  190.                 case IDOK:
  191.                     pGlobals->fAllowNewUser = !IsDlgButtonChecked(hDlg, IDD_NO_NEW_USERS);
  192.                     if (IsDlgButtonChecked(hDlg, IDD_AUTO_LOGON))
  193.                     {
  194.                         pGlobals->pAccount->Flags |= MINI_AUTO_LOGON;
  195.                     }
  196.                     else
  197.                     {
  198.                         pGlobals->pAccount->Flags &= ~MINI_AUTO_LOGON;
  199.                     }
  200.                     EndDialog(hDlg, IDOK);
  201.                     return(TRUE);
  202.                 case IDCANCEL:
  203.                     EndDialog(hDlg, IDCANCEL);
  204.                     return(TRUE);
  205.             }
  206.     }
  207.     return(FALSE);
  208. }
  209. int
  210. InitOptionsDialog(
  211.     HWND        hDlg,
  212.     LPARAM      lParam)
  213. {
  214.     CenterWindow(hDlg);
  215.     SetWindowLong(hDlg, GWL_USERDATA, lParam);
  216.     return(1);
  217. }
  218. int
  219. CALLBACK
  220. OptionsDlgProc(
  221.     HWND        hDlg,
  222.     UINT        Message,
  223.     WPARAM      wParam,
  224.     LPARAM      lParam)
  225. {
  226.     PGlobals    pGlobals;
  227.     int         result;
  228.     pGlobals = (PGlobals) GetWindowLong(hDlg, GWL_USERDATA);
  229.     switch (Message)
  230.     {
  231.         case WM_INITDIALOG:
  232.             return InitOptionsDialog(hDlg, lParam);
  233.         case WM_COMMAND:
  234.             switch (LOWORD(wParam))
  235.             {
  236.                 case IDCANCEL:
  237.                     EndDialog(hDlg, WLX_SAS_ACTION_NONE);
  238.                     return(TRUE);
  239.                 case IDD_LOCK_BUTTON:
  240.                     EndDialog(hDlg, WLX_SAS_ACTION_LOCK_WKSTA);
  241.                     return(TRUE);
  242.                 case IDD_TASK_BUTTON:
  243.                     EndDialog(hDlg, WLX_SAS_ACTION_TASKLIST);
  244.                     return(TRUE);
  245.                 case IDD_OPTIONS_EXIT:
  246.                     result = pWlxFuncs->WlxDialogBoxParam(  hGlobalWlx,
  247.                                                         hDllInstance,
  248.                                                         (LPTSTR) MAKEINTRESOURCE(IDD_SHUTDOWN),
  249.                                                         hDlg,
  250.                                                         ShutdownDlgProc,
  251.                                                         (LONG) pGlobals);
  252.                     if (result != WLX_SAS_ACTION_NONE)
  253.                     {
  254.                         EndDialog(hDlg, result);
  255.                     }
  256.                     return(TRUE);
  257.                 case IDD_PASSWORD_BUTTON:
  258.                     pWlxFuncs->WlxDialogBoxParam(   hGlobalWlx,
  259.                                                     hDllInstance,
  260.                                                     (LPTSTR) MAKEINTRESOURCE(IDD_CHANGE_PASSWORD),
  261.                                                     hDlg,
  262.                                                     ChangePasswordDlgProc,
  263.                                                     (LONG) pGlobals);
  264.                     return(TRUE);
  265.                 case IDD_CONFIG_BUTTON:
  266.                     pWlxFuncs->WlxDialogBoxParam(   hGlobalWlx,
  267.                                                     hDllInstance,
  268.                                                     (LPTSTR) MAKEINTRESOURCE(IDD_LOGON_CONFIG),
  269.                                                     hDlg,
  270.                                                     ConfigDlgProc,
  271.                                                     (LONG) pGlobals);
  272.                     return(TRUE);
  273.             }
  274.     }
  275.     return(FALSE);
  276. }