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.

226 lines
6.3 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998
  4. *
  5. * TITLE: SHMEMSEC.H
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 9/30/1999
  12. *
  13. * DESCRIPTION: Simple shared memory section template. Don't use it for classes!
  14. * Basically, simple objects only. structs are ok. Nothing with a
  15. * vtable.
  16. *
  17. *******************************************************************************/
  18. #ifndef __SHMEMSEC_H_INCLUDED
  19. #define __SHMEMSEC_H_INCLUDED
  20. #include <windows.h>
  21. #include <simstr.h>
  22. #include <miscutil.h>
  23. template <class T>
  24. class CSharedMemorySection
  25. {
  26. public:
  27. enum COpenResult
  28. {
  29. SmsFailed,
  30. SmsCreated,
  31. SmsOpened
  32. };
  33. private:
  34. HANDLE m_hMutex;
  35. HANDLE m_hFileMapping;
  36. T *m_pMappedSection;
  37. private:
  38. //
  39. // Not implemented
  40. //
  41. CSharedMemorySection( const CSharedMemorySection & );
  42. CSharedMemorySection &operator=( const CSharedMemorySection & );
  43. public:
  44. CSharedMemorySection( LPCTSTR pszName=NULL, bool bAllowCreate=true )
  45. : m_hFileMapping(NULL),
  46. m_pMappedSection(NULL),
  47. m_hMutex(NULL)
  48. {
  49. WIA_PUSHFUNCTION(TEXT("CSharedMemorySection::CSharedMemorySection"));
  50. Open(pszName,bAllowCreate);
  51. }
  52. ~CSharedMemorySection(void)
  53. {
  54. WIA_PUSHFUNCTION(TEXT("CSharedMemorySection::~CSharedMemorySection"));
  55. Close();
  56. }
  57. bool OK(void)
  58. {
  59. return(m_pMappedSection != NULL);
  60. }
  61. T *Lock(void)
  62. {
  63. T *pResult = NULL;
  64. if (OK())
  65. {
  66. if (WiaUiUtil::MsgWaitForSingleObject( m_hMutex, INFINITE ))
  67. {
  68. pResult = m_pMappedSection;
  69. }
  70. }
  71. return pResult;
  72. }
  73. void Release(void)
  74. {
  75. if (OK())
  76. {
  77. ReleaseMutex(m_hMutex);
  78. }
  79. }
  80. COpenResult Open( LPCTSTR pszName, bool bAllowCreate=true )
  81. {
  82. //
  83. // Close any previous instances
  84. //
  85. Close();
  86. //
  87. // Assume failure
  88. //
  89. COpenResult orResult = SmsFailed;
  90. //
  91. // Make sure we have a valid name
  92. //
  93. if (pszName && *pszName)
  94. {
  95. //
  96. // Save the name
  97. //
  98. CSimpleString strSectionName = pszName;
  99. //
  100. // Replace any invalid characters
  101. //
  102. for (int i=0;i<(int)strSectionName.Length();i++)
  103. {
  104. if (strSectionName[i] == TEXT('\\'))
  105. {
  106. strSectionName[i] = TEXT('-');
  107. }
  108. }
  109. //
  110. // Create the mutex name
  111. //
  112. CSimpleString strMutex(strSectionName);
  113. strMutex += TEXT("-Mutex");
  114. //
  115. // Try to create the mutex
  116. //
  117. m_hMutex = CreateMutex( NULL, FALSE, strMutex );
  118. if (m_hMutex)
  119. {
  120. //
  121. // Take ownership of the mutex
  122. //
  123. if (WiaUiUtil::MsgWaitForSingleObject( m_hMutex, INFINITE ))
  124. {
  125. //
  126. // If this file mapping already exists, open it.
  127. //
  128. m_hFileMapping = OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, strSectionName );
  129. if (m_hFileMapping)
  130. {
  131. m_pMappedSection = reinterpret_cast<T*>(MapViewOfFile( m_hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(T) ));
  132. orResult = SmsOpened;
  133. }
  134. else if (bAllowCreate)
  135. {
  136. //
  137. // Create the file mapping
  138. //
  139. m_hFileMapping = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(T), strSectionName );
  140. if (m_hFileMapping)
  141. {
  142. //
  143. // Try to acquire the file mapping
  144. //
  145. m_pMappedSection = reinterpret_cast<T*>(MapViewOfFile( m_hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(T) ));
  146. if (m_pMappedSection)
  147. {
  148. //
  149. // Initialize the data
  150. //
  151. ZeroMemory( m_pMappedSection, sizeof(T) );
  152. orResult = SmsCreated;
  153. }
  154. }
  155. }
  156. //
  157. // Release the mutex
  158. //
  159. ReleaseMutex(m_hMutex);
  160. }
  161. }
  162. }
  163. //
  164. // If we weren't able to map the file mapping section, we need to clean up
  165. //
  166. if (!m_pMappedSection)
  167. {
  168. Close();
  169. }
  170. return(orResult);
  171. }
  172. void Close(void)
  173. {
  174. //
  175. // First, try to delete it safely.
  176. //
  177. if (m_hMutex)
  178. {
  179. if (WiaUiUtil::MsgWaitForSingleObject( m_hMutex, INFINITE ))
  180. {
  181. if (m_pMappedSection)
  182. {
  183. UnmapViewOfFile(m_pMappedSection);
  184. m_pMappedSection = NULL;
  185. }
  186. if (m_hFileMapping)
  187. {
  188. CloseHandle(m_hFileMapping);
  189. m_hFileMapping = NULL;
  190. }
  191. ReleaseMutex(m_hMutex);
  192. }
  193. }
  194. //
  195. // Then, just clean up
  196. //
  197. if (m_pMappedSection)
  198. {
  199. UnmapViewOfFile(m_pMappedSection);
  200. m_pMappedSection = NULL;
  201. }
  202. if (m_hFileMapping)
  203. {
  204. CloseHandle(m_hFileMapping);
  205. m_hFileMapping = NULL;
  206. }
  207. if (m_hMutex)
  208. {
  209. CloseHandle(m_hMutex);
  210. m_hMutex = NULL;
  211. }
  212. }
  213. };
  214. #endif