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.

279 lines
6.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2001.
  5. //
  6. // File: Persist.cpp
  7. //
  8. // Contents: Implementation of persistence
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include <gpedit.h>
  13. #include "compdata.h"
  14. USE_HANDLE_MACROS("CERTMGR(persist.cpp)")
  15. #ifdef _DEBUG
  16. #ifndef ALPHA
  17. #define new DEBUG_NEW
  18. #endif
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. LPCWSTR PchGetMachineNameOverride(); // Defined in chooser.cpp
  23. /////////////////////////////////////////////////
  24. // The _dwMagicword is the internal version number.
  25. // Increment this number if you make a file format change.
  26. #define _dwMagicword 10001
  27. /////////////////////////////////////////////////////////////////////
  28. STDMETHODIMP CCertMgrComponentData::Load(IStream __RPC_FAR *pIStream)
  29. {
  30. HRESULT hResult;
  31. #ifndef DONT_PERSIST
  32. ASSERT (pIStream);
  33. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  34. // Read the magic word from the stream
  35. DWORD dwMagicword;
  36. hResult = pIStream->Read( OUT &dwMagicword, sizeof(dwMagicword), NULL );
  37. if ( FAILED(hResult) )
  38. {
  39. ASSERT( FALSE );
  40. return hResult;
  41. }
  42. if (dwMagicword != _dwMagicword)
  43. {
  44. // We have a version mismatch
  45. _TRACE (0, L"INFO: CCertMgrComponentData::Load() - Wrong Magicword. You need to re-save your .msc file.\n");
  46. return E_FAIL;
  47. }
  48. // read m_activeViewPersist from stream
  49. hResult = pIStream->Read (&m_activeViewPersist,
  50. sizeof(m_activeViewPersist), NULL);
  51. if ( FAILED(hResult) )
  52. {
  53. ASSERT( FALSE );
  54. return hResult;
  55. }
  56. // read m_dwLocationPersist from stream
  57. hResult = pIStream->Read (&m_dwLocationPersist,
  58. sizeof(m_dwLocationPersist), NULL);
  59. if ( FAILED(hResult) )
  60. {
  61. ASSERT( FALSE );
  62. return hResult;
  63. }
  64. // read m_bShowPhysicalStoresPersist from stream
  65. hResult = pIStream->Read (&m_bShowPhysicalStoresPersist,
  66. sizeof(m_bShowPhysicalStoresPersist), NULL);
  67. if ( FAILED(hResult) )
  68. {
  69. ASSERT( FALSE );
  70. return hResult;
  71. }
  72. // read m_bShowArchivedCertsPersist from stream
  73. hResult = pIStream->Read (&m_bShowArchivedCertsPersist,
  74. sizeof(m_bShowArchivedCertsPersist), NULL);
  75. if ( FAILED(hResult) )
  76. {
  77. ASSERT( FALSE );
  78. return hResult;
  79. }
  80. // read flags from stream
  81. DWORD dwFlags;
  82. hResult = pIStream->Read( OUT &dwFlags, sizeof(dwFlags), NULL );
  83. if ( FAILED(hResult) )
  84. {
  85. ASSERT( FALSE );
  86. return hResult;
  87. }
  88. SetPersistentFlags(dwFlags);
  89. // read server name from stream
  90. DWORD dwLen = 0;
  91. hResult = pIStream->Read (&dwLen, 4, NULL);
  92. if ( FAILED (hResult) )
  93. {
  94. ASSERT (FALSE);
  95. return hResult;
  96. }
  97. ASSERT (dwLen <= MAX_PATH * sizeof (WCHAR));
  98. LPCWSTR wcszMachineName = (LPCWSTR) alloca (dwLen);
  99. hResult = pIStream->Read ((PVOID) wcszMachineName, dwLen, NULL);
  100. if ( FAILED (hResult) )
  101. {
  102. ASSERT (FALSE);
  103. return hResult;
  104. }
  105. // Skip leading "\\", if present
  106. if ( !wcsncmp (wcszMachineName, L"\\\\", 2) )
  107. m_strMachineNamePersist = wcszMachineName + 2;
  108. else
  109. m_strMachineNamePersist = wcszMachineName;
  110. LPCWSTR pszMachineNameT = PchGetMachineNameOverride ();
  111. if ( m_fAllowOverrideMachineName && pszMachineNameT )
  112. {
  113. // Allow machine name override
  114. }
  115. else
  116. {
  117. pszMachineNameT = wcszMachineName;
  118. }
  119. // Truncate leading "\\"
  120. if ( !wcsncmp (pszMachineNameT, L"\\\\", 2) )
  121. pszMachineNameT += 2;
  122. QueryRootCookie().SetMachineName (pszMachineNameT);
  123. // read service name from stream
  124. dwLen = 0;
  125. hResult = pIStream->Read (&dwLen, 4, NULL);
  126. if ( FAILED (hResult) )
  127. {
  128. ASSERT (FALSE);
  129. return hResult;
  130. }
  131. ASSERT (dwLen <= MAX_PATH * sizeof (WCHAR));
  132. LPCWSTR wcszServiceName = (LPCWSTR) alloca (dwLen);
  133. hResult = pIStream->Read ((PVOID) wcszServiceName, dwLen, NULL);
  134. if ( FAILED (hResult) )
  135. {
  136. ASSERT (FALSE);
  137. return hResult;
  138. }
  139. m_szManagedServicePersist = wcszServiceName;
  140. #endif
  141. return S_OK;
  142. }
  143. /////////////////////////////////////////////////////////////////////
  144. STDMETHODIMP CCertMgrComponentData::Save(IStream __RPC_FAR *pIStream, BOOL /*fSameAsLoad*/)
  145. {
  146. HRESULT hResult;
  147. #ifndef DONT_PERSIST
  148. ASSERT (pIStream);
  149. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  150. // Store the magic word to the stream
  151. DWORD dwMagicword = _dwMagicword;
  152. hResult = pIStream->Write( IN &dwMagicword, sizeof(dwMagicword), NULL );
  153. if ( FAILED(hResult) )
  154. {
  155. ASSERT( FALSE );
  156. return hResult;
  157. }
  158. // Persist m_activeViewPersist
  159. hResult = pIStream->Write (&m_activeViewPersist,
  160. sizeof (m_activeViewPersist), NULL);
  161. if ( FAILED(hResult) )
  162. {
  163. ASSERT( FALSE );
  164. return hResult;
  165. }
  166. // Persist m_dwLocationPersist
  167. hResult = pIStream->Write (&m_dwLocationPersist,
  168. sizeof (m_dwLocationPersist), NULL);
  169. if ( FAILED(hResult) )
  170. {
  171. ASSERT( FALSE );
  172. return hResult;
  173. }
  174. // Persist m_bShowPhysicalStoresPersist
  175. hResult = pIStream->Write (&m_bShowPhysicalStoresPersist,
  176. sizeof (m_bShowPhysicalStoresPersist), NULL);
  177. if ( FAILED(hResult) )
  178. {
  179. ASSERT( FALSE );
  180. return hResult;
  181. }
  182. // Persist m_bShowArchivedCertsPersist
  183. hResult = pIStream->Write (&m_bShowArchivedCertsPersist,
  184. sizeof (m_bShowArchivedCertsPersist), NULL);
  185. if ( FAILED(hResult) )
  186. {
  187. ASSERT( FALSE );
  188. return hResult;
  189. }
  190. // persist flags
  191. DWORD dwFlags = GetPersistentFlags();
  192. hResult = pIStream->Write( IN &dwFlags, sizeof(dwFlags), NULL );
  193. if ( FAILED(hResult) )
  194. {
  195. ASSERT( FALSE );
  196. return hResult;
  197. }
  198. // Persist machine name length and machine name
  199. LPCWSTR wcszMachineName = m_strMachineNamePersist;
  200. size_t dwLen = (::wcslen (wcszMachineName) + 1) * sizeof (WCHAR);
  201. ASSERT( 4 == sizeof(DWORD) );
  202. hResult = pIStream->Write (&dwLen, 4, NULL);
  203. if ( FAILED(hResult) )
  204. {
  205. ASSERT( FALSE );
  206. return hResult;
  207. }
  208. hResult = pIStream->Write (wcszMachineName, (DWORD) dwLen, NULL);
  209. if ( FAILED (hResult) )
  210. {
  211. ASSERT (FALSE);
  212. return hResult;
  213. }
  214. // Persist service name length and service name
  215. LPCWSTR wcszServiceName = m_szManagedServicePersist;
  216. dwLen = (::wcslen (wcszServiceName) + 1) * sizeof (WCHAR);
  217. ASSERT (4 == sizeof (DWORD));
  218. hResult = pIStream->Write (&dwLen, 4, NULL);
  219. if ( FAILED (hResult) )
  220. {
  221. ASSERT( FALSE );
  222. return hResult;
  223. }
  224. hResult = pIStream->Write (wcszServiceName, (DWORD) dwLen, NULL);
  225. if ( FAILED (hResult) )
  226. {
  227. ASSERT (FALSE);
  228. return hResult;
  229. }
  230. #endif
  231. return S_OK;
  232. }