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.

231 lines
4.1 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. umfuncs.c
  5. Abstract:
  6. Help functions to map file into memory
  7. Environment:
  8. Windows NT printer drivers
  9. Revision History:
  10. 08/13/96 -davidx-
  11. Created it.
  12. --*/
  13. #include "lib.h"
  14. #if defined(KERNEL_MODE) && !defined(USERMODE_DRIVER)
  15. HFILEMAP
  16. MapFileIntoMemory(
  17. IN LPCTSTR ptstrFilename,
  18. OUT PVOID *ppvData,
  19. OUT PDWORD pdwSize
  20. )
  21. /*++
  22. Routine Description:
  23. Map a file into process memory space.
  24. Arguments:
  25. ptstrFilename - Specifies the name of the file to be mapped
  26. ppvData - Points to a variable for returning mapped memory address
  27. pdwSize - Points to a variable for returning the size of the file
  28. Return Value:
  29. Handle to identify the mapped file, NULL if there is an error
  30. --*/
  31. {
  32. HANDLE hModule = NULL;
  33. DWORD dwSize;
  34. if (hModule = EngLoadModule((PWSTR) ptstrFilename))
  35. {
  36. if (*ppvData = EngMapModule(hModule, &dwSize))
  37. {
  38. if (pdwSize)
  39. *pdwSize = dwSize;
  40. }
  41. else
  42. {
  43. TERSE(("EngMapModule failed: %d\n", GetLastError()));
  44. EngFreeModule(hModule);
  45. hModule = NULL;
  46. }
  47. }
  48. else
  49. ERR(("EngLoadModule failed: %d\n", GetLastError()));
  50. return (HFILEMAP) hModule;
  51. }
  52. VOID
  53. UnmapFileFromMemory(
  54. IN HFILEMAP hFileMap
  55. )
  56. /*++
  57. Routine Description:
  58. Unmap a file from memory
  59. Arguments:
  60. hFileMap - Identifies a file previously mapped into memory
  61. Return Value:
  62. NONE
  63. --*/
  64. {
  65. ASSERT(hFileMap != NULL);
  66. EngFreeModule((HANDLE) hFileMap);
  67. }
  68. #else // !KERNEL_MODE
  69. HFILEMAP
  70. MapFileIntoMemory(
  71. IN LPCTSTR ptstrFilename,
  72. OUT PVOID *ppvData,
  73. OUT PDWORD pdwSize
  74. )
  75. /*++
  76. Routine Description:
  77. Map a file into process memory space.
  78. Arguments:
  79. ptstrFilename - Specifies the name of the file to be mapped
  80. ppvData - Points to a variable for returning mapped memory address
  81. pdwSize - Points to a variable for returning the size of the file
  82. Return Value:
  83. Handle to identify the mapped file, NULL if there is an error
  84. --*/
  85. {
  86. HANDLE hFile, hFileMap;
  87. //
  88. // Open a handle to the specified file
  89. //
  90. hFile = CreateFile(ptstrFilename,
  91. GENERIC_READ,
  92. FILE_SHARE_READ,
  93. NULL,
  94. OPEN_EXISTING,
  95. FILE_ATTRIBUTE_NORMAL | SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS,
  96. NULL);
  97. if (hFile == INVALID_HANDLE_VALUE)
  98. {
  99. TERSE(("CreateFile failed: %d\n", GetLastError()));
  100. return NULL;
  101. }
  102. //
  103. // Obtain the file size if requested
  104. //
  105. if (pdwSize != NULL)
  106. {
  107. *pdwSize = GetFileSize(hFile, NULL);
  108. if (*pdwSize == 0xFFFFFFFF)
  109. {
  110. ERR(("GetFileSize failed: %d\n", GetLastError()));
  111. CloseHandle(hFile);
  112. return NULL;
  113. }
  114. }
  115. //
  116. // Map the file into memory
  117. //
  118. hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  119. if (hFileMap != NULL)
  120. {
  121. *ppvData = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
  122. CloseHandle(hFileMap);
  123. }
  124. else
  125. {
  126. ERR(("CreateFileMapping failed: %d\n", GetLastError()));
  127. *ppvData = NULL;
  128. }
  129. //
  130. // We can safely close both the file mapping object and the file object itself.
  131. //
  132. CloseHandle(hFile);
  133. //
  134. // The identifier for the mapped file is simply the starting memory address.
  135. //
  136. return (HFILEMAP) *ppvData;
  137. }
  138. VOID
  139. UnmapFileFromMemory(
  140. IN HFILEMAP hFileMap
  141. )
  142. /*++
  143. Routine Description:
  144. Unmap a file from memory
  145. Arguments:
  146. hFileMap - Identifies a file previously mapped into memory
  147. Return Value:
  148. NONE
  149. --*/
  150. {
  151. ASSERT(hFileMap != NULL);
  152. UnmapViewOfFile((PVOID) hFileMap);
  153. }
  154. #endif // !KERNEL_MODE