Source code of Windows XP (NT5)
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.

135 lines
3.2 KiB

  1. /*****************************************************************************************************************
  2. FILE: LoadFile.cpp
  3. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  4. FUNCTION:
  5. This module reads the specified file from the disk and loads it into memory returning
  6. a handle to the memory
  7. INPUT + OUTPUT:
  8. IN cFileName - is a valid file name
  9. IN pdwFileSize - specifies how many bytes to read - if zero then the size of the memory
  10. is checked and this value is calculated
  11. IN dwSharing - is the DWORD to pass to CreateFile about file sharing
  12. IN dwCreate - is the DWORD to pass to CreateFile about file creation
  13. GLOBALS:
  14. None.
  15. RETURN:
  16. Valid handle to the memory which holds the specified file
  17. NULL if not succesful
  18. */
  19. #include "stdafx.h"
  20. #include <windows.h>
  21. #include "ErrMacro.h"
  22. HANDLE
  23. LoadFile(
  24. IN PTCHAR cLoadFileName,
  25. IN PDWORD pdwFileSize,
  26. IN DWORD dwSharing,
  27. IN DWORD dwCreate
  28. )
  29. {
  30. HANDLE hFileHandle = INVALID_HANDLE_VALUE; // File handle
  31. DWORD dwFileSizeHi; // Hi word of file size
  32. DWORD dwFileSizeLo = 0xFFFFFFFF; // Lo word of file size and initialize
  33. DWORD dwReadTotal = 0; // Total bytes read during ReadFile
  34. HANDLE hMemory = NULL; // Memory handle
  35. LPVOID pMemory = NULL; // Memory pointer
  36. DWORD dwLastError = 0;
  37. BOOL bReturn = FALSE;
  38. __try{
  39. // Get the file handle.
  40. if((hFileHandle = CreateFile(cLoadFileName,
  41. 0,
  42. dwSharing,
  43. NULL,
  44. dwCreate,
  45. FILE_ATTRIBUTE_NORMAL,
  46. NULL)) == INVALID_HANDLE_VALUE) {
  47. // No error handling here! The calling function may wish
  48. // to handle different situations in different ways. Save the error.
  49. dwLastError = GetLastError();
  50. __leave;
  51. }
  52. // Get the file size.
  53. if (*pdwFileSize != 0) {
  54. dwFileSizeLo = *pdwFileSize;
  55. }
  56. else{
  57. // File size not passed in so calculate it
  58. dwFileSizeLo = GetFileSize(hFileHandle,&dwFileSizeHi);
  59. if (dwFileSizeLo == 0xFFFFFFFF){
  60. EH_ASSERT(FALSE);
  61. __leave;
  62. }
  63. }
  64. // Set the file size to the actual size of the file for the calling function.
  65. *pdwFileSize = dwFileSizeLo;
  66. // Return a NULL pointer if the file size is 0.
  67. if(dwFileSizeLo == 0){
  68. __leave;
  69. }
  70. // Allocate memory for the file.
  71. hMemory = GlobalAlloc(GHND, dwFileSizeLo);
  72. if (!hMemory){
  73. EH_ASSERT(FALSE);
  74. __leave;
  75. }
  76. // Lock the memory and get pointer to the memory.
  77. pMemory = GlobalLock(hMemory);
  78. if (!pMemory){
  79. EH_ASSERT(FALSE);
  80. __leave;
  81. }
  82. // Read the file into memory.
  83. if (!ReadFile(hFileHandle, pMemory, dwFileSizeLo, &dwReadTotal,0)){
  84. __leave;
  85. }
  86. bReturn = TRUE; // all is ok
  87. }
  88. __finally {
  89. // Close the file handle.
  90. if(hFileHandle != INVALID_HANDLE_VALUE){
  91. CloseHandle(hFileHandle);
  92. }
  93. // Success so unlock the memory and return the memory handle.
  94. if(bReturn) {
  95. GlobalUnlock(hMemory);
  96. }
  97. else { // had an error
  98. // So cleanup and return NULL.
  99. if(hMemory){
  100. EH_ASSERT(GlobalUnlock(hMemory) == FALSE);
  101. EH_ASSERT(GlobalFree(hMemory) == NULL);
  102. hMemory = NULL;
  103. }
  104. // Recover the error if any since another API may have cleared it.
  105. if(dwLastError){
  106. SetLastError(dwLastError);
  107. }
  108. }
  109. }
  110. return hMemory;
  111. }