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.

160 lines
4.4 KiB

  1. /****************************************************************************
  2. Copyright information : Copyright (c) 1998-1999 Microsoft Corporation
  3. File Name : OutputStream.cpp
  4. Project Name : WMI Command Line
  5. Author Name : C V Nandi
  6. Date of Creation (dd/mm/yy) : 9th-July-2001
  7. Version Number : 1.0
  8. Brief Description : This file consist of class implementation of
  9. class CFileOutputStream and CStackUnknown
  10. Revision History :
  11. Last Modified By : C V Nandi
  12. Last Modified Date : 10th-July-2001
  13. ******************************************************************************/
  14. #include "Precomp.h"
  15. #include "OutputStream.h"
  16. /*------------------------------------------------------------------------
  17. Name :Init
  18. Synopsis :This function initializes the handle to stream.
  19. Type :Member Function
  20. Input parameter :
  21. h - HANDLE, HANDLE to the stream.
  22. Output parameters :None
  23. Return Type :HRESULT
  24. Global Variables :None
  25. Calling Syntax :Init(hOutSteram)
  26. Notes :None
  27. ------------------------------------------------------------------------*/
  28. HRESULT CFileOutputStream::Init(HANDLE h)
  29. {
  30. m_hOutStream = h;
  31. m_bClose = false;
  32. return S_OK;
  33. }
  34. /*------------------------------------------------------------------------
  35. Name :Init
  36. Synopsis :Open local file named pwszFileName for writing.
  37. Type :Member Function
  38. Input parameter :
  39. pszFileName - Pointer to a string containing file name.
  40. Output parameters :None
  41. Return Type :HRESULT
  42. Global Variables :None
  43. Calling Syntax :Init(szFileName)
  44. Notes :Overloaded function.
  45. ------------------------------------------------------------------------*/
  46. HRESULT
  47. CFileOutputStream::Init(const _TCHAR * pszFileName)
  48. {
  49. HRESULT hr = S_OK;
  50. m_hOutStream =::CreateFile(
  51. pszFileName,
  52. GENERIC_WRITE,
  53. 0,
  54. NULL,
  55. CREATE_ALWAYS,
  56. FILE_ATTRIBUTE_NORMAL,
  57. NULL);
  58. if ( m_hOutStream == INVALID_HANDLE_VALUE )
  59. hr = S_FALSE;
  60. else
  61. m_bClose = TRUE;
  62. return hr;
  63. }
  64. /*------------------------------------------------------------------------
  65. Name :Close
  66. Synopsis :This function closes the handle to stream.
  67. Type :Member Function
  68. Input parameter :None
  69. Output parameters :None
  70. Return Type :void
  71. Global Variables :None
  72. Calling Syntax :Close();
  73. Notes :None
  74. ------------------------------------------------------------------------*/
  75. void CFileOutputStream::Close()
  76. {
  77. if (m_bClose)
  78. {
  79. ::CloseHandle(m_hOutStream);
  80. m_bClose = FALSE;
  81. }
  82. }
  83. /*------------------------------------------------------------------------
  84. Name :Write
  85. Synopsis :Implement ISequentialStream::Write by forwarding
  86. calls to WriteFile.
  87. Type :Member Function
  88. Input parameter :
  89. pv - Pointer to buffer containing data.
  90. cb - Number of bytes to be written
  91. Output parameters :
  92. pcbWritten - Number of bytes written.
  93. Return Type :HRESULT
  94. Global Variables :None
  95. Calling Syntax :Called by transform() function of IXSLProcessor.
  96. Notes :None
  97. ------------------------------------------------------------------------*/
  98. HRESULT STDMETHODCALLTYPE
  99. CFileOutputStream::Write(void const * pv, ULONG cb, ULONG * pcbWritten)
  100. {
  101. HRESULT hr = S_OK;
  102. void* p = const_cast < void* > ( pv );
  103. ULONG sizep = cb;
  104. LPWSTR psz = reinterpret_cast < LPWSTR > ( p );
  105. BOOL bSkip = FALSE;
  106. if ( psz )
  107. {
  108. if ( FILE_TYPE_DISK == GetFileType ( m_hOutStream ) )
  109. {
  110. if(SetFilePointer(m_hOutStream, 0, NULL, FILE_CURRENT))
  111. {
  112. // skip unicode signature 0xfffe
  113. BYTE *signature = NULL;
  114. signature = reinterpret_cast < BYTE* > ( psz );
  115. if ( signature [ 0 ] == 0xff && signature [ 1 ] == 0xfe )
  116. {
  117. psz++;
  118. bSkip = TRUE;
  119. }
  120. if ( bSkip )
  121. {
  122. p = reinterpret_cast < void* > ( psz );
  123. sizep = sizep - 2;
  124. }
  125. }
  126. }
  127. }
  128. if ( ::WriteFile(m_hOutStream,
  129. p,
  130. sizep,
  131. pcbWritten,
  132. NULL) == FALSE )
  133. {
  134. hr = S_FALSE;
  135. }
  136. // need to fake as we wrote multibytes here
  137. if ( bSkip )
  138. {
  139. * pcbWritten = ( * pcbWritten ) + 2;
  140. }
  141. return hr;
  142. }