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.

171 lines
5.4 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 2002.
  5. //
  6. // File : SMem.hxx
  7. //
  8. // Contents : Shared memory (named + file based)
  9. //
  10. // Classes : CNamedSharedMem
  11. //
  12. // History: 22-Mar-94 t-joshh Created
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. #include <secutil.hxx>
  17. //+---------------------------------------------------------------------------
  18. //
  19. // Class: CNamedSharedMem
  20. //
  21. // Purpose: Named shared memory
  22. //
  23. // History: 22-Mar-94 t-joshh Created
  24. //
  25. //----------------------------------------------------------------------------
  26. class CNamedSharedMem
  27. {
  28. public:
  29. CNamedSharedMem()
  30. : _hMap( 0 ),
  31. _pb( 0 )
  32. {
  33. }
  34. enum eNameType { AppendPid };
  35. ~CNamedSharedMem()
  36. {
  37. if ( 0 != _pb )
  38. UnmapViewOfFile( _pb );
  39. if ( 0 != _hMap )
  40. CloseHandle( _hMap );
  41. }
  42. BYTE * GetPointer() { return _pb; }
  43. BYTE * Get() { return _pb; }
  44. BOOL Ok() { return ( 0 != _pb ); }
  45. void CreateForWriteFromRegKey( WCHAR const * pwszName,
  46. ULONG cb,
  47. WCHAR const * pwcRegKey )
  48. {
  49. Win4Assert( 0 == _hMap );
  50. Win4Assert( 0 == _pb );
  51. Win4Assert( 0 != pwcRegKey );
  52. _hMap = CreateFileMapping( INVALID_HANDLE_VALUE, // Map to page file
  53. 0, // Security attributes
  54. PAGE_READWRITE, // Page-level protection
  55. 0, // Size high
  56. cb, // size low
  57. pwszName ); // Name
  58. if ( 0 == _hMap )
  59. THROW( CException() );
  60. DWORD dwErr = CopyNamedDacls( pwszName, pwcRegKey );
  61. if ( NO_ERROR != dwErr )
  62. THROW( CException( HRESULT_FROM_WIN32( dwErr ) ) );
  63. _pb = (BYTE *) MapViewOfFile( _hMap, // Handle to map
  64. FILE_MAP_WRITE, // Access
  65. 0, // Offset (high)
  66. 0, // Offset (low)
  67. 0 ); // Map all
  68. if ( 0 == _pb )
  69. {
  70. DWORD dwErr = GetLastError();
  71. CloseHandle( _hMap );
  72. _hMap = 0;
  73. THROW( CException( HRESULT_FROM_WIN32( dwErr ) ) );
  74. }
  75. }
  76. void CreateForWriteFromSA( WCHAR const * pwszName,
  77. ULONG cb,
  78. SECURITY_ATTRIBUTES & sa )
  79. {
  80. Win4Assert( 0 == _hMap );
  81. Win4Assert( 0 == _pb );
  82. _hMap = CreateFileMapping( INVALID_HANDLE_VALUE, // Map to page file
  83. &sa, // Security attributes
  84. PAGE_READWRITE, // Page-level protection
  85. 0, // Size high
  86. cb, // size low
  87. pwszName ); // Name
  88. if ( 0 == _hMap )
  89. THROW( CException() );
  90. _pb = (BYTE *) MapViewOfFile( _hMap, // Handle to map
  91. FILE_MAP_WRITE, // Access
  92. 0, // Offset (high)
  93. 0, // Offset (low)
  94. 0 ); // Map all
  95. if ( 0 == _pb )
  96. {
  97. DWORD dwErr = GetLastError();
  98. CloseHandle( _hMap );
  99. _hMap = 0;
  100. THROW( CException( HRESULT_FROM_WIN32( dwErr ) ) );
  101. }
  102. }
  103. BOOL OpenForRead( WCHAR const * pwszName )
  104. {
  105. Win4Assert( 0 == _hMap );
  106. Win4Assert( 0 == _pb );
  107. //
  108. // Note: you have to ask for PAGE_READWRITE even though we only want
  109. // PAGE_READ, otherwise you get ERROR_ACCESS_DENIED. I don't know
  110. // why, but since only a limited # of contexts have write access,
  111. // it should be OK.
  112. //
  113. _hMap = OpenFileMapping( PAGE_READWRITE, // Access
  114. FALSE, // Inherit
  115. pwszName ); // Name
  116. if ( 0 == _hMap )
  117. {
  118. if ( ERROR_FILE_NOT_FOUND == GetLastError() )
  119. return FALSE;
  120. THROW( CException() );
  121. }
  122. _pb = (BYTE *) MapViewOfFile( _hMap,
  123. FILE_MAP_READ, // Access
  124. 0, // Offset (high)
  125. 0, // Offset (low)
  126. 0 ); // Map all
  127. if ( 0 == _pb )
  128. {
  129. DWORD dwErr = GetLastError();
  130. CloseHandle( _hMap );
  131. _hMap = 0;
  132. THROW( CException( HRESULT_FROM_WIN32( dwErr ) ) );
  133. }
  134. return TRUE;
  135. }
  136. private:
  137. HANDLE _hMap;
  138. BYTE * _pb;
  139. };