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.

185 lines
4.3 KiB

  1. /*****************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1999 - 2000
  4. *
  5. * TITLE: mpdview.h
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: OrenR
  10. *
  11. * DATE: 2000/10/27
  12. *
  13. * DESCRIPTION: Implements CMappedView
  14. *
  15. *****************************************************************************/
  16. #ifndef _MPDVIEW_H_
  17. #define _MPDVIEW_H_
  18. class CMappedView
  19. {
  20. public:
  21. ///////////////////////////////
  22. // Constructor
  23. //
  24. CMappedView(LPCTSTR pszFile,
  25. LONG lSize,
  26. DWORD dwOpen) :
  27. m_pBits(NULL),
  28. m_hFile(INVALID_HANDLE_VALUE),
  29. m_hMap(NULL),
  30. m_lSize(lSize)
  31. {
  32. m_hFile = CreateFile(pszFile,
  33. GENERIC_READ | GENERIC_WRITE,
  34. FILE_SHARE_WRITE | FILE_SHARE_READ,
  35. NULL,
  36. dwOpen,
  37. FILE_ATTRIBUTE_NORMAL,
  38. NULL);
  39. if (m_hFile == INVALID_HANDLE_VALUE)
  40. {
  41. DBG_ERR(("CreateFile failed with LastError = %d",GetLastError()));
  42. }
  43. DoCreateFileMapping();
  44. }
  45. ///////////////////////////////
  46. // Constructor
  47. //
  48. CMappedView( HANDLE hFile, LONG lSize ) :
  49. m_pBits(NULL),
  50. m_hFile(hFile),
  51. m_hMap(INVALID_HANDLE_VALUE),
  52. m_lSize(lSize)
  53. {
  54. DoCreateFileMapping();
  55. }
  56. ///////////////////////////////
  57. // Destructor
  58. //
  59. ~CMappedView()
  60. {
  61. DBG_FN(("~CMappedView"));
  62. CloseAndRelease();
  63. }
  64. ///////////////////////////////
  65. // DoCreateFileMapping
  66. //
  67. void DoCreateFileMapping()
  68. {
  69. if (m_hFile != INVALID_HANDLE_VALUE)
  70. {
  71. m_hMap = CreateFileMapping(m_hFile,
  72. NULL,
  73. PAGE_READWRITE,
  74. 0,
  75. m_lSize,
  76. NULL);
  77. if (m_hMap)
  78. {
  79. m_pBits = (PBYTE)MapViewOfFileEx(
  80. m_hMap,
  81. FILE_MAP_READ | FILE_MAP_WRITE,
  82. 0,
  83. 0,
  84. 0,
  85. NULL);
  86. }
  87. else
  88. {
  89. DBG_ERR(("CreateFileMapping failed with LastError = %d",
  90. GetLastError()));
  91. }
  92. }
  93. }
  94. ///////////////////////////////
  95. // FileSize
  96. //
  97. LARGE_INTEGER FileSize()
  98. {
  99. LARGE_INTEGER li;
  100. li.QuadPart = 0;
  101. #ifdef WINNT
  102. GetFileSizeEx(m_hFile, &li);
  103. #else
  104. DWORD LowPart = 0;
  105. LowPart = GetFileSize(m_hFile, (DWORD *)&li.HighPart);
  106. if (LowPart != -1)
  107. {
  108. li.LowPart = LowPart;
  109. }
  110. #endif
  111. return li;
  112. }
  113. ///////////////////////////////
  114. // CloseAndRelease
  115. //
  116. void CloseAndRelease()
  117. {
  118. if (m_pBits)
  119. {
  120. if (!UnmapViewOfFile( m_pBits ))
  121. {
  122. DBG_ERR(("UnmapViewOfFile failed with LastError = %d",
  123. GetLastError()));
  124. }
  125. m_pBits = NULL;
  126. }
  127. if (m_hMap)
  128. {
  129. if (!CloseHandle( m_hMap ))
  130. {
  131. DBG_ERR(("CloseHandle( m_hMap ) failed with LastError = %s",
  132. GetLastError()));
  133. }
  134. m_hMap = NULL;
  135. }
  136. if (m_hFile != INVALID_HANDLE_VALUE)
  137. {
  138. if (!CloseHandle( m_hFile ))
  139. {
  140. DBG_ERR(("CloseHandle( m_hFile ) failed with LastError = %s",
  141. GetLastError()));
  142. }
  143. m_hFile = INVALID_HANDLE_VALUE;
  144. }
  145. }
  146. ///////////////////////////////
  147. // Bits
  148. //
  149. PBYTE Bits()
  150. {
  151. return m_pBits;
  152. }
  153. private:
  154. BYTE *m_pBits;
  155. HANDLE m_hFile;
  156. HANDLE m_hMap;
  157. LONG m_lSize;
  158. };
  159. #endif