[API]
CreateFileA
CreateFileW
GetFileSize
WriteFile
SetFilePointer
CloseHandle

[CODE]
Public Function WriteTxtFileLog(ByVal Filename As String, ByVal xLine As String) As Boolean
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  'Dim xLong4 As Long
  Dim xBool1 As Boolean
  Dim xBool2 As Boolean

'  Stop
  If IsUnicode Then
    xLong1 = CreateFileW(StrPtr(Filename), &HC0000000, &H0&, 0&, 4&, &H20&, 0&)
    Else
    xLong1 = CreateFileA(Filename, &HC0000000, &H0&, 0&, 4&, &H20&, 0&)
    End If
  If Not xLong1 = -1& Then
    xLong2 = GetFileSize(xLong1, xLong3)
    If xLong3 = 0& Then
      If xLong2 = 0& Then
        xBool1 = IsUnicode
        If xBool1 Then
          xLong2 = -257 ' FEFF '-2 ' FFFE
          If Not WriteFile(xLong1, ByVal VarPtr(xLong2), 2&, xLong3, 0&) Then
            If xLong3 = 2& Then
              xBool2 = True
              End If
            End If
          Else
          xBool2 = True
          End If
        ElseIf Not ReadFile(xLong1, ByVal VarPtr(xLong3), 2&, 0&, 0&) = 0& Then
        If (xLong3 And &HFFFF&) = &HFEFF& Then
          If xLong2 Mod 2& = 0& Then
            xBool1 = True
            xBool2 = True
            End If
          Else
          xBool1 = False
          xBool2 = True
          End If
        If xBool2 Then
          If SetFilePointer(xLong1, 0&, 0&, 2&) = -1& Then
            xBool2 = False
            End If
          End If
        End If
      If xBool2 Then
        xLine = xLine + vbCrLf
        xLong2 = Len(xLine)
        If xBool1 Then
          WriteTxtFileLog = Not (WriteFile(xLong1, ByVal StrPtr(xLine), xLong2 * 2&, xLong3, 0&) = 0&)
          Else
          WriteTxtFileLog = Not (WriteFile(xLong1, ByVal xLine, xLong2, xLong3, 0&) = 0&)
          End If
        End If
      End If
    Call CloseHandle(xLong1)
    End If
  End Function
