secdlg.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 6k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /********************************************************
  2.  secdlg.cpp
  3.   User Manager security dialog implementation
  4.  History:
  5.   09/23/98: dsheldon created
  6. ********************************************************/
  7. #include "stdafx.h"
  8. #include "resource.h"
  9. #include "secdlg.h"
  10. #include "misc.h"
  11. INT_PTR CSecurityCheckDlg::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  12. {
  13.     switch(uMsg)
  14.     {
  15.         HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  16.         HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  17.     }
  18.     return FALSE;
  19. }
  20. BOOL CSecurityCheckDlg::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  21. {
  22.     TraceEnter(TRACE_USR_CORE, "CSecurityCheckDlg::OnInitDialog");
  23.     BOOL fIsLocalAdmin;
  24.     // First we must check if the current user is a local administrator; if this is
  25.     // the case, our dialog doesn't even display
  26.     
  27.     if (SUCCEEDED(IsUserLocalAdmin(NULL, &fIsLocalAdmin)))
  28.     {
  29.         if (fIsLocalAdmin)
  30.         {
  31.             // We want to continue and launch the applet (don't display the security check dlg)
  32.             EndDialog(hwnd, IDOK);
  33.         }
  34.     }
  35.     else
  36.     {
  37.         TraceMsg("IsUserLocalAdmin failed");
  38.         EndDialog(hwnd, IDCANCEL);
  39.     }
  40.     // Set the "can't launch User Options" message
  41.     TCHAR szUsername[MAX_USER + 1];
  42.     DWORD cchUsername = ARRAYSIZE(szUsername);
  43.     TCHAR szDomain[MAX_DOMAIN + 1];
  44.     DWORD cchDomain = ARRAYSIZE(szDomain);
  45.     if (GetCurrentUserAndDomainName(szUsername, &cchUsername, szDomain,
  46.         &cchDomain))
  47.     {
  48.         TCHAR szDomainAndUsername[MAX_DOMAIN + MAX_USER + 2];
  49.         MakeDomainUserString(szDomain, szUsername, szDomainAndUsername, 
  50.             ARRAYSIZE(szDomainAndUsername));
  51.         TCHAR szMessage[256];
  52.         if (FormatMessageString(IDS_USR_CANTRUNCPL_FORMAT, szMessage, ARRAYSIZE(szMessage), szDomainAndUsername))
  53.         {
  54.             SetWindowText(GetDlgItem(hwnd, IDC_CANTRUNCPL_STATIC), szMessage);
  55.         }
  56.         TCHAR szAdministrator[MAX_USER + 1];
  57.         LoadString(g_hInstance, IDS_ADMINISTRATOR, szAdministrator,
  58.             ARRAYSIZE(szAdministrator));
  59.         SetWindowText(GetDlgItem(hwnd, IDC_USER), szAdministrator);
  60.         TCHAR szMachine[MAX_COMPUTERNAME + 1];
  61.         
  62.         DWORD dwSize = ARRAYSIZE(szMachine);
  63.         ::GetComputerName(szMachine, &dwSize);
  64.         SetWindowText(GetDlgItem(hwnd, IDC_DOMAIN), szMachine);
  65.     }
  66.     // Limit the text in the edit fields
  67.     HWND hwndUsername = GetDlgItem(hwnd, IDC_USER);
  68.     Edit_LimitText(hwndUsername, MAX_USER);
  69.     HWND hwndDomain = GetDlgItem(hwnd, IDC_DOMAIN);
  70.     Edit_LimitText(hwndDomain, MAX_DOMAIN);
  71.     HWND hwndPassword = GetDlgItem(hwnd, IDC_PASSWORD);
  72.     Edit_LimitText(hwndPassword, MAX_PASSWORD);
  73.     if (!IsComputerInDomain())
  74.     {
  75.         // Don't need domain box
  76.         EnableWindow(hwndDomain, FALSE);
  77.         ShowWindow(hwndDomain, SW_HIDE);
  78.         ShowWindow(GetDlgItem(hwnd, IDC_DOMAIN_STATIC), SW_HIDE);
  79.         // Move up the OK/Cancel buttons and text and shrink the dialog
  80.         RECT rcDomain;
  81.         GetWindowRect(hwndDomain, &rcDomain);
  82.         RECT rcPassword;
  83.         GetWindowRect(hwndPassword, &rcPassword);
  84.         
  85.         int dy = (rcPassword.top - rcDomain.top);
  86.         // dy is negative 
  87.         OffsetWindow(GetDlgItem(hwnd, IDOK), 0, dy);
  88.         OffsetWindow(GetDlgItem(hwnd, IDCANCEL), 0, dy);
  89.         OffsetWindow(GetDlgItem(hwnd, IDC_PASSWORD_STATIC), 0, dy);
  90.         RECT rcDialog;
  91.         GetWindowRect(hwnd, &rcDialog);
  92.         rcDialog.bottom += dy;  
  93.         MoveWindow(hwnd, rcDialog.left, rcDialog.top, rcDialog.right-rcDialog.left,
  94.             rcDialog.bottom-rcDialog.top, FALSE);
  95.     }
  96.     TraceLeaveValue(TRUE);
  97. }
  98. BOOL CSecurityCheckDlg::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  99. {
  100.     TraceEnter(TRACE_USR_CORE, "CSecurityCheckDlg::OnCommand");
  101.     BOOL fReturn = FALSE;
  102.     switch (id)
  103.     {
  104.     case IDOK:
  105.         if (SUCCEEDED(RelaunchAsUser(hwnd)))
  106.         {
  107.             EndDialog(hwnd, IDCANCEL);
  108.         }
  109.         fReturn = TRUE;
  110.         break;
  111.     case IDCANCEL:
  112.         EndDialog(hwnd, IDCANCEL);
  113.         fReturn = TRUE;
  114.         break;
  115.     }
  116.     TraceLeaveValue(fReturn);
  117. }
  118. HRESULT CSecurityCheckDlg::RelaunchAsUser(HWND hwnd)
  119. {
  120.     TraceEnter(TRACE_USR_CORE, "CSecurityCheckDlg::RelaunchAsUser");
  121.     USES_CONVERSION;
  122.     HRESULT hr = E_FAIL;
  123.     TCHAR szUsername[MAX_USER + 1];
  124.     FetchText(hwnd, IDC_USER, szUsername, ARRAYSIZE(szUsername));
  125.     TCHAR szDomain[MAX_DOMAIN + 1];
  126.     FetchText(hwnd, IDC_DOMAIN, szDomain, ARRAYSIZE(szDomain));
  127.     // If the user didn't type a domain
  128.     if (szDomain[0] == TEXT(''))
  129.     {
  130.         // Use this machine as the domain
  131.         DWORD cchComputername = ARRAYSIZE(szDomain);
  132.         ::GetComputerName(szDomain, &cchComputername);
  133.     }
  134.     TCHAR szPassword[MAX_PASSWORD + 1];
  135.     GetWindowText(GetDlgItem(hwnd, IDC_PASSWORD), szPassword, ARRAYSIZE(szPassword));
  136.     
  137.     // Now relaunch ourselves with this information
  138.     STARTUPINFO startupinfo = {0};
  139.     startupinfo.cb = sizeof (startupinfo);
  140.     WCHAR c_szCommandLineFormat[] = L"rundll32.exe netplwiz.dll,UsersRunDll %s";
  141.     // Put the "real" user name in the command-line so that we know what user is
  142.     // actually logged on to the machine even though we are re-launching in a different
  143.     // user context
  144.     WCHAR szCommandLine[ARRAYSIZE(c_szCommandLineFormat) + MAX_DOMAIN + MAX_USER + 2];
  145.     wnsprintf(szCommandLine, ARRAYSIZE(szCommandLine), c_szCommandLineFormat, m_pszDomainUser);
  146.     PROCESS_INFORMATION process_information;
  147.     if (CreateProcessWithLogonW(szUsername, szDomain, szPassword, 0, NULL,
  148.         szCommandLine, 0, NULL, NULL, &startupinfo, &process_information))
  149.     {
  150.         hr = S_OK;
  151.         CloseHandle(process_information.hProcess);
  152.         CloseHandle(process_information.hThread);
  153.     }
  154.     else
  155.     {
  156.         DisplayFormatMessage(hwnd, IDS_USR_APPLET_CAPTION, IDS_USR_CANTOPENCPLASUSER_ERROR, 
  157.             MB_OK | MB_ICONERROR);
  158.     }
  159.  
  160.     TraceLeaveResult(hr);
  161. }