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.

306 lines
6.6 KiB

  1. // Persist.cpp : Implementation of persistence for CFileMgmtComponentData
  2. //
  3. // HISTORY
  4. // 01-Jan-1996 ??? Creation
  5. // 28-May-1997 t-danm Added a version number to storage and
  6. // Command Line override.
  7. //
  8. /////////////////////////////////////////////////////////////////////
  9. #include "stdafx.h"
  10. #include "compdata.h"
  11. #include "safetemp.h"
  12. #include "macros.h"
  13. USE_HANDLE_MACROS("FILEMGMT(persist.cpp)")
  14. #include <comstrm.h>
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. LPCTSTR PchGetMachineNameOverride(); // Defined in chooser.cpp
  21. /////////////////////////////////////////////////
  22. // The _dwMagicword is the internal version number.
  23. // Increment this number if you makea file format change.
  24. #define _dwMagicword 10000
  25. ///////////////////////////////////////////////////////////////////////////////
  26. /// IPersistStorage
  27. #ifdef PERSIST_TO_STORAGE
  28. /*
  29. STDMETHODIMP CFileMgmtComponentData::Load(IStorage __RPC_FAR *pStg)
  30. {
  31. MFC_TRY;
  32. ASSERT( NULL != pStg );
  33. #ifndef DONT_PERSIST
  34. // open stream
  35. IStream* pIStream = NULL;
  36. HRESULT hr = pStg->OpenStream(
  37. L"ServerName",
  38. NULL,
  39. STGM_READ | STGM_SHARE_EXCLUSIVE,
  40. 0L,
  41. &pIStream );
  42. if ( FAILED(hr) )
  43. {
  44. ASSERT( FALSE );
  45. return hr;
  46. }
  47. ASSERT( NULL != pIStream );
  48. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  49. // read object type from stream
  50. hr = pIStream->Read( &(QueryRootCookie().QueryObjectType()), 4, NULL );
  51. if ( FAILED(hr) )
  52. {
  53. ASSERT( FALSE );
  54. return hr;
  55. }
  56. // read server name from stream
  57. DWORD dwLen = 0;
  58. hr = pIStream->Read( &dwLen, 4, NULL );
  59. if ( FAILED(hr) )
  60. {
  61. ASSERT( FALSE );
  62. return hr;
  63. }
  64. ASSERT( dwLen <= MAX_PATH*sizeof(WCHAR) );
  65. // allocated from stack, we don't need to free
  66. LPCWSTR lpwcszMachineName = (LPCWSTR)alloca( dwLen );
  67. if (NULL == lpwcszMachineName)
  68. {
  69. AfxThrowMemoryException();
  70. return E_OUTOFMEMORY;
  71. }
  72. hr = pIStream->Read( (PVOID)lpwcszMachineName, dwLen, NULL );
  73. if ( FAILED(hr) )
  74. {
  75. ASSERT( FALSE );
  76. return hr;
  77. }
  78. m_RootCookieBlock.SetMachineName( lpwcszMachineName );
  79. #endif
  80. return S_OK;
  81. MFC_CATCH;
  82. }
  83. */
  84. /*
  85. STDMETHODIMP CFileMgmtComponentData::Save(IStorage __RPC_FAR *pStgSave, BOOL fSameAsLoad)
  86. {
  87. MFC_TRY;
  88. ASSERT( NULL != pStgSave );
  89. #ifndef DONT_PERSIST
  90. IStream* pIStream = NULL;
  91. HRESULT hr = pStgSave->CreateStream(
  92. L"ServerName",
  93. STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  94. 0L,
  95. 0L,
  96. &pIStream );
  97. if ( FAILED(hr) )
  98. {
  99. ASSERT( FALSE );
  100. return hr;
  101. }
  102. ASSERT( NULL != pIStream );
  103. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  104. ASSERT( 4 == sizeof(QueryRootCookie().QueryObjectType()) );
  105. hr = pIStream->Write( &(QueryRootCookie().QueryObjectType()), 4, NULL );
  106. if ( FAILED(hr) )
  107. {
  108. ASSERT( FALSE );
  109. return hr;
  110. }
  111. LPCWSTR lpwcszMachineName = QueryRootCookie().QueryNonNULLMachineName();
  112. DWORD dwLen = (::wcslen(lpwcszMachineName)+1)*sizeof(WCHAR);
  113. ASSERT( 4 == sizeof(DWORD) );
  114. hr = pIStream->Write( &dwLen, 4, NULL );
  115. if ( FAILED(hr) )
  116. {
  117. ASSERT( FALSE );
  118. return hr;
  119. }
  120. hr = pIStream->Write( lpwcszMachineName, dwLen, NULL );
  121. if ( FAILED(hr) )
  122. {
  123. ASSERT( FALSE );
  124. return hr;
  125. }
  126. #endif
  127. return S_OK;
  128. MFC_CATCH;
  129. }
  130. */
  131. #else // PERSIST_TO_STORAGE
  132. STDMETHODIMP CFileMgmtComponentData::Load(IStream __RPC_FAR *pIStream)
  133. {
  134. MFC_TRY;
  135. HRESULT hr;
  136. #ifndef DONT_PERSIST
  137. ASSERT( NULL != pIStream );
  138. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  139. // Read the magic word from the stream
  140. DWORD dwMagicword;
  141. hr = pIStream->Read( OUT &dwMagicword, sizeof(dwMagicword), NULL );
  142. if ( FAILED(hr) )
  143. {
  144. ASSERT( FALSE );
  145. return hr;
  146. }
  147. if (dwMagicword != _dwMagicword)
  148. {
  149. // We have a version mismatch
  150. TRACE0("INFO: CFileMgmtComponentData::Load() - Wrong Magicword. You need to re-save your .msc file.\n");
  151. return E_FAIL;
  152. }
  153. // read object type from stream
  154. FileMgmtObjectType objecttype;
  155. ASSERT( 4 == sizeof(objecttype) );
  156. hr = pIStream->Read( &objecttype, 4, NULL );
  157. if ( FAILED(hr) )
  158. {
  159. ASSERT( FALSE );
  160. return hr;
  161. }
  162. QueryRootCookie().SetObjectType( objecttype );
  163. // read flags from stream
  164. DWORD dwFlags;
  165. hr = pIStream->Read( OUT &dwFlags, sizeof(dwFlags), NULL );
  166. if ( FAILED(hr) )
  167. {
  168. ASSERT( FALSE );
  169. return hr;
  170. }
  171. SetPersistentFlags(dwFlags);
  172. // read server name from stream
  173. DWORD dwLen = 0;
  174. hr = pIStream->Read( &dwLen, 4, NULL );
  175. if ( FAILED(hr) )
  176. {
  177. ASSERT( FALSE );
  178. return hr;
  179. }
  180. ASSERT( dwLen <= MAX_PATH*sizeof(WCHAR) );
  181. LPCWSTR lpwcszMachineName = (LPCWSTR)alloca( dwLen );
  182. // allocated from stack, we don't need to free
  183. if (NULL == lpwcszMachineName)
  184. {
  185. AfxThrowMemoryException();
  186. return E_OUTOFMEMORY;
  187. }
  188. hr = pIStream->Read( (PVOID)lpwcszMachineName, dwLen, NULL );
  189. if ( FAILED(hr) )
  190. {
  191. ASSERT( FALSE );
  192. return hr;
  193. }
  194. m_strMachineNamePersist = lpwcszMachineName;
  195. LPCTSTR pszMachineNameT = PchGetMachineNameOverride();
  196. if (m_fAllowOverrideMachineName && pszMachineNameT != NULL)
  197. {
  198. // Allow machine name override
  199. }
  200. else
  201. {
  202. pszMachineNameT = lpwcszMachineName;
  203. }
  204. // JonN 1/27/99: If the persisted name is the local computername,
  205. // leave the persisted name alone but make the effective name (Local).
  206. if ( IsLocalComputername(pszMachineNameT) )
  207. pszMachineNameT = L"";
  208. if (pszMachineNameT && !_tcsncmp(pszMachineNameT, _T("\\\\"), 2))
  209. QueryRootCookie().SetMachineName(pszMachineNameT + 2);
  210. else
  211. QueryRootCookie().SetMachineName(pszMachineNameT);
  212. #endif
  213. return S_OK;
  214. MFC_CATCH;
  215. } // CFileMgmtComponentData::Load()
  216. STDMETHODIMP CFileMgmtComponentData::Save(IStream __RPC_FAR *pIStream, BOOL /*fSameAsLoad*/)
  217. {
  218. MFC_TRY;
  219. HRESULT hr;
  220. #ifndef DONT_PERSIST
  221. ASSERT( NULL != pIStream );
  222. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  223. // Store the magic word to the stream
  224. DWORD dwMagicword = _dwMagicword;
  225. hr = pIStream->Write( IN &dwMagicword, sizeof(dwMagicword), NULL );
  226. if ( FAILED(hr) )
  227. {
  228. ASSERT( FALSE );
  229. return hr;
  230. }
  231. FileMgmtObjectType objecttype = QueryRootCookie().QueryObjectType();
  232. ASSERT( 4 == sizeof(objecttype) );
  233. hr = pIStream->Write( &objecttype, 4, NULL );
  234. if ( FAILED(hr) )
  235. {
  236. ASSERT( FALSE );
  237. return hr;
  238. }
  239. DWORD dwFlags = GetPersistentFlags();
  240. hr = pIStream->Write( IN &dwFlags, sizeof(dwFlags), NULL );
  241. if ( FAILED(hr) )
  242. {
  243. ASSERT( FALSE );
  244. return hr;
  245. }
  246. LPCWSTR lpwcszMachineName = m_strMachineNamePersist;
  247. ULONG cbLen = (ULONG)((::wcslen(lpwcszMachineName)+1)*sizeof(WCHAR));
  248. ASSERT( 4 == sizeof(DWORD) );
  249. hr = pIStream->Write( &cbLen, 4, NULL );
  250. if ( FAILED(hr) )
  251. {
  252. ASSERT( FALSE );
  253. return hr;
  254. }
  255. hr = pIStream->Write( lpwcszMachineName, cbLen, NULL );
  256. if ( FAILED(hr) )
  257. {
  258. ASSERT( FALSE );
  259. return hr;
  260. }
  261. #endif
  262. return S_OK;
  263. MFC_CATCH;
  264. } // CFileMgmtComponentData::Save()
  265. #endif // PERSIST_TO_STORAGE