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.

163 lines
3.3 KiB

  1. #include "stdafx.h"
  2. #include "rwfile.h"
  3. CRWFile::CRWFile()
  4. {
  5. IRWFile::m_hFile = INVALID_HANDLE_VALUE;
  6. }
  7. CRWFile::~CRWFile()
  8. {
  9. if( IRWFile::m_hFile != INVALID_HANDLE_VALUE ) {
  10. CloseHandle( IRWFile::m_hFile );
  11. }
  12. }
  13. HRESULT CRWFile::InitFile
  14. (
  15. IN LPCTSTR lpFileName,
  16. IN DWORD dwDesiredAccess,
  17. IN DWORD dwShareMode,
  18. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  19. IN DWORD dwCreationDisposition,
  20. IN DWORD dwFlagsAndAttributes,
  21. IN HANDLE hTemplateFile
  22. )
  23. {
  24. _ASSERTE( lpFileName != NULL );
  25. HRESULT hr = E_FAIL;
  26. __try
  27. {
  28. if( !lpFileName ) { hr = E_INVALIDARG; goto qInitFile; }
  29. m_hFile = CreateFile(
  30. lpFileName,
  31. dwDesiredAccess,
  32. dwShareMode,
  33. lpSecurityAttributes,
  34. dwCreationDisposition,
  35. dwFlagsAndAttributes,
  36. hTemplateFile
  37. );
  38. if( m_hFile == INVALID_HANDLE_VALUE ) {
  39. hr = HRESULT_FROM_WIN32(GetLastError());
  40. goto qInitFile;
  41. }
  42. hr = S_OK;
  43. qInitFile:
  44. if( FAILED(hr) ) {}
  45. }
  46. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  47. hr = E_UNEXPECTED;
  48. _ASSERTE( false );
  49. }
  50. return hr;
  51. }
  52. HRESULT CRWFile::Read
  53. (
  54. OUT LPVOID lpBuffer,
  55. IN DWORD nNumberOfBytesToRead,
  56. OUT LPDWORD lpNumberOfBytesRead,
  57. OUT LPOVERLAPPED lpOverlapped
  58. )
  59. {
  60. _ASSERTE( lpBuffer != NULL );
  61. _ASSERTE( nNumberOfBytesToRead > 0 );
  62. HRESULT hr = E_FAIL;
  63. BOOL bRet = false;
  64. __try
  65. {
  66. if( !lpBuffer || nNumberOfBytesToRead <= 0 ){
  67. hr = E_INVALIDARG; goto qRead;
  68. }
  69. bRet = ReadFile(
  70. m_hFile,
  71. lpBuffer,
  72. nNumberOfBytesToRead,
  73. lpNumberOfBytesRead,
  74. lpOverlapped
  75. );
  76. if( bRet && *lpNumberOfBytesRead == 0 ) { // crossed beyond the end of the file.
  77. hr = S_FALSE; goto qRead;
  78. }
  79. if( !bRet ) {
  80. hr = HRESULT_FROM_WIN32(GetLastError()); goto qRead;
  81. }
  82. hr = S_OK;
  83. qRead:
  84. if( FAILED(hr) ){}
  85. }
  86. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  87. hr = E_UNEXPECTED;
  88. _ASSERTE( false );
  89. }
  90. return hr;
  91. }
  92. HRESULT CRWFile::Write
  93. (
  94. OUT LPCVOID lpBuffer,
  95. IN DWORD nNumberOfBytesToWrite,
  96. OUT LPDWORD lpNumberOfBytesWritten,
  97. OUT LPOVERLAPPED lpOverLapped
  98. )
  99. {
  100. _ASSERTE( lpBuffer != NULL );
  101. HRESULT hr = E_FAIL;
  102. BOOL bRet = false;
  103. __try
  104. {
  105. if( !lpBuffer ) { hr = E_INVALIDARG; goto qWrite; }
  106. bRet = WriteFile(
  107. m_hFile,
  108. lpBuffer,
  109. nNumberOfBytesToWrite,
  110. lpNumberOfBytesWritten,
  111. lpOverLapped
  112. );
  113. if( !bRet ) {
  114. hr = HRESULT_FROM_WIN32(GetLastError()); goto qWrite;
  115. }
  116. hr = S_OK;
  117. qWrite:
  118. if( FAILED(hr) ){}
  119. }
  120. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  121. hr = E_UNEXPECTED;
  122. _ASSERTE( false );
  123. }
  124. return hr;
  125. }