Leaked source code of windows server 2003
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.

169 lines
3.6 KiB

  1. #include "stdafx.h"
  2. #include "elem.h"
  3. CElem::CElem()
  4. {
  5. m_hKey = NULL;
  6. m_index = 0;
  7. m_ip = _T("");
  8. m_name = _T("");
  9. m_value = _T("");
  10. }
  11. CElem::~CElem()
  12. {
  13. if (m_hKey)
  14. RegCloseKey(m_hKey);
  15. }
  16. BOOL CElem::OpenReg(LPCTSTR szSubKey)
  17. {
  18. BOOL fReturn = FALSE;
  19. LONG err = ERROR_SUCCESS;
  20. err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szSubKey, 0, KEY_READ, &m_hKey);
  21. if (ERROR_SUCCESS == err)
  22. {
  23. fReturn = TRUE;
  24. }
  25. else
  26. {
  27. m_hKey = NULL;
  28. if ( err != ERROR_FILE_NOT_FOUND )
  29. {iisDebugOut((LOG_TYPE_ERROR, _T("CElem::OpenReg(): %s. FAILED. code=0x%x\n"), szSubKey, err));}
  30. }
  31. return (fReturn);
  32. }
  33. void CElem::CloseReg()
  34. {
  35. if (m_hKey)
  36. {
  37. RegCloseKey(m_hKey);
  38. m_hKey = NULL;
  39. }
  40. }
  41. BOOL CElem::GetNext()
  42. {
  43. BOOL fReturn = FALSE;
  44. LONG err = ERROR_SUCCESS;
  45. TCHAR szBufferL[_MAX_PATH], szBufferR[_MAX_PATH];
  46. DWORD dwBufferL = _MAX_PATH, dwBufferR = _MAX_PATH;
  47. err = RegEnumValue(m_hKey, m_index, szBufferL, &dwBufferL, NULL, NULL, (LPBYTE)szBufferR, &dwBufferR);
  48. if (err == ERROR_SUCCESS)
  49. {
  50. LPTSTR token;
  51. m_index++;
  52. m_value = szBufferR;
  53. token = _tcstok(szBufferL, _T(","));
  54. if (token)
  55. {
  56. m_name = token;
  57. token = _tcstok(NULL, _T(","));
  58. if (token) {
  59. m_ip = token;
  60. } else {
  61. m_ip = _T("null");
  62. }
  63. fReturn = TRUE;
  64. }
  65. m_name.MakeLower();
  66. m_value.MakeLower();
  67. //
  68. // m_name looks like = /scripts
  69. // m_value looks like = c:\inetpub\scripts,,4
  70. //
  71. // m_value could look like anything
  72. // c:
  73. // c:\stuff
  74. // c:\whatevers\
  75. // who knows what they put in there.
  76. // we need to make sure it look like a fully qualified path though.
  77. //
  78. // Get the first Value before the comma
  79. int iWhere = 0;
  80. iWhere = m_value.Find(_T(','));
  81. if (-1 != iWhere)
  82. {
  83. CString BeforeComma;
  84. CString AfterComma;
  85. // there is a ',' in the string
  86. BeforeComma = m_value.Left(iWhere);
  87. // Get the after comma vlues
  88. AfterComma = m_value.Right( m_value.GetLength() - iWhere);
  89. TCHAR thefilename[_MAX_PATH];
  90. TCHAR thepath[_MAX_PATH];
  91. TCHAR * pmypath;
  92. if ( BeforeComma.GetLength() >= ( sizeof(thefilename) / sizeof( TCHAR ) ) )
  93. {
  94. fReturn = FALSE;
  95. }
  96. else
  97. {
  98. _tcscpy(thefilename, BeforeComma.GetBuffer(0) );
  99. // make sure the left side is a valid directory name!
  100. if (0 != GetFullPathName(thefilename, _MAX_PATH, thepath, &pmypath))
  101. {
  102. BeforeComma = thepath;
  103. }
  104. // reconcatenate them
  105. m_value = BeforeComma;
  106. m_value += AfterComma;
  107. }
  108. }
  109. }
  110. else
  111. {
  112. if ( err != ERROR_FILE_NOT_FOUND && err != ERROR_NO_MORE_ITEMS)
  113. {
  114. iisDebugOut((LOG_TYPE_WARN, _T("CElem::GetNext(): FAILED. code=0x%x\n"), err));
  115. }
  116. }
  117. return (fReturn);
  118. }
  119. void CElem::ReadRegVRoots(LPCTSTR szSubKey, CMapStringToOb *pMap)
  120. {
  121. if ( OpenReg(szSubKey) )
  122. {
  123. while (GetNext())
  124. {
  125. Add(pMap);
  126. }
  127. CloseReg();
  128. }
  129. }
  130. void CElem::Add(CMapStringToOb *pMap)
  131. {
  132. CObject *pObj;
  133. CMapStringToString *pNew;
  134. if (pMap->Lookup(m_ip, pObj) == TRUE) {
  135. pNew = (CMapStringToString*)pObj;
  136. pNew->SetAt(m_name, m_value);
  137. } else {
  138. pNew = new CMapStringToString;
  139. if ( pNew )
  140. {
  141. pNew->SetAt(m_name, m_value);
  142. pMap->SetAt(m_ip, (CObject*)pNew);
  143. }
  144. }
  145. }