[API]
LocalAlloc
LocalFree
RtlMoveMemory
WideCharToMultiByte
MultiByteToWideChar

[INCLUDE]
UIntSubUInt
UIntAddUInt

[CODE]
Public Function TxtFile_SetEncoding(ByVal hTxtFileIn As Long, ByVal Encoding As TxtFile_EncodingEnum, Optional ByVal ResetPosition As Boolean) As Boolean
  Dim xType1 As TxtFileReadStruct
  Dim xType2 As TxtFileReadStruct
  Dim hMemMB As Long
  Dim xSizeOrg As Long
  Dim xSizeNew As Long
  Dim xLong1 As Long
  'Const FindCRLF As String = vbCrLf

  If Not hTxtFileIn = 0& Then
    Call RtlMoveMemory(xType1, ByVal hTxtFileIn, TxtFileReadStructLen)
    If Not xType1.hMem = 0& Then
      If xType1.Encoding = Encoding Then
        TxtFile_SetEncoding = True
        ElseIf xType1.Encoding = TxtFile_EncodingEnum.CP_ACP And IsUnicode Then
        'ElseIf xType1.Encoding = TxtFile_EncodingEnum.CP_ACP Then
        If Encoding = TxtFile_EncodingEnum.CP_UTF8 Then
          xSizeOrg = xType1.hMemEnd
          UIntSubUInt xSizeOrg, xType1.hMem
          xSizeOrg = xSizeOrg / 2&
          hMemMB = LocalAlloc(0&, xSizeOrg + 2&)
          If Not hMemMB = 0& Then
            If WideCharToMultiByte(0&, 0&, ByVal xType1.hMem, xSizeOrg, ByVal hMemMB, xSizeOrg + 1&, ByVal 0&, ByVal 0&) = xSizeOrg Then
              xSizeNew = MultiByteToWideChar(Encoding, 0&, ByVal hMemMB, xSizeOrg, 0&, 0&)
              If Not xSizeNew = 0& Then
                xType2.hMem = LocalAlloc(0&, xSizeNew * 2& + 8&)
                If Not xType2.hMem = 0& Then
                  If ResetPosition Or xType1.hMemPos = xType1.hMem Then
                    xType2.hMemPos = xType2.hMem
                    Else
                    xLong1 = xType1.hMemPos
                    UIntSubUInt xLong1, xType1.hMem
                    xLong1 = xLong1 / 2&
                    xType2.hMemPos = MultiByteToWideChar(Encoding, 0&, ByVal hMemMB, xLong1, 0&, 0&)
                    UIntAddUInt xType2.hMemPos, xType2.hMem
                    End If
                  xLong1 = MultiByteToWideChar(Encoding, 0&, ByVal hMemMB, xSizeOrg, ByVal xType2.hMem, xSizeNew + 1&)
                  If xLong1 = xSizeNew Then
                    xType2.Encoding = Encoding
                    xType2.hMemEnd = xType2.hMem
                    xSizeNew = xSizeNew * 2&
                    UIntAddUInt xType2.hMemEnd, xSizeNew
                    Call LocalFree(xType1.hMem)
                    Call RtlMoveMemory(ByVal hTxtFileIn, xType2, TxtFileReadStructLen)
                    Call RtlMoveMemory(ByVal xType2.hMemEnd, ByVal StrPtr(vbCrLf), 4&)
                    UIntAddUInt xType2.hMemEnd, 4&
                    Call RtlMoveMemory(ByVal xType2.hMemEnd, ByVal StrPtr(vbNullChar), 2&)
                    TxtFile_SetEncoding = True
                    GoTo Success
                    End If
                  Call LocalFree(xType2.hMem)
Success:
                  End If
                End If
              End If
            Call LocalFree(hMemMB)
            End If
          End If
        End If
      End If
    End If
  End Function
