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.

128 lines
3.2 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include "mapfile.h"
  4. //-----------------------------------------------------------------------------
  5. // MappedFile
  6. //
  7. // A simple encapsulation of opening a mapped file in memory.
  8. // The file is opened with READ access only.
  9. // Client calls Base() to retrieve the base pointer of the mapped file.
  10. //-----------------------------------------------------------------------------
  11. MappedFile::MappedFile(
  12. VOID
  13. ) : m_hFile(INVALID_HANDLE_VALUE),
  14. m_hFileMapping(INVALID_HANDLE_VALUE),
  15. m_pbBase(NULL),
  16. m_llSize(0)
  17. {
  18. DBGTRACE((DM_MAPFILE, DL_HIGH, TEXT("MappedFile::MappedFile")));
  19. }
  20. MappedFile::~MappedFile(
  21. VOID
  22. )
  23. {
  24. DBGTRACE((DM_MAPFILE, DL_HIGH, TEXT("MappedFile::~MappedFile")));
  25. Close();
  26. }
  27. LONGLONG
  28. MappedFile::Size(
  29. VOID
  30. ) const
  31. {
  32. DBGTRACE((DM_MAPFILE, DL_MID, TEXT("MappedFile::Size")));
  33. return m_llSize;
  34. }
  35. //
  36. // Open the file. Caller retrieves the base pointer through the
  37. // Base() member function.
  38. //
  39. HRESULT
  40. MappedFile::Open(
  41. LPCTSTR pszFile
  42. )
  43. {
  44. DBGTRACE((DM_MAPFILE, DL_HIGH, TEXT("MappedFile::Open")));
  45. DBGPRINT((DM_MAPFILE, DL_HIGH, TEXT("\topening \"%s\""), pszFile));
  46. HRESULT hr = NO_ERROR;
  47. m_hFile = CreateFile(pszFile,
  48. GENERIC_READ,
  49. FILE_SHARE_READ,
  50. NULL,
  51. OPEN_EXISTING,
  52. 0,
  53. NULL);
  54. if (INVALID_HANDLE_VALUE == m_hFile)
  55. {
  56. hr = HRESULT_FROM_WIN32(GetLastError());
  57. }
  58. else
  59. {
  60. if ((m_hFileMapping = CreateFileMapping(m_hFile,
  61. NULL,
  62. PAGE_READONLY,
  63. 0,
  64. 0,
  65. NULL)) == NULL)
  66. {
  67. hr = HRESULT_FROM_WIN32(GetLastError());
  68. }
  69. else
  70. {
  71. m_pbBase = (LPBYTE)MapViewOfFile(m_hFileMapping,
  72. FILE_MAP_READ,
  73. 0,
  74. 0,
  75. 0);
  76. if (NULL == m_pbBase)
  77. {
  78. hr = HRESULT_FROM_WIN32(GetLastError());
  79. }
  80. else
  81. {
  82. ULARGE_INTEGER liSize;
  83. liSize.LowPart = GetFileSize(m_hFile, &liSize.HighPart);
  84. m_llSize = liSize.QuadPart;
  85. }
  86. }
  87. }
  88. return hr;
  89. }
  90. //
  91. // Close the file mapping and the file.
  92. //
  93. VOID
  94. MappedFile::Close(
  95. VOID
  96. )
  97. {
  98. DBGTRACE((DM_MAPFILE, DL_HIGH, TEXT("MappedFile::Close")));
  99. if (NULL != m_pbBase)
  100. {
  101. UnmapViewOfFile(m_pbBase);
  102. m_pbBase = NULL;
  103. }
  104. if (INVALID_HANDLE_VALUE != m_hFileMapping)
  105. {
  106. CloseHandle(m_hFileMapping);
  107. m_hFileMapping = INVALID_HANDLE_VALUE;
  108. }
  109. if (INVALID_HANDLE_VALUE != m_hFile)
  110. {
  111. CloseHandle(m_hFile);
  112. m_hFile = INVALID_HANDLE_VALUE;
  113. }
  114. }