3

我有以下 Visual Basic 6.0 函数,它通过 FTP 将 ANSI 字符串写入新文件。我希望它将文件写为 UTF-16LE。在以下方法中是否有任何好的方法可以做到这一点?

Public Sub writeToFile(ByVal FTPServer As String _
                 , ByVal userName As String _
                 , ByVal password As String _
                 , ByVal contents As String _
                 , ByVal destinationFile As String)

    Dim hFile As Long
    Dim lCount As Long

    inetOpen
    inetConnect FTPServer, userName, password
    hFile = apiFtpOpenFile(m_hFTP, destinationFile, GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY, 0&)
    If hFile = 0 Then
        Err.Raise EWMFTPErrorCodes.wmFTPSendError, , internetError
    End If

    If apiInternetWriteFile(hFile, contents, Len(contents), lCount) = 0 Then
        Err.Raise EWMFTPErrorCodes.wmFTPSendError, , internetError
    End If

    apiInternetCloseHandle hFile
End Sub

我已经有大约 10 年没有做过 Visual Basic 6.0 了,所以我充其量只是摇摇晃晃。任何投入将不胜感激。

这是 apiInternetWriteFile 声明;

Private Declare Function apiInternetWriteFile Lib "wininet.dll" Alias "InternetWriteFile" ( _
                         ByVal hFile As Long _
                       , ByVal lpBuffer As String _
                       , ByVal dwNumberOfBytesToWrite As Long _
                       , ByRef lpdwNumberOfBytesWritten As Long) As Long
4

1 回答 1

2

我们需要查看 apiInternetWriteFile 的声明。我很确定这是Declare一个 API 调用,可能WinINet.dll 中的某些内容。我的猜测是你需要:

  • 更改 Declare 以使其ByVal Long第二个参数需要 a
  • 编辑以在开始时获取 BOM,尝试Contents = ChrW(&HFEFF&) & Contents. 或者可能FFEF,不确定字节顺序。
  • 通过StrPtr(contents)第二个参数
  • 传递Len(contents)*2第三个参数(以字节为单位的长度)

这将传递一个 Unicode UTF-16 字符串作为内容参数

于 2011-03-31T08:25:25.070 回答