Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1012 lines
21 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 2001 **/
  4. /**********************************************************************/
  5. /*
  6. helper.cpp
  7. Implementation of the following helper classes:
  8. CDlgHelper -- enable, check, getcheck of dialog items
  9. CStrArray -- manages an array of CString*
  10. It doesn't duplicate the string when add
  11. It deletes the pointers during destruction
  12. It imports and exports SAFEARRAY of BSTRs
  13. It has copy operatators
  14. CManagedPage -- provide a middle layer between CpropertyPage and
  15. real property page class to manage: readonly, set modify, and
  16. context help info.
  17. And global functions:
  18. BOOL CheckADsError() -- check error code from ADSI
  19. void DecorateName() -- make new name to "CN=name" for LDAP
  20. FILE HISTORY:
  21. */
  22. #include "stdafx.h"
  23. #include <afxtempl.h>
  24. #include <winldap.h>
  25. #include "helper.h"
  26. #include "resource.h"
  27. #include "lm.h"
  28. #include "dsrole.h"
  29. #include "lmserver.h"
  30. //build a StrArray from a safe array
  31. CBYTEArray::CBYTEArray(SAFEARRAY* pSA)
  32. {
  33. if(pSA) AppendSA(pSA);
  34. }
  35. //build a DWArray from another array
  36. CBYTEArray::CBYTEArray(const CBYTEArray& ba)
  37. {
  38. int count = ba.GetSize();
  39. for(int i = 0; i < count; i++)
  40. {
  41. try{
  42. Add(ba[i]);
  43. }
  44. catch(CMemoryException&)
  45. {
  46. throw;
  47. }
  48. }
  49. }
  50. //build a StrArray from a safe array
  51. bool CBYTEArray::AppendSA(SAFEARRAY* pSA)
  52. {
  53. if(!pSA) return false;
  54. CString* pString = NULL;
  55. long lIter;
  56. long lBound, uBound;
  57. union {
  58. VARIANT v;
  59. BYTE b;
  60. } value;
  61. bool bSuc = true; // ser return value to true;
  62. USES_CONVERSION;
  63. VariantInit(&(value.v));
  64. SafeArrayGetLBound(pSA, 1, &lBound);
  65. SafeArrayGetUBound(pSA, 1, &uBound);
  66. for(lIter = lBound; lIter <= uBound; lIter++)
  67. {
  68. if(SUCCEEDED(SafeArrayGetElement(pSA, &lIter, &value)))
  69. {
  70. if(pSA->cbElements == sizeof(VARIANT))
  71. Add(V_UI1(&(value.v)));
  72. else
  73. Add(value.b);
  74. }
  75. }
  76. return bSuc;
  77. }
  78. // convert an array of CString to SAFEARRAY
  79. CBYTEArray::operator SAFEARRAY*()
  80. {
  81. USES_CONVERSION;
  82. int count = GetSize();
  83. if(count == 0) return NULL;
  84. SAFEARRAYBOUND bound[1];
  85. SAFEARRAY* pSA = NULL;
  86. long l[2];
  87. VARIANT v;
  88. VariantInit(&v);
  89. bound[0].cElements = count;
  90. bound[0].lLbound = 0;
  91. try{
  92. // creat empty right size array
  93. #ifdef ARRAY_OF_VARIANT_OF_UI1
  94. pSA = SafeArrayCreate(VT_VARIANT, 1, bound);
  95. #else
  96. pSA = SafeArrayCreate(VT_UI1, 1, bound);
  97. #endif
  98. if(NULL == pSA) return NULL;
  99. // put in each element
  100. for (long i = 0; i < count; i++)
  101. {
  102. #ifdef ARRAY_OF_VARIANT_OF_UI1
  103. V_VT(&v) = VT_UI1;
  104. V_UI1(&v) = GetAt(i);
  105. l[0] = i;
  106. HRESULT hr = SafeArrayPutElement(pSA, l, &v);
  107. VariantClear(&v);
  108. if (FAILED(hr))
  109. {
  110. throw hr;
  111. }
  112. #else
  113. BYTE ele = GetAt(i);
  114. l[0] = i;
  115. HRESULT hr = SafeArrayPutElement(pSA, l, &ele);
  116. if (FAILED(hr))
  117. {
  118. throw hr;
  119. }
  120. #endif
  121. }
  122. }
  123. catch(...)
  124. {
  125. SafeArrayDestroy(pSA);
  126. pSA = NULL;
  127. VariantClear(&v);
  128. throw;
  129. }
  130. return pSA;
  131. }
  132. // return index if found, otherwise -1;
  133. int CBYTEArray::Find(const BYTE b) const
  134. {
  135. int count = GetSize();
  136. while(count--)
  137. {
  138. if(GetAt(count) == b) break;
  139. }
  140. return count;
  141. }
  142. CBYTEArray& CBYTEArray::operator = (const CBYTEArray& ba)
  143. {
  144. int count;
  145. RemoveAll();
  146. // copy new
  147. count = ba.GetSize();
  148. for(int i = 0; i < count; i++)
  149. {
  150. Add(ba[i]);
  151. }
  152. return *this;
  153. }
  154. HRESULT CBYTEArray::AssignBlob(PBYTE pByte, DWORD size)
  155. {
  156. RemoveAll();
  157. // copy new
  158. try{
  159. for(int i = 0; i < size; i++)
  160. {
  161. Add(*pByte++);
  162. }
  163. }
  164. catch(CMemoryException&)
  165. {
  166. RemoveAll();
  167. return E_OUTOFMEMORY;
  168. }
  169. return S_OK;
  170. }
  171. HRESULT CBYTEArray::GetBlob(PBYTE pByte, DWORD* pSize)
  172. {
  173. *pSize = GetSize();
  174. if(pByte == NULL) return S_OK;
  175. ASSERT(*pSize >= GetSize());
  176. int i = 0;
  177. while(i < GetSize() && i < *pSize)
  178. {
  179. *pByte++ = GetAt(i++);
  180. }
  181. *pSize = i;
  182. return S_OK;
  183. }
  184. // helper function -- enable a dialog button
  185. void CDlgHelper::EnableDlgItem(CDialog* pDialog, int id, bool bEnable)
  186. {
  187. CWnd* pWnd = pDialog->GetDlgItem(id);
  188. ASSERT(pWnd);
  189. pWnd->EnableWindow(bEnable);
  190. }
  191. // helper function -- set check status of a dialog button
  192. void CDlgHelper::SetDlgItemCheck(CDialog* pDialog, int id, int nCheck)
  193. {
  194. CButton* pButton = (CButton*)pDialog->GetDlgItem(id);
  195. ASSERT(pButton);
  196. pButton->SetCheck(nCheck);
  197. }
  198. // helper function -- get check status of a dialog button
  199. int CDlgHelper::GetDlgItemCheck(CDialog* pDialog, int id)
  200. {
  201. CButton* pButton = (CButton*)(pDialog->GetDlgItem(id));
  202. ASSERT(pButton);
  203. return pButton->GetCheck();
  204. }
  205. CStrArray& CStrArray::operator = (const CStrArray& sarray)
  206. {
  207. int count = GetSize();
  208. CString* pString;
  209. // remove existing members
  210. while(count --)
  211. {
  212. pString = GetAt(0);
  213. RemoveAt(0);
  214. delete pString;
  215. }
  216. // copy new
  217. count = sarray.GetSize();
  218. for(int i = 0; i < count; i++)
  219. {
  220. pString = new CString(*sarray[i]);
  221. Add(pString);
  222. }
  223. return *this;
  224. }
  225. // convert an array of CString to SAFEARRAY
  226. CStrArray::operator SAFEARRAY*()
  227. {
  228. USES_CONVERSION;
  229. int count = GetSize();
  230. if(count == 0) return NULL;
  231. SAFEARRAYBOUND bound[1];
  232. SAFEARRAY* pSA = NULL;
  233. CString* pStr = NULL;
  234. long l[2];
  235. VARIANT v;
  236. VariantInit(&v);
  237. bound[0].cElements = count;
  238. bound[0].lLbound = 0;
  239. try{
  240. // creat empty right size array
  241. pSA = SafeArrayCreate(VT_VARIANT, 1, bound);
  242. if(NULL == pSA) return NULL;
  243. // put in each element
  244. for (long i = 0; i < count; i++)
  245. {
  246. pStr = GetAt(i);
  247. V_VT(&v) = VT_BSTR;
  248. V_BSTR(&v) = T2BSTR((LPTSTR)(LPCTSTR)(*pStr));
  249. l[0] = i;
  250. HRESULT hr = SafeArrayPutElement(pSA, l, &v);
  251. VariantClear(&v);
  252. if ( FAILED(hr) )
  253. {
  254. throw hr;
  255. }
  256. }
  257. }
  258. catch(...)
  259. {
  260. SafeArrayDestroy(pSA);
  261. pSA = NULL;
  262. VariantClear(&v);
  263. throw;
  264. }
  265. return pSA;
  266. }
  267. //build a StrArray from another array
  268. CStrArray::CStrArray(const CStrArray& sarray)
  269. {
  270. int count = sarray.GetSize();
  271. CString* pString = NULL;
  272. for(int i = 0; i < count; i++)
  273. {
  274. try{
  275. pString = new CString(*sarray[i]);
  276. Add(pString);
  277. }
  278. catch(CMemoryException&)
  279. {
  280. delete pString;
  281. throw;
  282. }
  283. }
  284. }
  285. //build a StrArray from a safe array
  286. CStrArray::CStrArray(SAFEARRAY* pSA)
  287. {
  288. if(pSA) AppendSA(pSA);
  289. }
  290. //remove the elements from the array and delete them
  291. int CStrArray::DeleteAll()
  292. {
  293. int ret, count;
  294. CString* pStr;
  295. ret = count = GetSize();
  296. while(count--)
  297. {
  298. pStr = GetAt(0);
  299. RemoveAt(0);
  300. delete pStr;
  301. }
  302. return ret;
  303. }
  304. //build a StrArray from a safe array
  305. bool CStrArray::AppendSA(SAFEARRAY* pSA)
  306. {
  307. if(!pSA) return false;
  308. CString* pString = NULL;
  309. long lIter;
  310. long lBound, uBound;
  311. VARIANT v;
  312. bool bSuc = true; // ser return value to true;
  313. USES_CONVERSION;
  314. VariantInit(&v);
  315. try{
  316. SafeArrayGetLBound(pSA, 1, &lBound);
  317. SafeArrayGetUBound(pSA, 1, &uBound);
  318. for(lIter = lBound; lIter <= uBound; lIter++)
  319. {
  320. if(SUCCEEDED(SafeArrayGetElement(pSA, &lIter, &v)))
  321. {
  322. if(V_VT(&v) == VT_BSTR)
  323. {
  324. pString = new CString;
  325. (*pString) = (LPCTSTR)W2T(V_BSTR(&v));
  326. Add(pString);
  327. }
  328. }
  329. }
  330. }
  331. catch(CMemoryException&)
  332. {
  333. delete pString;
  334. VariantClear(&v);
  335. bSuc = false;
  336. throw;
  337. }
  338. return bSuc;
  339. }
  340. //build a StrArray from a safe array
  341. CStrArray::~CStrArray()
  342. {
  343. DeleteAll();
  344. }
  345. // return index if found, otherwise -1;
  346. int CStrArray::Find(const CString& Str) const
  347. {
  348. int count = GetSize();
  349. while(count--)
  350. {
  351. if(*GetAt(count) == Str) break;
  352. }
  353. return count;
  354. }
  355. //build a StrArray from a safe array
  356. CDWArray::CDWArray(SAFEARRAY* pSA)
  357. {
  358. if(pSA) AppendSA(pSA);
  359. }
  360. //build a DWArray from another array
  361. CDWArray::CDWArray(const CDWArray& dwarray)
  362. {
  363. int count = dwarray.GetSize();
  364. for(int i = 0; i < count; i++)
  365. {
  366. try{
  367. Add(dwarray[i]);
  368. }
  369. catch(CMemoryException&)
  370. {
  371. throw;
  372. }
  373. }
  374. }
  375. //build a StrArray from a safe array
  376. bool CDWArray::AppendSA(SAFEARRAY* pSA)
  377. {
  378. if(!pSA) return false;
  379. CString* pString = NULL;
  380. long lIter;
  381. long lBound, uBound;
  382. union {
  383. VARIANT v;
  384. DWORD dw;
  385. } value;
  386. bool bSuc = true; // ser return value to true;
  387. USES_CONVERSION;
  388. VariantInit(&(value.v));
  389. SafeArrayGetLBound(pSA, 1, &lBound);
  390. SafeArrayGetUBound(pSA, 1, &uBound);
  391. for(lIter = lBound; lIter <= uBound; lIter++)
  392. {
  393. if(SUCCEEDED(SafeArrayGetElement(pSA, &lIter, &value)))
  394. {
  395. if(pSA->cbElements == sizeof(VARIANT))
  396. Add(V_I4(&(value.v)));
  397. else
  398. Add(value.dw);
  399. }
  400. }
  401. return bSuc;
  402. }
  403. // convert an array of CString to SAFEARRAY
  404. CDWArray::operator SAFEARRAY*()
  405. {
  406. USES_CONVERSION;
  407. int count = GetSize();
  408. if(count == 0) return NULL;
  409. SAFEARRAYBOUND bound[1];
  410. SAFEARRAY* pSA = NULL;
  411. long l[2];
  412. #if 1
  413. VARIANT v;
  414. VariantInit(&v);
  415. #endif
  416. bound[0].cElements = count;
  417. bound[0].lLbound = 0;
  418. try{
  419. // creat empty right size array
  420. pSA = SafeArrayCreate(VT_VARIANT, 1, bound);
  421. if(NULL == pSA) return NULL;
  422. // put in each element
  423. for (long i = 0; i < count; i++)
  424. {
  425. #if 1 // changed to use VT_I4 directly, rather inside a variant
  426. V_VT(&v) = VT_I4;
  427. V_I4(&v) = GetAt(i);
  428. l[0] = i;
  429. HRESULT hr = SafeArrayPutElement(pSA, l, &v);
  430. VariantClear(&v);
  431. if (FAILED(hr))
  432. {
  433. throw hr;
  434. }
  435. #else
  436. int ele = GetAt(i);
  437. l[0] = i;
  438. HRESULT hr = SafeArrayPutElement(pSA, l, &ele);
  439. if (FAILED(hr))
  440. {
  441. throw hr;
  442. }
  443. #endif
  444. }
  445. }
  446. catch(...)
  447. {
  448. SafeArrayDestroy(pSA);
  449. pSA = NULL;
  450. #if 0
  451. VariantClear(&v);
  452. #endif
  453. throw;
  454. }
  455. return pSA;
  456. }
  457. // return index if found, otherwise -1;
  458. int CDWArray::Find(const DWORD dw) const
  459. {
  460. int count = GetSize();
  461. while(count--)
  462. {
  463. if(GetAt(count) == dw) break;
  464. }
  465. return count;
  466. }
  467. CDWArray& CDWArray::operator = (const CDWArray& dwarray)
  468. {
  469. int count;
  470. RemoveAll();
  471. // copy new
  472. count = dwarray.GetSize();
  473. for(int i = 0; i < count; i++)
  474. {
  475. Add(dwarray[i]);
  476. }
  477. return *this;
  478. }
  479. IMPLEMENT_DYNCREATE(CManagedPage, CPropertyPage)
  480. void CManagedPage::OnContextMenu(CWnd* pWnd, CPoint point)
  481. {
  482. if (m_pHelpTable)
  483. ::WinHelp (pWnd->m_hWnd, AfxGetApp()->m_pszHelpFilePath,
  484. HELP_CONTEXTMENU, (DWORD_PTR)(LPVOID)m_pHelpTable);
  485. }
  486. BOOL CManagedPage::OnHelpInfo(HELPINFO* pHelpInfo)
  487. {
  488. if (pHelpInfo->iContextType == HELPINFO_WINDOW && m_pHelpTable)
  489. {
  490. ::WinHelp ((HWND)pHelpInfo->hItemHandle,
  491. AfxGetApp()->m_pszHelpFilePath,
  492. HELP_WM_HELP,
  493. (DWORD_PTR)(LPVOID)m_pHelpTable);
  494. }
  495. return TRUE;
  496. }
  497. int CManagedPage::MyMessageBox(UINT ids, UINT nType)
  498. {
  499. CString string;
  500. string.LoadString(ids);
  501. return MyMessageBox1(string, NULL, nType);
  502. }
  503. int CManagedPage::MyMessageBox1(LPCTSTR lpszText, LPCTSTR lpszCaption, UINT nType)
  504. {
  505. CString caption;
  506. if (lpszCaption == NULL)
  507. {
  508. GetWindowText(caption);
  509. }
  510. else
  511. {
  512. caption = lpszCaption;
  513. }
  514. return MessageBox(lpszText, caption, nType);
  515. }
  516. //+----------------------------------------------------------------------------
  517. //
  518. // Function: CheckADsError
  519. //
  520. // Sysnopsis: Checks the result code from an ADSI call.
  521. //
  522. // Returns: TRUE if no error.
  523. //
  524. //-----------------------------------------------------------------------------
  525. BOOL CheckADsError(HRESULT hr, BOOL fIgnoreAttrNotFound, PSTR file,
  526. int line)
  527. {
  528. if (SUCCEEDED(hr))
  529. return TRUE;
  530. if( hr == E_ADS_PROPERTY_NOT_FOUND && fIgnoreAttrNotFound)
  531. return TRUE;
  532. if (hr == HRESULT_FROM_WIN32(ERROR_EXTENDED_ERROR))
  533. {
  534. DWORD dwErr;
  535. WCHAR wszErrBuf[MAX_PATH+1];
  536. WCHAR wszNameBuf[MAX_PATH+1];
  537. ADsGetLastError(&dwErr, wszErrBuf, MAX_PATH, wszNameBuf, MAX_PATH);
  538. if ((LDAP_RETCODE)dwErr == LDAP_NO_SUCH_ATTRIBUTE && fIgnoreAttrNotFound)
  539. {
  540. return TRUE;
  541. }
  542. TRACE(_T("Extended Error 0x%x: %ws %ws (%s @line %d).\n"), dwErr,
  543. wszErrBuf, wszNameBuf, file, line);
  544. }
  545. else
  546. TRACE(_T("Error %08lx (%s @line %d)\n"), hr, file, line);
  547. return FALSE;
  548. }
  549. void DecorateName(LPWSTR outString, LPCWSTR inString)
  550. {
  551. wcscpy (outString, L"CN=");
  552. wcscat(outString, inString);
  553. }
  554. void FindNameByDN(LPWSTR outString, LPCWSTR inString)
  555. {
  556. LPWSTR p = wcsstr(inString, L"CN=");
  557. if(!p)
  558. p = wcsstr(inString, L"cn=");
  559. if(!p)
  560. wcscpy(outString, inString);
  561. else
  562. {
  563. p+=3;
  564. LPWSTR p1 = outString;
  565. while(*p == L' ') p++;
  566. while(*p != L',')
  567. *p1++ = *p++;
  568. *p1 = L'\0';
  569. }
  570. }
  571. #define MAX_STRING 1024
  572. //+----------------------------------------------------------------------------
  573. //
  574. // Function: ReportError
  575. //
  576. // Sysnopsis: Attempts to get a user-friendly error message from the system.
  577. //
  578. //-----------------------------------------------------------------------------
  579. void ReportError(HRESULT hr, int nStr, HWND hWnd)
  580. {
  581. PTSTR ptzSysMsg;
  582. int cch;
  583. CString AppStr;
  584. CString SysStr;
  585. CString ErrTitle;
  586. CString ErrMsg;
  587. if(S_OK == hr)
  588. return;
  589. TRACE (_T("*+*+* ReportError called with hr = %lx"), hr);
  590. if (!hWnd)
  591. {
  592. hWnd = GetDesktopWindow();
  593. }
  594. try{
  595. if (nStr)
  596. {
  597. AppStr.LoadString(nStr);
  598. }
  599. if(HRESULT_FACILITY(hr) == FACILITY_WIN32) // if win32 error code
  600. cch = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  601. NULL, HRESULT_CODE(hr), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  602. (PTSTR)&ptzSysMsg, 0, NULL);
  603. else
  604. cch = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  605. NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  606. (PTSTR)&ptzSysMsg, 0, NULL);
  607. if (!cch) { //try ads errors
  608. HMODULE adsMod;
  609. adsMod = GetModuleHandle (L"activeds.dll");
  610. cch = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE,
  611. adsMod, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  612. (PTSTR)&ptzSysMsg, 0, NULL);
  613. }
  614. if (!cch)
  615. {
  616. CString str;
  617. str.LoadString(IDS_ERR_ERRORCODE);
  618. SysStr.Format(str, hr);
  619. }
  620. else
  621. {
  622. SysStr = ptzSysMsg;
  623. LocalFree(ptzSysMsg);
  624. }
  625. ErrTitle.LoadString(IDS_ERR_TITLE);
  626. if(!AppStr.IsEmpty())
  627. {
  628. ErrMsg.Format(AppStr, (LPCTSTR)SysStr);
  629. }
  630. else
  631. {
  632. ErrMsg = SysStr;
  633. }
  634. MessageBox(hWnd, (LPCTSTR)ErrMsg, (LPCTSTR)ErrTitle, MB_OK | MB_ICONINFORMATION);
  635. }catch(CMemoryException&)
  636. {
  637. MessageBox(hWnd, _T("No enought memory, please close other applications and try again."), _T("ACS Snapin Error"), MB_OK | MB_ICONINFORMATION);
  638. }
  639. }
  640. /////////////////////////////////////////////////////////////////////////////
  641. // Min Chars Dialog Data Validation
  642. void AFXAPI DDV_MinChars(CDataExchange* pDX, CString const& value, int nChars)
  643. {
  644. ASSERT(nChars >= 1); // allow them something
  645. if (pDX->m_bSaveAndValidate && value.GetLength() < nChars)
  646. {
  647. TCHAR szT[32];
  648. wsprintf(szT, _T("%d"), nChars);
  649. CString prompt;
  650. AfxFormatString1(prompt, IDS_MIN_CHARS, szT);
  651. AfxMessageBox(prompt, MB_ICONEXCLAMATION, IDS_MIN_CHARS);
  652. prompt.Empty(); // exception prep
  653. pDX->Fail();
  654. }
  655. }
  656. /*!--------------------------------------------------------------------------
  657. HrIsStandaloneServer
  658. Returns S_OK if the machine name passed in is a standalone server,
  659. or if pszMachineName is S_FALSE.
  660. Returns FALSE otherwise.
  661. Author: WeiJiang
  662. ---------------------------------------------------------------------------*/
  663. HRESULT HrIsStandaloneServer(LPCWSTR pMachineName)
  664. {
  665. DWORD netRet = 0;
  666. HRESULT hr = S_OK;
  667. DSROLE_PRIMARY_DOMAIN_INFO_BASIC* pdsRole = NULL;
  668. netRet = DsRoleGetPrimaryDomainInformation(pMachineName, DsRolePrimaryDomainInfoBasic, (LPBYTE*)&pdsRole);
  669. if(netRet != 0)
  670. {
  671. hr = HRESULT_FROM_WIN32(netRet);
  672. goto L_ERR;
  673. }
  674. ASSERT(pdsRole);
  675. // if the machine is not a standalone server
  676. if(pdsRole->MachineRole != DsRole_RoleStandaloneServer)
  677. {
  678. hr = S_FALSE;
  679. }
  680. L_ERR:
  681. if(pdsRole)
  682. DsRoleFreeMemory(pdsRole);
  683. return hr;
  684. }
  685. /*!--------------------------------------------------------------------------
  686. HrIsNTServer
  687. Author:
  688. ---------------------------------------------------------------------------*/
  689. HRESULT HrIsNTServer(LPCWSTR pMachineName)
  690. {
  691. HRESULT hr = S_OK;
  692. SERVER_INFO_102* pServerInfo102 = NULL;
  693. NET_API_STATUS netRet = 0;
  694. netRet = NetServerGetInfo((LPWSTR)pMachineName, 102, (LPBYTE*)&pServerInfo102);
  695. if(netRet != 0)
  696. {
  697. hr = HRESULT_FROM_WIN32(netRet);
  698. goto L_ERR;
  699. }
  700. ASSERT(pServerInfo102);
  701. if (!(pServerInfo102->sv102_type & SV_TYPE_SERVER_NT))
  702. {
  703. hr = S_FALSE;
  704. }
  705. L_ERR:
  706. if(pServerInfo102)
  707. NetApiBufferFree(pServerInfo102);
  708. return hr;
  709. }
  710. struct EnableChildControlsEnumParam
  711. {
  712. HWND m_hWndParent;
  713. DWORD m_dwFlags;
  714. };
  715. BOOL CALLBACK EnableChildControlsEnumProc(HWND hWnd, LPARAM lParam)
  716. {
  717. EnableChildControlsEnumParam * pParam;
  718. pParam = reinterpret_cast<EnableChildControlsEnumParam *>(lParam);
  719. // Enable/disable only if this is an immediate descendent
  720. if (GetParent(hWnd) == pParam->m_hWndParent)
  721. {
  722. if (pParam->m_dwFlags & PROPPAGE_CHILD_SHOW)
  723. ::ShowWindow(hWnd, SW_SHOW);
  724. else if (pParam->m_dwFlags & PROPPAGE_CHILD_HIDE)
  725. ::ShowWindow(hWnd, SW_HIDE);
  726. if (pParam->m_dwFlags & PROPPAGE_CHILD_ENABLE)
  727. ::EnableWindow(hWnd, TRUE);
  728. else if (pParam->m_dwFlags & PROPPAGE_CHILD_DISABLE)
  729. ::EnableWindow(hWnd, FALSE);
  730. }
  731. return TRUE;
  732. }
  733. HRESULT EnableChildControls(HWND hWnd, DWORD dwFlags)
  734. {
  735. EnableChildControlsEnumParam param;
  736. param.m_hWndParent = hWnd;
  737. param.m_dwFlags = dwFlags;
  738. EnumChildWindows(hWnd, EnableChildControlsEnumProc, (LPARAM) &param);
  739. return S_OK;
  740. }
  741. #undef CONST_STRING
  742. #undef CONST_STRINGA
  743. #undef CONST_STRINGW
  744. #define _STRINGS_DEFINE_STRINGS
  745. #ifdef _STRINGS_DEFINE_STRINGS
  746. #define CONST_STRING(rg,s) const TCHAR rg[] = TEXT(s);
  747. #define CONST_STRINGA(rg,s) const char rg[] = s;
  748. #define CONST_STRINGW(rg,s) const WCHAR rg[] = s;
  749. #else
  750. #define CONST_STRING(rg,s) extern const TCHAR rg[];
  751. #define CONST_STRINGA(rg,s) extern const char rg[];
  752. #define CONST_STRINGW(rg,s) extern const WCHAR rg[];
  753. #endif
  754. CONST_STRING(c_szRasmanPPPKey, "System\\CurrentControlSet\\Services\\Rasman\\PPP")
  755. CONST_STRING(c_szEAP, "EAP")
  756. CONST_STRING(c_szConfigCLSID, "ConfigCLSID")
  757. CONST_STRING(c_szFriendlyName, "FriendlyName")
  758. CONST_STRING(c_szMPPEEncryptionSupported, "MPPEEncryptionSupported")
  759. CONST_STRING(c_szStandaloneSupported, "StandaloneSupported")
  760. // EAP helper functions
  761. HRESULT LoadEapProviders(HKEY hkeyBase, AuthProviderArray *pProvList, BOOL bStandAlone);
  762. HRESULT GetEapProviders(LPCTSTR pServerName, AuthProviderArray *pProvList)
  763. {
  764. RegKey m_regkeyRasmanPPP;
  765. RegKey regkeyEap;
  766. DWORD dwErr = ERROR_SUCCESS;
  767. HRESULT hr = S_OK;
  768. BOOL bStandAlone = ( S_OK == HrIsStandaloneServer(pServerName));
  769. // Get the list of EAP providers
  770. // ----------------------------------------------------------------
  771. dwErr = m_regkeyRasmanPPP.Open(HKEY_LOCAL_MACHINE,c_szRasmanPPPKey,KEY_ALL_ACCESS,pServerName);
  772. if ( ERROR_SUCCESS == dwErr )
  773. {
  774. if ( ERROR_SUCCESS == regkeyEap.Open(m_regkeyRasmanPPP, c_szEAP) )
  775. hr = LoadEapProviders(regkeyEap, pProvList, bStandAlone);
  776. }
  777. else
  778. hr = HRESULT_FROM_WIN32(dwErr);
  779. return hr;
  780. }
  781. /*!--------------------------------------------------------------------------
  782. DATA_SRV_AUTH::LoadEapProviders
  783. -
  784. Author: KennT
  785. ---------------------------------------------------------------------------*/
  786. HRESULT LoadEapProviders(HKEY hkeyBase, AuthProviderArray *pProvList, BOOL bStandAlone)
  787. {
  788. RegKey regkeyProviders;
  789. HRESULT hr = S_OK;
  790. HRESULT hrIter;
  791. RegKeyIterator regkeyIter;
  792. CString stKey;
  793. RegKey regkeyProv;
  794. AuthProviderData data;
  795. DWORD dwErr;
  796. DWORD dwData;
  797. ASSERT(hkeyBase);
  798. ASSERT(pProvList);
  799. // Open the providers key
  800. // ----------------------------------------------------------------
  801. regkeyProviders.Attach(hkeyBase);
  802. CHECK_HR(hr = regkeyIter.Init(&regkeyProviders) );
  803. for ( hrIter=regkeyIter.Next(&stKey); hrIter == S_OK;
  804. hrIter=regkeyIter.Next(&stKey), regkeyProv.Close() )
  805. {
  806. // Open the key
  807. // ------------------------------------------------------------
  808. dwErr = regkeyProv.Open(regkeyProviders, stKey, KEY_READ);
  809. if ( dwErr != ERROR_SUCCESS )
  810. continue;
  811. // Initialize the data structure
  812. // ------------------------------------------------------------
  813. data.m_stKey = stKey;
  814. data.m_stTitle.Empty();
  815. data.m_stConfigCLSID.Empty();
  816. data.m_stGuid.Empty();
  817. data.m_fSupportsEncryption = FALSE;
  818. data.m_dwStandaloneSupported = 0;
  819. // Read in the values that we require
  820. // ------------------------------------------------------------
  821. regkeyProv.QueryValue(c_szFriendlyName, data.m_stTitle);
  822. regkeyProv.QueryValue(c_szConfigCLSID, data.m_stConfigCLSID);
  823. regkeyProv.QueryValue(c_szMPPEEncryptionSupported, dwData);
  824. data.m_fSupportsEncryption = (dwData != 0);
  825. // Read in the standalone supported value.
  826. // ------------------------------------------------------------
  827. if (S_OK != regkeyProv.QueryValue(c_szStandaloneSupported, dwData))
  828. dwData = 1; // the default
  829. data.m_dwStandaloneSupported = dwData;
  830. if(dwData == 0 /* standalone not supported */ && bStandAlone)
  831. ;
  832. else
  833. pProvList->Add(data);
  834. }
  835. L_ERR:
  836. regkeyProviders.Detach();
  837. return hr;
  838. }