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.

408 lines
9.2 KiB

  1. //
  2. //
  3. //
  4. #include "private.h"
  5. #include "sharemem.h"
  6. #include "globals.h"
  7. #include "candutil.h"
  8. //+---------------------------------------------------------------------------
  9. //
  10. // GetDesktopUniqueName
  11. //
  12. //----------------------------------------------------------------------------
  13. void GetDesktopUniqueName(const TCHAR *pchPrefix, ULONG cchPrefix, TCHAR *pch, ULONG cchPch)
  14. {
  15. StringCchCopy(pch, cchPch, pchPrefix);
  16. if (FIsWindowsNT() && cchPrefix < cchPch)
  17. {
  18. TCHAR ach[MAX_PATH];
  19. DWORD dwLength;
  20. HDESK hdesk;
  21. hdesk = GetThreadDesktop(GetCurrentThreadId());
  22. if (GetUserObjectInformation(hdesk, UOI_NAME, ach, sizeof(ach) /* byte count */, &dwLength))
  23. {
  24. StringCchCopy(pch + cchPrefix, cchPch - cchPrefix, ach);
  25. }
  26. }
  27. }
  28. //
  29. // CCandUIMMFile
  30. //
  31. /* C C A N D U I M M F I L E */
  32. /*------------------------------------------------------------------------------
  33. ------------------------------------------------------------------------------*/
  34. CCandUIMMFile::CCandUIMMFile( void )
  35. {
  36. m_hFile = NULL;
  37. m_pvData = NULL;
  38. }
  39. /* ~ C C A N D U I M M F I L E */
  40. /*------------------------------------------------------------------------------
  41. ------------------------------------------------------------------------------*/
  42. CCandUIMMFile::~CCandUIMMFile( void )
  43. {
  44. Close();
  45. }
  46. /* O P E N */
  47. /*------------------------------------------------------------------------------
  48. ------------------------------------------------------------------------------*/
  49. BOOL CCandUIMMFile::Open( LPSTR szName, DWORD dwFlag )
  50. {
  51. TCHAR achDesktopUniqueName[MAX_PATH];
  52. DWORD dwDesiredAccess;
  53. if (m_hFile != NULL) {
  54. return FALSE;
  55. }
  56. // access flag
  57. if ((dwFlag & CANDUIMM_READWRITE) != 0) {
  58. dwDesiredAccess = FILE_MAP_ALL_ACCESS;
  59. }
  60. else {
  61. dwDesiredAccess = FILE_MAP_READ;
  62. }
  63. // open file
  64. GetDesktopUniqueName(szName, lstrlen(szName), achDesktopUniqueName, ARRAYSIZE(achDesktopUniqueName));
  65. m_hFile = OpenFileMapping( dwDesiredAccess, FALSE, achDesktopUniqueName );
  66. if (m_hFile == NULL) {
  67. return FALSE;
  68. }
  69. // memory mapping
  70. m_pvData = MapViewOfFile( m_hFile, dwDesiredAccess, 0, 0, 0 );
  71. if (m_pvData == NULL) {
  72. Close();
  73. return FALSE;
  74. }
  75. return TRUE;
  76. }
  77. /* C R E A T E */
  78. /*------------------------------------------------------------------------------
  79. ------------------------------------------------------------------------------*/
  80. BOOL CCandUIMMFile::Create( LPSTR szName, DWORD dwFlag, SECURITY_ATTRIBUTES *psa, DWORD dwSize )
  81. {
  82. DWORD flProtect;
  83. DWORD dwDesiredAccess;
  84. TCHAR achDesktopUniqueName[MAX_PATH];
  85. if (m_hFile != NULL) {
  86. return FALSE;
  87. }
  88. // access flag
  89. if ((dwFlag & CANDUIMM_READWRITE) != 0) {
  90. flProtect = PAGE_READWRITE;
  91. dwDesiredAccess = FILE_MAP_ALL_ACCESS;
  92. }
  93. else {
  94. flProtect = PAGE_READONLY;
  95. dwDesiredAccess = FILE_MAP_READ;
  96. }
  97. // create file
  98. GetDesktopUniqueName(szName, lstrlen(szName), achDesktopUniqueName, ARRAYSIZE(achDesktopUniqueName));
  99. m_hFile = CreateFileMapping(
  100. INVALID_HANDLE_VALUE,
  101. psa,
  102. flProtect,
  103. 0,
  104. dwSize,
  105. achDesktopUniqueName );
  106. if (m_hFile == NULL) {
  107. return FALSE;
  108. }
  109. // memory mapping
  110. m_pvData = MapViewOfFile( m_hFile, dwDesiredAccess, 0, 0, 0 );
  111. if (m_pvData == NULL) {
  112. Close();
  113. return FALSE;
  114. }
  115. // initialize
  116. memset( m_pvData, 0, dwSize );
  117. return TRUE;
  118. }
  119. /* C L O S E */
  120. /*------------------------------------------------------------------------------
  121. ------------------------------------------------------------------------------*/
  122. BOOL CCandUIMMFile::Close( void )
  123. {
  124. if (m_pvData != NULL) {
  125. UnmapViewOfFile( m_pvData );
  126. m_pvData = NULL;
  127. }
  128. if (m_hFile != NULL) {
  129. CloseHandle( m_hFile );
  130. m_hFile = NULL;
  131. }
  132. return TRUE;
  133. }
  134. //
  135. // CCandUIMutex
  136. //
  137. /* C C A N D U I M U T E X */
  138. /*------------------------------------------------------------------------------
  139. ------------------------------------------------------------------------------*/
  140. CCandUIMutex::CCandUIMutex( void )
  141. {
  142. m_hMutex = NULL;
  143. }
  144. /* ~ C C A N D U I M U T E X */
  145. /*------------------------------------------------------------------------------
  146. ------------------------------------------------------------------------------*/
  147. CCandUIMutex::~CCandUIMutex( void )
  148. {
  149. Close();
  150. }
  151. /* C R E A T E */
  152. /*------------------------------------------------------------------------------
  153. ------------------------------------------------------------------------------*/
  154. BOOL CCandUIMutex::Create( LPSTR szName, SECURITY_ATTRIBUTES *psa )
  155. {
  156. TCHAR achDesktopUniqueName[MAX_PATH];
  157. if (m_hMutex != NULL) {
  158. return FALSE;
  159. }
  160. GetDesktopUniqueName(szName, lstrlen(szName), achDesktopUniqueName, ARRAYSIZE(achDesktopUniqueName));
  161. m_hMutex = CreateMutex( psa, FALSE, achDesktopUniqueName );
  162. return (m_hMutex != NULL);
  163. }
  164. /* C L O S E */
  165. /*------------------------------------------------------------------------------
  166. ------------------------------------------------------------------------------*/
  167. BOOL CCandUIMutex::Close( void )
  168. {
  169. if (m_hMutex != NULL) {
  170. CloseHandle( m_hMutex );
  171. m_hMutex = NULL;
  172. }
  173. return TRUE;
  174. }
  175. /* L O C K */
  176. /*------------------------------------------------------------------------------
  177. ------------------------------------------------------------------------------*/
  178. BOOL CCandUIMutex::Lock( void )
  179. {
  180. DWORD dwResult;
  181. if (m_hMutex == NULL) {
  182. return FALSE;
  183. }
  184. dwResult = WaitForSingleObject( m_hMutex, INFINITE );
  185. switch (dwResult) {
  186. case WAIT_OBJECT_0:
  187. case WAIT_ABANDONED: {
  188. return TRUE;
  189. }
  190. default:
  191. case WAIT_TIMEOUT: {
  192. return FALSE;
  193. }
  194. }
  195. }
  196. /* U N L O C K */
  197. /*------------------------------------------------------------------------------
  198. ------------------------------------------------------------------------------*/
  199. BOOL CCandUIMutex::Unlock( void )
  200. {
  201. if (m_hMutex == NULL) {
  202. return FALSE;
  203. }
  204. ReleaseMutex( m_hMutex );
  205. return TRUE;
  206. }
  207. //
  208. // CCandUIShareMem
  209. //
  210. /* C C A N D U I S H A R E M E M */
  211. /*------------------------------------------------------------------------------
  212. Constructor of CCandUIShareMem
  213. ------------------------------------------------------------------------------*/
  214. CCandUIShareMem::CCandUIShareMem( void )
  215. {
  216. }
  217. /* ~ C C A N D U I S H A R E M E M */
  218. /*------------------------------------------------------------------------------
  219. Destructor of CCandUIShareMem
  220. ------------------------------------------------------------------------------*/
  221. CCandUIShareMem::~CCandUIShareMem( void )
  222. {
  223. m_Mutex.Close();
  224. }
  225. /* I N I T I A L I Z E */
  226. /*------------------------------------------------------------------------------
  227. ------------------------------------------------------------------------------*/
  228. BOOL CCandUIShareMem::Initialize( void )
  229. {
  230. return m_Mutex.Create( SZNAME_SHAREDDATA_MUTEX, GetCandUISecurityAttributes() );
  231. }
  232. /* O P E N */
  233. /*------------------------------------------------------------------------------
  234. ------------------------------------------------------------------------------*/
  235. BOOL CCandUIShareMem::Open( void )
  236. {
  237. return m_MMFile.Open( SZNAME_SHAREDDATA_MMFILE, CANDUIMM_READONLY );
  238. }
  239. /* C R E A T E */
  240. /*------------------------------------------------------------------------------
  241. ------------------------------------------------------------------------------*/
  242. BOOL CCandUIShareMem::Create( void )
  243. {
  244. BOOL fResult = FALSE;
  245. if (!m_Mutex.Lock()) {
  246. return FALSE;
  247. }
  248. fResult = m_MMFile.Create( SZNAME_SHAREDDATA_MMFILE, CANDUIMM_READWRITE, GetCandUISecurityAttributes(), sizeof(SHAREDDATA) );
  249. m_Mutex.Unlock();
  250. return fResult;
  251. }
  252. /* C L O S E */
  253. /*------------------------------------------------------------------------------
  254. ------------------------------------------------------------------------------*/
  255. BOOL CCandUIShareMem::Close( void )
  256. {
  257. return m_MMFile.Close();
  258. }
  259. /* L O C K D A T A */
  260. /*------------------------------------------------------------------------------
  261. ------------------------------------------------------------------------------*/
  262. BOOL CCandUIShareMem::LockData( void )
  263. {
  264. return m_MMFile.IsValid() && m_Mutex.Lock();
  265. }
  266. /* U N L O C K D A T A */
  267. /*------------------------------------------------------------------------------
  268. ------------------------------------------------------------------------------*/
  269. BOOL CCandUIShareMem::UnlockData( void )
  270. {
  271. return m_MMFile.IsValid() && m_Mutex.Unlock();
  272. }