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

Windows编程

开发平台:

Visual C++

  1. //+---------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //  Copyright 1992 - 1997 Microsoft Corporation.
  5. //
  6. //  File:       shutdown.c
  7. //
  8. //  Contents:
  9. //
  10. //  Classes:
  11. //
  12. //  Functions:
  13. //
  14. //  History:    4-28-95   RichardW   Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "gina.h"
  18. #pragma hdrstop
  19. WCHAR   szShutdownSettingPath[] = TEXT("Software\Microsoft\Windows NT\CurrentVersion\Shutdown");
  20. int
  21. ShutdownDialogInit(
  22.     HWND        hDlg,
  23.     LPARAM      lParam)
  24. {
  25.     PGlobals    pGlobals;
  26.     HKEY        hKey;
  27.     DWORD       dwValue;
  28.     DWORD       dwSize;
  29.     DWORD       dwType;
  30.     DWORD       PowerOff;
  31.     pGlobals = (PGlobals) lParam;
  32.     SetWindowLong(hDlg, GWL_USERDATA, lParam);
  33.     PowerOff = GetProfileInt(TEXT("Winlogon"), TEXT("PowerDownAfterShutdown"), 0);
  34.     if (pGlobals->hUserToken)
  35.     {
  36.         ImpersonateLoggedOnUser(pGlobals->hUserToken);
  37.     }
  38.     dwValue = 0;
  39.     if (!RegOpenKey(HKEY_CURRENT_USER,
  40.                     szShutdownSettingPath,
  41.                     &hKey))
  42.     {
  43.         dwSize = sizeof(DWORD);
  44.         RegQueryValueEx(hKey, TEXT("Shutdown Setting"), 0, &dwType, (PBYTE) &dwValue, &dwSize);
  45.         RegCloseKey(hKey);
  46.     }
  47.     if (pGlobals->hUserToken)
  48.     {
  49.         RevertToSelf();
  50.     }
  51.     if (!PowerOff)
  52.     {
  53.         ShowWindow(GetDlgItem(hDlg, IDD_CONFIRM_POWEROFF), SW_HIDE);
  54.     }
  55.     if (!pGlobals->hUserToken)
  56.     {
  57.         ShowWindow(GetDlgItem(hDlg, IDD_CONFIRM_LOGOFF), SW_HIDE);
  58.     }
  59.     switch (dwValue)
  60.     {
  61.         case 0:
  62.             if (pGlobals->hUserToken)
  63.             {
  64.                 CheckDlgButton(hDlg, IDD_CONFIRM_LOGOFF, 1);
  65.                 SetFocus(GetDlgItem(hDlg, IDD_CONFIRM_LOGOFF));
  66.                 break;
  67.             }
  68.         case 2:
  69.             CheckDlgButton(hDlg, IDD_CONFIRM_REBOOT, 1);
  70.                 SetFocus(GetDlgItem(hDlg, IDD_CONFIRM_REBOOT));
  71.             break;
  72.         case 3:
  73.             if (PowerOff)
  74.             {
  75.                 CheckDlgButton(hDlg, IDD_CONFIRM_POWEROFF, 1);
  76.                 SetFocus(GetDlgItem(hDlg, IDD_CONFIRM_POWEROFF));
  77.                 break;
  78.             }
  79.         default:
  80.             CheckDlgButton(hDlg, IDD_CONFIRM_SHUTDOWN, 1);
  81.             SetFocus(GetDlgItem(hDlg, IDD_CONFIRM_SHUTDOWN));
  82.     }
  83.     CenterWindow(hDlg);
  84.     SetFocus(GetDlgItem(hDlg, IDOK));
  85.     return(1);
  86. }
  87. VOID
  88. UpdateShutdownSettings(
  89.             PGlobals    pGlobals,
  90.             DWORD       Setting)
  91. {
  92.     // int err;
  93.     HKEY    hKey;
  94.     DWORD   Actual;
  95.     switch (Setting)
  96.     {
  97.         default:
  98.         case IDD_CONFIRM_LOGOFF:
  99.             Actual = 0;
  100.             break;
  101.         case IDD_CONFIRM_SHUTDOWN:
  102.             Actual = 1;
  103.             break;
  104.         case IDD_CONFIRM_REBOOT:
  105.             Actual = 2;
  106.             break;
  107.         case IDD_CONFIRM_POWEROFF:
  108.             Actual = 3;
  109.             break;
  110.     }
  111.     if (pGlobals->hUserToken)
  112.     {
  113.         ImpersonateLoggedOnUser(pGlobals->hUserToken);
  114.     }
  115.     if (!RegOpenKey(HKEY_CURRENT_USER,
  116.                     szShutdownSettingPath,
  117.                     &hKey))
  118.     {
  119.         RegSetValueEx(  hKey,
  120.                         TEXT("Shutdown Setting"),
  121.                         0,
  122.                         REG_DWORD,
  123.                         (PBYTE) &Actual,
  124.                         sizeof(DWORD) );
  125.         RegCloseKey(hKey);
  126.     }
  127.     if (pGlobals->hUserToken)
  128.     {
  129.         RevertToSelf();
  130.     }
  131. }
  132. int
  133. CALLBACK
  134. ShutdownDlgProc(
  135.     HWND        hDlg,
  136.     UINT        Message,
  137.     WPARAM      wParam,
  138.     LPARAM      lParam)
  139. {
  140.     PGlobals    pGlobals;
  141.     switch (Message)
  142.     {
  143.         case WM_INITDIALOG:
  144.             return(ShutdownDialogInit(hDlg, lParam));
  145.         case WM_COMMAND:
  146.             if (LOWORD(wParam) == IDCANCEL)
  147.             {
  148.                 EndDialog(hDlg, WLX_SAS_ACTION_NONE);
  149.             }
  150.             if (LOWORD(wParam) == IDOK)
  151.             {
  152.                 pGlobals = (PGlobals) GetWindowLong(hDlg, GWL_USERDATA);
  153.                 if (IsDlgButtonChecked(hDlg, IDD_CONFIRM_LOGOFF))
  154.                 {
  155.                     UpdateShutdownSettings(pGlobals, IDD_CONFIRM_LOGOFF);
  156.                     EndDialog(hDlg, WLX_SAS_ACTION_LOGOFF);
  157.                 }
  158.                 else if (IsDlgButtonChecked(hDlg, IDD_CONFIRM_REBOOT))
  159.                 {
  160.                     UpdateShutdownSettings(pGlobals, IDD_CONFIRM_REBOOT);
  161.                     EndDialog(hDlg, WLX_SAS_ACTION_SHUTDOWN_REBOOT);
  162.                 }
  163.                 else if (IsDlgButtonChecked(hDlg, IDD_CONFIRM_POWEROFF))
  164.                 {
  165.                     UpdateShutdownSettings(pGlobals, IDD_CONFIRM_POWEROFF);
  166.                     EndDialog(hDlg, WLX_SAS_ACTION_SHUTDOWN_POWER_OFF);
  167.                 }
  168.                 else
  169.                 {
  170.                     UpdateShutdownSettings(pGlobals, IDD_CONFIRM_SHUTDOWN);
  171.                     EndDialog(hDlg, WLX_SAS_ACTION_SHUTDOWN);
  172.                 }
  173.             }
  174.             return(TRUE);
  175.     }
  176.     return(FALSE);
  177. }