[API]
SetEndOfFile
WriteFile

[INCLUDE]
TxtFile

[DEFINE]
Private FileApi_Handle As Long

[CODE]
Public Function TxtFile_Create(ByVal Filename As String) As Boolean 'append option-> seektoend
  Dim xLong1 As Long
  Dim xInt1 As Integer

  If IsUnicode Then
    FileApi_Handle = CreateFileW(ByVal StrPtr(Filename), &H40000000, &H1&, 0&, 2&, &H20&, 0&)
    If Not FileApi_Handle = -1& Then
      If Not SetEndOfFile(FileApi_Handle) = 0& Then
        xInt1 = -257 ' FEFF '-2 ' FFFE
        If Not WriteFile(FileApi_Handle, ByVal VarPtr(xInt1), 2&, xLong1, 0&) Then
          If xLong1 = 2& Then
            TxtFile_Create = True
            End If
          End If
        End If
      End If
    Else
    FileApi_Handle = CreateFileA(Filename, &H40000000, &H1&, 0&, 2&, &H20&, 0&)
    If Not FileApi_Handle = -1& Then
      If Not SetEndOfFile(FileApi_Handle) = 0& Then
        TxtFile_Create = True
        End If
      End If
    End If
  If Not (FileApi_Handle = -1& Or TxtFile_Create) Then
    CloseHandle FileApi_Handle
    FileApi_Handle = -1&
    End If
  End Function

Public Sub TxtFile_WriteLine(ByVal xLine As String)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  
  xLine = xLine + vbCrLf
  If Not FileApi_Handle = -1& Then
    xLong1 = Len(xLine)
    If IsUnicode Then
      xLong1 = xLong1 * 2&
      xLong3 = WriteFile(FileApi_Handle, ByVal StrPtr(xLine), xLong1, xLong2, 0&)
      Else
      xLong3 = WriteFile(FileApi_Handle, ByVal xLine, xLong1, xLong2, 0&)
      End If
    
    If xLong3 = 0& Or Not xLong1 = xLong2 Then
      Call CloseHandle(FileApi_Handle)
      FileApi_Handle = -1&
      End If
    End If
  End Sub

Public Sub TxtFile_WriteClose()
  If Not FileApi_Handle = 0& Then
    Call SetEndOfFile(FileApi_Handle)
    Call CloseHandle(FileApi_Handle)
    FileApi_Handle = -1&
    End If
  End Sub
