[DEVINTERN]
 - Pointer and buffer valid until closed.
 - UTF8/16 files currently just blocked on Win9x, regardless if unicode at all... unicode causes vb error using strcomp!
 - SetEncoding converts full context, so possible enabling reprocessing / seek to position '0'
   (Re)setPosition function will come eventually
 - On zero size, all data is 0, means no buffer, all positions 0

[API]
LocalAlloc
LocalFree
RtlMoveMemory
ReadFile
CloseHandle
MultiByteToWideChar

[INCLUDE]
TxtFile_ReadOpenEx
UIntAddUInt

[DEFINE]
Private Type TxtFileReadStruct
  hMem As Long
  ' Same position as in Write Struct!!!!
  Encoding As Long
  hMemPos As Long
  #If TxtFile_SetEncoding Or TxtFile_ReadLine Or TxtFile_ReadCompleteString Then
    hMemEnd As Long
    #End If
  End Type
#If TxtFile_SetEncoding Or TxtFile_ReadLine Or TxtFile_ReadCompleteString Then
  Private Const TxtFileReadStructLen As Long = 16&
  #Else
  Private Const TxtFileReadStructLen As Long = 12&
  #End If

[CODE]
Public Function TxtFile_ReadOpen(ByVal Filename As String, Optional ByVal DefaultEncoding As TxtFile_EncodingEnum = CP_ACP) As Long
  Dim hFile As Long
  Dim xSize As Long
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim hMemTmp As Long
  Dim xType1 As TxtFileReadStruct

  hFile = TxtFile_ReadOpenEx(Filename, DefaultEncoding, xSize, False)
  If Not hFile = -1& Then
    If xSize = 0& Then
      TxtFile_ReadOpen = LocalAlloc(&H40&, TxtFileReadStructLen)
      ElseIf DefaultEncoding = TxtFile_EncodingEnum.CP_WINUNICODE Then
      xType1.hMem = LocalAlloc(0&, xSize + 6&)
      If Not xType1.hMem = 0& Then
        If Not ReadFile(hFile, ByVal xType1.hMem, xSize, xLong1, 0&) = 0& Then
          If xSize = xLong1 Then GoTo Success1
          End If
        End If
      Else
      hMemTmp = LocalAlloc(0&, xSize + 1&)
      If Not hMemTmp = 0& Then
        If Not ReadFile(hFile, ByVal hMemTmp, xSize, xLong1, 0&) = 0& Then
          If xSize = xLong1 Then
            xLong1 = MultiByteToWideChar(DefaultEncoding, 0&, ByVal hMemTmp, xSize, 0&, 0&)
            If Not xLong1 = 0& Then
              xType1.hMem = LocalAlloc(0&, xLong1 * 2& + 8&)
              If Not xType1.hMem = 0& Then
                xLong2 = MultiByteToWideChar(DefaultEncoding, 0&, ByVal hMemTmp, xSize, ByVal xType1.hMem, xLong1)
                If xLong1 = xLong2 Then
                  xSize = xLong1 * 2
Success1:
                  xType1.Encoding = DefaultEncoding
                  TxtFile_ReadOpen = LocalAlloc(0&, TxtFileReadStructLen)
                  If Not TxtFile_ReadOpen = 0& Then
                    xType1.hMemPos = xType1.hMem
                    UIntAddUInt xSize, xType1.hMem
                    #If TxtFile_SetEncoding Or TxtFile_ReadLine Or TxtFile_ReadCompleteString Then
                      xType1.hMemEnd = xSize
                      #End If
                    Call RtlMoveMemory(ByVal TxtFile_ReadOpen, xType1, TxtFileReadStructLen)
                    Call RtlMoveMemory(ByVal xSize, ByVal StrPtr(vbCrLf), 4&)
                    UIntAddUInt xLong1, 4&
                    Call RtlMoveMemory(ByVal xSize, ByVal StrPtr(vbNullChar), 2&)
                    GoTo Success2
                    End If
                  End If
                Call LocalFree(xType1.hMem)
                End If
              End If
            End If
          End If
Success2:
        If Not hMemTmp = 0& Then
          Call LocalFree(hMemTmp)
          End If
        End If
      End If
    Call CloseHandle(hFile)
    End If
  End Function

Public Sub TxtFile_ReadClose(ByRef hTxtFileIn As Long)
  Dim xType1 As TxtFileReadStruct

  If Not hTxtFileIn = 0& Then
    Call RtlMoveMemory(xType1, ByVal hTxtFileIn, TxtFileReadStructLen)
    If Not xType1.hMem = 0& Then
      Call LocalFree(xType1.hMem)
      End If
    Call LocalFree(hTxtFileIn)
    hTxtFileIn = 0&
    End If
  End Sub
