Results 1 to 9 of 9

Thread: [RESOLVED] WideCharToMultiByte problem

Threaded View

  1. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Re: WideCharToMultiByte problem

    I'm facing a problem right now. Why this code below causes crash after executing CopyMemory?
    VB Code:
    1. Private Declare Function WideCharToMultiByte Lib "Kernel32.dll" ( _
    2.     ByVal CodePage As Long, _
    3.     ByVal dwFlags As Long, _
    4.     ByVal lpWideCharStr As Long, _
    5.     ByVal cchWideChar As Long, _
    6.     ByVal lpMultiByteStr As Long, _
    7.     ByVal cbMultiByte As Long, _
    8.     ByVal lpDefaultChar As Long, _
    9.     ByVal lpUsedDefaultChar As Long _
    10. ) As Long
    11.  
    12. Private Const CP_ACP        As Long = 0
    13.  
    14. Private Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal _
    15.         dest As Long, ByVal src As Long, ByVal Length As Long) As Long
    16.  
    17. Private Const BINARY_CHUNK As Long = 256
    18.  
    19. Private Function ToCPString(ByRef the_sValue As String) As Byte()
    20.  
    21.     Dim abytOutput()                As Byte
    22.     Dim nValueLen                   As Long
    23.     Dim nOutputByteLen              As Long
    24.  
    25.     ' Cache the input length.
    26.     nValueLen = Len(the_sValue)
    27.  
    28.     ' See how big the output buffer will be.
    29.     nOutputByteLen = WideCharToMultiByte(CP_ACP, 0&, StrPtr(the_sValue), nValueLen, 0&, 0&, 0&, 0&)
    30.  
    31.     If nOutputByteLen > 0 Then
    32.         ' Resize output byte array to the size of the UTF-8 string.
    33.         ReDim abytOutput(1 To nOutputByteLen)
    34.  
    35.         ' Make this API call again, this time giving a pointer to the output byte array.
    36.         WideCharToMultiByte CP_ACP, 0&, StrPtr(the_sValue), nValueLen, VarPtr(abytOutput(1)), nOutputByteLen, 0&, 0&
    37.     End If
    38.  
    39.     ' Return the array.
    40.     ToCPString = abytOutput()
    41.  
    42. End Function
    43.  
    44. Private Sub CatBinary(bytData() As Byte, Bytes() As Byte)
    45.     Dim BytesLen As Long, BinaryNext As Long
    46.    
    47.     BinaryNext = UBound(bytData) + 1
    48.     BytesLen = UBound(Bytes) - LBound(Bytes) + 1
    49.     If BinaryNext + BytesLen - 1 > BinaryNext Then
    50.         If BytesLen > BINARY_CHUNK Then
    51.             ReDim Preserve bytData(BinaryNext + BytesLen - 1)
    52.         Else
    53.             ReDim Preserve bytData(BinaryNext + BINARY_CHUNK - 1)
    54.         End If
    55.     End If
    56.     CopyMemory bytData(BinaryNext), Bytes(LBound(Bytes)), BytesLen
    57. End Sub
    58.  
    59. Private Sub CatBinaryString(bytData() As Byte, Text As String)
    60.     Dim Bytes() As Byte
    61.    
    62.     Bytes = ToCPString(Text)
    63.     CatBinary bytData, Bytes
    64. End Sub
    65.  
    66. Private Sub TrimBinary(bytData() As Byte)
    67.     If UBound(bytData) > 0 Then
    68.         ReDim Preserve bytData(UBound(bytData) - 1)
    69.     Else
    70.         bytData = ""
    71.     End If
    72. End Sub
    73.  
    74. Sub Main()
    75. Dim bytData() As Byte: bytData = ""
    76. CatBinaryString bytData, "test"
    77. MsgBox StrConv(bytData, vbUnicode)
    78. End Sub
    Last edited by MikiSoft; Apr 13th, 2015 at 08:30 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width