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.

244 lines
5.4 KiB

  1. // Persist.cpp : Implementation of persistence
  2. //
  3. // HISTORY
  4. // 01-Jan-1996 ??? Creation
  5. // 03-Jun-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 "stdutils.h" // IsLocalComputername
  13. #include "macros.h"
  14. USE_HANDLE_MACROS("MYCOMPUT(persist.cpp)")
  15. #include <comstrm.h>
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. LPCTSTR PchGetMachineNameOverride(); // Defined in chooser.cpp
  22. /////////////////////////////////////////////////
  23. // The _dwMagicword is the internal version number.
  24. // Increment this number if you makea file format change.
  25. #define _dwMagicword 10001
  26. // IComponentData persistence (remember persistent flags and target computername
  27. /////////////////////////////////////////////////////////////////////
  28. STDMETHODIMP CMyComputerComponentData::Load(IStream __RPC_FAR *pIStream)
  29. {
  30. MFC_TRY;
  31. HRESULT hr;
  32. #ifndef DONT_PERSIST_COMPONENT_DATA
  33. ASSERT( NULL != pIStream );
  34. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  35. // Read the magic word from the stream
  36. DWORD dwMagicword;
  37. hr = pIStream->Read( OUT &dwMagicword, sizeof(dwMagicword), NULL );
  38. if ( FAILED(hr) )
  39. {
  40. ASSERT( FALSE );
  41. return hr;
  42. }
  43. if (dwMagicword != _dwMagicword)
  44. {
  45. // We have a version mismatch
  46. TRACE0("INFO: CMyComputerComponentData::Load() - Wrong Magicword. You need to re-save your .msc file.\n");
  47. return E_FAIL;
  48. }
  49. // read flags from stream
  50. DWORD dwFlags;
  51. hr = pIStream->Read( OUT &dwFlags, sizeof(dwFlags), NULL );
  52. if ( FAILED(hr) )
  53. {
  54. ASSERT( FALSE );
  55. return hr;
  56. }
  57. SetPersistentFlags(dwFlags);
  58. // read server name from stream
  59. DWORD dwLen = 0;
  60. hr = pIStream->Read( &dwLen, 4, NULL );
  61. if ( FAILED(hr) )
  62. {
  63. ASSERT( FALSE );
  64. return hr;
  65. }
  66. ASSERT( dwLen <= MAX_PATH*sizeof(WCHAR) );
  67. LPCWSTR lpwcszMachineName = (LPCWSTR)alloca( dwLen );
  68. // allocated from stack, we don't need to free
  69. if (NULL == lpwcszMachineName)
  70. {
  71. AfxThrowMemoryException();
  72. return E_OUTOFMEMORY;
  73. }
  74. hr = pIStream->Read( (PVOID)lpwcszMachineName, dwLen, NULL );
  75. if ( FAILED(hr) )
  76. {
  77. ASSERT( FALSE );
  78. return hr;
  79. }
  80. m_strMachineNamePersist = lpwcszMachineName;
  81. LPCTSTR pszMachineNameT = PchGetMachineNameOverride();
  82. if (m_fAllowOverrideMachineName && pszMachineNameT != NULL)
  83. {
  84. // Allow machine name override
  85. }
  86. else
  87. {
  88. pszMachineNameT = lpwcszMachineName;
  89. }
  90. // JonN 1/27/99: If the persisted name is the local computername,
  91. // leave the persisted name alone but make the effective name (Local).
  92. if ( IsLocalComputername(pszMachineNameT) )
  93. pszMachineNameT = L"";
  94. QueryRootCookie().SetMachineName(pszMachineNameT);
  95. #endif
  96. return S_OK;
  97. MFC_CATCH;
  98. }
  99. /////////////////////////////////////////////////////////////////////
  100. STDMETHODIMP CMyComputerComponentData::Save(IStream __RPC_FAR *pIStream, BOOL /*fSameAsLoad*/)
  101. {
  102. MFC_TRY;
  103. HRESULT hr;
  104. #ifndef DONT_PERSIST_COMPONENT_DATA
  105. ASSERT( NULL != pIStream );
  106. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  107. // Store the magic word to the stream
  108. DWORD dwMagicword = _dwMagicword;
  109. hr = pIStream->Write( IN &dwMagicword, sizeof(dwMagicword), NULL );
  110. if ( FAILED(hr) )
  111. {
  112. ASSERT( FALSE );
  113. return hr;
  114. }
  115. DWORD dwFlags = GetPersistentFlags();
  116. hr = pIStream->Write( IN &dwFlags, sizeof(dwFlags), NULL );
  117. if ( FAILED(hr) )
  118. {
  119. ASSERT( FALSE );
  120. return hr;
  121. }
  122. LPCWSTR lpwcszMachineName = m_strMachineNamePersist;
  123. DWORD dwLen = (DWORD) (::wcslen(lpwcszMachineName)+1)*sizeof(WCHAR);
  124. ASSERT( 4 == sizeof(DWORD) );
  125. hr = pIStream->Write( &dwLen, sizeof (DWORD), NULL );
  126. if ( FAILED(hr) )
  127. {
  128. ASSERT( FALSE );
  129. return hr;
  130. }
  131. hr = pIStream->Write( lpwcszMachineName, dwLen, NULL );
  132. if ( FAILED(hr) )
  133. {
  134. ASSERT( FALSE );
  135. return hr;
  136. }
  137. #endif
  138. return S_OK;
  139. MFC_CATCH;
  140. }
  141. // IComponent persistence
  142. /////////////////////////////////////////////////////////////////////
  143. STDMETHODIMP CMyComputerComponent::Load(IStream __RPC_FAR *pIStream)
  144. {
  145. MFC_TRY;
  146. HRESULT hr;
  147. #ifndef DONT_PERSIST_COMPONENT
  148. ASSERT( NULL != pIStream );
  149. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  150. // Read the magic word from the stream
  151. DWORD dwMagicword;
  152. hr = pIStream->Read( OUT &dwMagicword, sizeof(dwMagicword), NULL );
  153. if ( FAILED(hr) )
  154. {
  155. ASSERT( FALSE );
  156. return hr;
  157. }
  158. if (dwMagicword != _dwMagicword)
  159. {
  160. // We have a version mismatch
  161. TRACE0("INFO: CMyComputerComponentData::Load() - Wrong Magicword. You need to re-save your .msc file.\n");
  162. return E_FAIL;
  163. }
  164. // read flags from stream
  165. DWORD dwFlags;
  166. hr = pIStream->Read( OUT &dwFlags, sizeof(dwFlags), NULL );
  167. if ( FAILED(hr) )
  168. {
  169. ASSERT( FALSE );
  170. return hr;
  171. }
  172. SetPersistentFlags(dwFlags);
  173. #endif
  174. return S_OK;
  175. MFC_CATCH;
  176. }
  177. /////////////////////////////////////////////////////////////////////
  178. STDMETHODIMP CMyComputerComponent::Save(IStream __RPC_FAR *pIStream, BOOL /*fSameAsLoad*/)
  179. {
  180. MFC_TRY;
  181. HRESULT hr;
  182. #ifndef DONT_PERSIST_COMPONENT
  183. ASSERT( NULL != pIStream );
  184. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  185. // Store the magic word to the stream
  186. DWORD dwMagicword = _dwMagicword;
  187. hr = pIStream->Write( IN &dwMagicword, sizeof(dwMagicword), NULL );
  188. if ( FAILED(hr) )
  189. {
  190. ASSERT( FALSE );
  191. return hr;
  192. }
  193. DWORD dwFlags = GetPersistentFlags();
  194. hr = pIStream->Write( IN &dwFlags, sizeof(dwFlags), NULL );
  195. if ( FAILED(hr) )
  196. {
  197. ASSERT( FALSE );
  198. return hr;
  199. }
  200. #endif
  201. return S_OK;
  202. MFC_CATCH;
  203. }