通过 FTP 将 ANSI 字符串写入 Unicode 文件

发布于 2024-10-28 13:08:03 字数 1328 浏览 6 评论 0原文

我有以下 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

I have the following Visual Basic 6.0 function which writes an ANSI string to a new file over FTP. I would like it to write the file as UTF-16LE. Is there any good way to do that within this following method?

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

I haven't done Visual Basic 6.0 in about 10 years, so I'm shaky at best. Any input would be greatly appreciated.

Here is the apiInternetWriteFile declaration;

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悍妇囚夫 2024-11-04 13:08:03

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

  • 更改 Declare,以便第二个参数
  • EDIT 需要一个 ByVal Long 来在开始时获取 BOM,尝试 Contents = ChrW(&HFEFF&) &内容。或者可能是 FFEF,不确定字节顺序。
  • 为第二个参数传递 StrPtr(contents) 为第三个参数
  • 传递 Len(contents)*2 (长度以字节为单位)

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

We need to see the declaration for apiInternetWriteFile. I'm pretty sure it's a Declare into an API call, maybe something in WinINet.dll. My guess is that you need to:

  • Change the Declare so it expects a ByVal Long for the 2nd argument
  • EDIT to get a BOM at the start, try Contents = ChrW(&HFEFF&) & Contents. Or possibly FFEF, not sure of the endianness.
  • Pass StrPtr(contents) for the 2nd argument
  • Pass Len(contents)*2 for the 3rd argument (length in bytes)

That will pass a Unicode UTF-16 string as the contents argument

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文