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

Windows Kernel

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "advanced.h"
  3. BOOL CAdvancedDialog::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  4. {
  5.     switch (uMsg)
  6.     {
  7.         HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  8.         HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  9.     default:
  10.         break;
  11.     }
  12.     return FALSE;
  13. }
  14. BOOL CAdvancedDialog::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  15. {
  16.     // Create the message for the advanced dialog that informs the user
  17.     // of the purpose of the dialog, etc.
  18.     TCHAR szUsername[UNLEN + 1];
  19.     TCHAR szDomainname[UNCLEN + 1];
  20.     TCHAR szMessage[UNLEN + UNCLEN + MAX_STATIC + 1];
  21.     TCHAR szFormat[MAX_STATIC + 1];
  22.     TCHAR szUserAndDomain[UNLEN + UNCLEN + 1];
  23.     // Load the current user name
  24.     ULONG cchUsername = ARRAYSIZE(szUsername);
  25.     ULONG cchDomainname = ARRAYSIZE(szDomainname);
  26.     if (GetCurrentUserAndDomainName(szUsername, &cchUsername, szDomainname, &cchDomainname))
  27.     {
  28.         // Create the serverusername form
  29.         wsprintf(szUserAndDomain, TEXT("%s\%s"), szDomainname, szUsername);
  30.         // Create the message
  31.         LoadString(g_hInstance, IDS_CONNECTASUSER_MESSAGE, szFormat, ARRAYSIZE(szFormat));
  32.         _sntprintf(szMessage, ARRAYSIZE(szMessage), szFormat, m_pdata->szResourceName, 
  33.             szUserAndDomain);
  34.         SetDlgItemText(hwnd, IDC_LOGGEDIN_STATIC, szMessage);
  35.     }
  36.     // Load the current alternate user name into the text box
  37.     SetDlgItemText(hwnd, IDC_USER, m_pdata->szUsername);
  38.     Edit_LimitText(GetDlgItem(hwnd, IDC_USER), UNLEN);
  39.     // If there is no DC, disable "find user" function
  40.     if (!GetEnvironmentVariable(TEXT("USERDNSDOMAIN"), NULL, 0) )
  41.     {
  42.         // DS isn't available, disable browse
  43.         EnableWindow(GetDlgItem(hwnd, IDC_FINDUSER_BUTTON), FALSE);
  44.     }
  45.     // Load the current alternate user password into the text box
  46.     SetDlgItemText(hwnd, IDC_PASSWORD, m_pdata->szPassword);
  47.     Edit_LimitText(GetDlgItem(hwnd, IDC_PASSWORD), PWLEN);
  48.     // Set the reconnect check box if needed
  49.     Button_SetCheck(GetDlgItem(hwnd, IDC_COMPLETION_RECONNECT_CHECK),
  50.                     (m_pdata->fReconnect ? BST_CHECKED : BST_UNCHECKED));
  51.     return TRUE;
  52. }
  53. BOOL CAdvancedDialog::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  54. {
  55.     switch (id)
  56.     {
  57.     case IDC_FINDUSER_BUTTON:
  58.         // User wants to look up a username
  59.         FindUser(hwnd, IDC_USER);
  60.         return TRUE;
  61.     case IDOK:
  62.         // User has click OK, save the username they've entered
  63.         GetDlgItemText(hwnd, IDC_USER, m_pdata->szUsername, 
  64.             ARRAYSIZE(m_pdata->szUsername));
  65.         GetDlgItemText(hwnd, IDC_PASSWORD, m_pdata->szPassword,
  66.             ARRAYSIZE(m_pdata->szPassword));
  67.         // See if the user wants to reconnect to the share on startup
  68.         m_pdata->fReconnect = IsDlgButtonChecked(hwnd, IDC_COMPLETION_RECONNECT_CHECK);
  69.         // Fall through
  70.     case IDCANCEL:
  71.         EndDialog(hwnd, id);
  72.         return TRUE;
  73.     default:
  74.         break;
  75.     }
  76.     return FALSE;
  77. }
  78. void CAdvancedDialog::FindUser(HWND hwndDlg, UINT uiTextLocation)
  79. // This routine activates the appropriate Object Picker to allow
  80. // the user to select a user
  81. // uiTextLocation  -- The resource ID of the Edit control where the selected 
  82. //                    object should be printed 
  83. {
  84.     static const TCHAR c_szObjectSid[] = TEXT("ObjectSid");
  85.     static const TCHAR* aszAttributes[] = { c_szObjectSid, };
  86.     HRESULT             hr;
  87.     PDSSELECTIONLIST    pDsSelectionList;
  88.     GETUSERGROUPSELECTIONINFO gui = {0};
  89.     gui.cbSize = sizeof(gui);
  90.     gui.ptzComputerName = NULL;
  91.     gui.flDsObjectPicker =
  92.           DSOP_SCOPE_DIRECTORY
  93.        |  DSOP_SCOPE_DOMAIN_TREE
  94.        |  DSOP_SCOPE_EXTERNAL_TRUSTED_DOMAINS
  95.        |  DSOP_RESTRICT_NAMES_TO_KNOWN_DOMAINS;
  96.     gui.flStartingScope = DSOP_SCOPE_SPECIFIED_DOMAIN;
  97.     gui.flUserGroupObjectPickerOtherDomains =
  98.           UGOP_USERS;
  99.     gui.cRequestedAttributes = ARRAYSIZE(aszAttributes);
  100.     gui.aptzRequestedAttributes = aszAttributes;
  101.     gui.ppDsSelList = &pDsSelectionList;
  102.     gui.hwndParent = hwndDlg;
  103.     //
  104.     // List users and groups, with this computer as the focus
  105.     //
  106.     hr = GetUserGroupSelection(&gui);
  107.     //
  108.     // Note: If the object picker dialog is cancelled, it returns S_FALSE,
  109.     // so we must check for that as well. Technically, we could just check 
  110.     // (hr == S_OK), but this way, we're safe if COM adds more success codes.
  111.     // Also, note that the NULL check for pDsSelectionList is a workaround
  112.     // for a bug in the Object Picker where it returns S_OK with no selection.
  113.     //
  114.     if (SUCCEEDED(hr) && hr != S_FALSE && pDsSelectionList)
  115.     {
  116.         // Fill in the edit box with the user name
  117.         LPVARIANT pvarSid = pDsSelectionList->aDsSelection[0].pvarOtherAttributes;
  118.         if (NULL != pvarSid && (VT_ARRAY | VT_UI1) == V_VT(pvarSid))
  119.         {
  120.             PSID pSid; // = (PSID)(V_ARRAY(pvarSid)->pvData);
  121.             if (SUCCEEDED(SafeArrayAccessData(V_ARRAY(pvarSid), &pSid)))
  122.             {
  123.                 TCHAR szDomain[DNLEN + 1];
  124.                 TCHAR szUser[UNLEN + 1];
  125.                 DWORD cchDomain = ARRAYSIZE(szDomain);
  126.                 DWORD cchUser = ARRAYSIZE(szUser);
  127.                 SID_NAME_USE sUse;
  128.                 // Lookup the domain and user name for this SID
  129.                 BOOL fSuccess = LookupAccountSid(NULL, pSid, szUser, &cchUser, 
  130.                     szDomain, &cchDomain, &sUse);
  131.                 if (fSuccess)
  132.                 {
  133.                     TCHAR szDomainUser[UNLEN + DNLEN + 2];
  134.                     wnsprintf(szDomainUser, ARRAYSIZE(szDomainUser), TEXT("%s\%s"),
  135.                         szDomain, szUser);
  136.                     SetDlgItemText(hwndDlg, uiTextLocation, szDomainUser);
  137.                 }
  138.                 else
  139.                 {
  140.                     // Lookupaccountsid failed
  141.                 }
  142.                 SafeArrayUnaccessData(V_ARRAY(pvarSid));
  143.             }
  144.         }
  145.         else
  146.         {
  147.             TraceMsg("SID wasn't returned from GetUserGroupSelection");
  148.         }
  149.     }
  150.     else
  151.     {
  152.         // MPRUI_LOG0(TRACE, "Object picker was closed with no selection maden");
  153.     }
  154.     if (pDsSelectionList)
  155.         FreeDsSelectionList(pDsSelectionList);
  156. }