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.

86 lines
2.3 KiB

  1. //Copyright (c) 1997-2000 Microsoft Corporation
  2. // Memory mapped file routines
  3. #include "precomp.h"
  4. #include "mappedfile.h"
  5. BOOL CMemMappedFile::Open(
  6. LPCTSTR szName, // name of the mapped file
  7. unsigned long ulMemSize // size of the mapped file
  8. )
  9. {
  10. // assumption: the code isn't going to call Open twice w/different szName
  11. if (!m_hMappedFile)
  12. {
  13. // Create the mapped file from system page file. If it has been created
  14. // previously, then CreateFileMapping acts like OpenFileMapping.
  15. m_hMappedFile = CreateFileMapping(
  16. INVALID_HANDLE_VALUE, // Current file handle.
  17. NULL, // Default security.
  18. PAGE_READWRITE, // Read/write permission.
  19. 0, // Hi-order DWORD of file size
  20. ulMemSize, // Lo-order DWORD of file size
  21. szName); // Name of mapping object.
  22. if (!m_hMappedFile)
  23. {
  24. return FALSE;
  25. }
  26. // Note if this is the first open for the file?
  27. m_fFirstOpen = (GetLastError() == ERROR_SUCCESS)?TRUE:FALSE;
  28. }
  29. return TRUE;
  30. }
  31. BOOL CMemMappedFile::AccessMem(
  32. void **ppvMappedAddr // returned pointer into memory
  33. )
  34. {
  35. if (IsBadWritePtr(ppvMappedAddr, sizeof(void *)))
  36. return FALSE;
  37. if (!m_hMappedFile)
  38. return FALSE;
  39. // Get a pointer to the mapped memory if we don't already have it
  40. if (!m_pvMappedAddr)
  41. {
  42. m_pvMappedAddr = MapViewOfFile(
  43. m_hMappedFile, // Handle to mapping object.
  44. FILE_MAP_ALL_ACCESS, // Read/write permission
  45. 0, // Max. object size.
  46. 0, // Size of hFile.
  47. 0); // Map entire file.
  48. *ppvMappedAddr = m_pvMappedAddr;
  49. }
  50. if (NULL == m_pvMappedAddr)
  51. {
  52. return FALSE;
  53. }
  54. return TRUE;
  55. }
  56. void CMemMappedFile::Close()
  57. {
  58. if (m_pvMappedAddr)
  59. {
  60. UnmapViewOfFile(m_pvMappedAddr);
  61. m_pvMappedAddr = 0;
  62. }
  63. if (m_hMappedFile)
  64. {
  65. CloseHandle(m_hMappedFile);
  66. m_hMappedFile = 0;
  67. }
  68. m_fFirstOpen = FALSE;
  69. }