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.

253 lines
5.4 KiB

  1. // EmFile.cpp : Implementation of CEmFile
  2. #include "stdafx.h"
  3. #include "Emsvc.h"
  4. #include "EmFile.h"
  5. /////////////////////////////////////////////////////////////////////////////
  6. // CEmFile
  7. STDMETHODIMP CEmFile::InterfaceSupportsErrorInfo(REFIID riid)
  8. {
  9. static const IID* arr[] =
  10. {
  11. &IID_IEmFile
  12. };
  13. for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
  14. {
  15. if (::InlineIsEqualGUID(*arr[i],riid))
  16. return S_OK;
  17. }
  18. return S_FALSE;
  19. }
  20. CEmFile::CEmFile()
  21. {
  22. m_hEmFile = INVALID_HANDLE_VALUE;
  23. m_bstrFileName = NULL;
  24. }
  25. CEmFile::~CEmFile()
  26. {
  27. if( m_hEmFile != INVALID_HANDLE_VALUE ) {
  28. CloseHandle( m_hEmFile );
  29. m_hEmFile = INVALID_HANDLE_VALUE;
  30. }
  31. if( m_bstrFileName ) { SysFreeString( m_bstrFileName ); m_bstrFileName = NULL; }
  32. }
  33. STDMETHODIMP CEmFile::Read(void *pv, ULONG cb, ULONG *pcbRead)
  34. {
  35. _ASSERTE( pv != NULL );
  36. _ASSERTE( cb != 0L );
  37. _ASSERTE( m_hEmFile != INVALID_HANDLE_VALUE );
  38. HRESULT hr = E_FAIL;
  39. __try
  40. {
  41. if( pv == NULL || cb == 0L ) { hr = E_INVALIDARG; goto qRead; }
  42. if( m_hEmFile == INVALID_HANDLE_VALUE ) { hr = EMERROR_OBJECTNOTINITIALIZED; goto qRead; }
  43. if( ReadFile(
  44. m_hEmFile,
  45. pv,
  46. cb,
  47. pcbRead,
  48. NULL
  49. ) == false ) {
  50. hr = HRESULT_FROM_WIN32(GetLastError()); goto qRead;
  51. }
  52. hr = S_OK;
  53. qRead:
  54. if( FAILED(hr) ) {
  55. }
  56. }
  57. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  58. hr = E_UNEXPECTED;
  59. _ASSERTE( false );
  60. }
  61. return hr;
  62. }
  63. STDMETHODIMP CEmFile::Write(void const *pv, ULONG cb, ULONG *pcbWritten)
  64. {
  65. // TODO: Add your implementation code here
  66. return E_NOTIMPL;
  67. }
  68. STDMETHODIMP CEmFile::Seek(LARGE_INTEGER dlibMove, ULONG dwOrigin, ULARGE_INTEGER *plibNewPosition)
  69. {
  70. // TODO: Add your implementation code here
  71. return E_NOTIMPL;
  72. }
  73. STDMETHODIMP CEmFile::SetSize(ULARGE_INTEGER libNewSize)
  74. {
  75. // TODO: Add your implementation code here
  76. return E_NOTIMPL;
  77. }
  78. STDMETHODIMP CEmFile::CopyTo(IStream *pstm, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
  79. {
  80. // TODO: Add your implementation code here
  81. return E_NOTIMPL;
  82. }
  83. STDMETHODIMP CEmFile::Commit(DWORD grfCommitFlags)
  84. {
  85. // TODO: Add your implementation code here
  86. return E_NOTIMPL;
  87. }
  88. STDMETHODIMP CEmFile::Revert()
  89. {
  90. // TODO: Add your implementation code here
  91. return E_NOTIMPL;
  92. }
  93. STDMETHODIMP CEmFile::LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
  94. {
  95. // TODO: Add your implementation code here
  96. return E_NOTIMPL;
  97. }
  98. STDMETHODIMP CEmFile::UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
  99. {
  100. // TODO: Add your implementation code here
  101. return E_NOTIMPL;
  102. }
  103. STDMETHODIMP CEmFile::Stat(STATSTG *pstatstg, DWORD grfStatFlag)
  104. {
  105. // TODO: Add your implementation code here
  106. return E_NOTIMPL;
  107. }
  108. STDMETHODIMP CEmFile::Clone(IStream **ppstm)
  109. {
  110. // TODO: Add your implementation code here
  111. return E_NOTIMPL;
  112. }
  113. STDMETHODIMP CEmFile::InitFile(BSTR bstrFileName)
  114. {
  115. _ASSERTE( bstrFileName != NULL );
  116. HRESULT hr = E_FAIL;
  117. __try
  118. {
  119. if( bstrFileName == NULL ){ hr = E_INVALIDARG; goto qInitFile; }
  120. m_bstrFileName = SysAllocString( bstrFileName );
  121. _ASSERTE( m_bstrFileName != NULL );
  122. if( m_bstrFileName == NULL ) { hr = E_OUTOFMEMORY; goto qInitFile; }
  123. hr = CreateEmFile();
  124. if( FAILED(hr) ) { goto qInitFile; }
  125. hr = S_OK;
  126. qInitFile:
  127. if( FAILED(hr) ) {
  128. if( m_bstrFileName ) { SysFreeString( m_bstrFileName ); m_bstrFileName = NULL; }
  129. }
  130. }
  131. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  132. hr = E_UNEXPECTED;
  133. if( m_bstrFileName ) { SysFreeString( m_bstrFileName ); m_bstrFileName = NULL; }
  134. _ASSERTE( false );
  135. }
  136. return hr;
  137. }
  138. HRESULT
  139. CEmFile::CreateEmFile
  140. (
  141. IN DWORD dwDesiredAccess /*= GENERIC_READ*/,
  142. IN DWORD dwShareMode /*= FILE_SHARE_READ*/,
  143. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes /*= NULL*/,
  144. IN DWORD dwCreationDisposition /*= OPEN_EXISTING*/,
  145. IN DWORD dwFlagsAndAttributes /*= FILE_ATTRIBUTE_NORMAL*/,
  146. IN HANDLE hTemplateFile /*= NULL*/
  147. )
  148. {
  149. HRESULT hr = E_FAIL;
  150. DWORD dwLastRet = 0L;
  151. __try
  152. {
  153. m_hEmFile = ::CreateFile (
  154. m_bstrFileName,
  155. dwDesiredAccess,
  156. dwShareMode,
  157. lpSecurityAttributes,
  158. dwCreationDisposition,
  159. dwFlagsAndAttributes,
  160. hTemplateFile
  161. );
  162. if( m_hEmFile == INVALID_HANDLE_VALUE ) {
  163. hr = HRESULT_FROM_WIN32( GetLastError() );
  164. goto qCreateEmFile;
  165. }
  166. hr = S_OK;
  167. qCreateEmFile:
  168. if( FAILED(hr) ) {
  169. if( m_hEmFile != INVALID_HANDLE_VALUE ) {
  170. CloseHandle( m_hEmFile );
  171. m_hEmFile = INVALID_HANDLE_VALUE;
  172. }
  173. }
  174. }
  175. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  176. hr = E_UNEXPECTED;
  177. if( m_hEmFile != INVALID_HANDLE_VALUE ) {
  178. CloseHandle( m_hEmFile );
  179. m_hEmFile = INVALID_HANDLE_VALUE;
  180. }
  181. _ASSERTE( false );
  182. }
  183. return hr;
  184. }