Home All Groups Group Topic Archive Search About

Problem converting bytes array into string

Author
5 Mar 2006 5:42 AM
Jack
Hello,
What is wrong in the following code:
    Dim cds As COPYDATASTRUCT
    Dim buf(1 To 255) As Byte

    Call CopyMemory(cds, ByVal lParam, Len(cds))
    Call CopyMemory(buf(1), ByVal cds.lpData, cds.cbData)
    lpString = StrConv(buf, vbUnicode)

In the above code lpString is empty, despite the fact that there is data
sitting in buf() array.
What puzzles me is that I had that code working before.
Jack

Author
5 Mar 2006 11:24 AM
Michael C
Show quote Hide quote
"Jack" <replyTo@newsgroup> wrote in message
news:elC7YeBQGHA.456@TK2MSFTNGP15.phx.gbl...
> Hello,
> What is wrong in the following code:
>    Dim cds As COPYDATASTRUCT
>    Dim buf(1 To 255) As Byte
>
>    Call CopyMemory(cds, ByVal lParam, Len(cds))
>    Call CopyMemory(buf(1), ByVal cds.lpData, cds.cbData)
>    lpString = StrConv(buf, vbUnicode)
>
> In the above code lpString is empty, despite the fact that there is data
> sitting in buf() array.
> What puzzles me is that I had that code working before.

Not sure but Len should be LenB in there but that's prolly not the
difference.

Show quoteHide quote
> Jack
>
>
Author
5 Mar 2006 1:18 PM
Mike D Sutton
> What is wrong in the following code:
>     Dim cds As COPYDATASTRUCT
>     Dim buf(1 To 255) As Byte
>
>     Call CopyMemory(cds, ByVal lParam, Len(cds))
>     Call CopyMemory(buf(1), ByVal cds.lpData, cds.cbData)
>     lpString = StrConv(buf, vbUnicode)
>
> In the above code lpString is empty, despite the fact that there is data
> sitting in buf() array.
> What puzzles me is that I had that code working before.

Try this:

'***
Private Declare Sub RtlMoveMemory Lib "Kernel32.dll" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As
Long)

Private Type CopyDataStruct
    dwData As Long
    cbData As Long
    lpData As Long
End Type

Public Event DoCopyData(ByVal inWnd As Long, ByRef inData() As Byte, ByVal inDataLen As Long)

....

Private Function OnCopyData(ByVal inWParam As Long, ByVal inLParam As Long) As Long
    Dim CDS As CopyDataStruct
    Dim DataBuf() As Byte
    Dim DataStr As String

    If (inLParam) Then ' Copy CopyDataStruct locally
        Call RtlMoveMemory(CDS, ByVal inLParam, Len(CDS))

        ' Validate CDS data
        If ((CDS.cbData > 0) And (CDS.lpData <> 0)) Then
            ReDim DataBuf(0 To CDS.cbData - 1) As Byte
            Call RtlMoveMemory(DataBuf(0), ByVal CDS.lpData, CDS.cbData)

            ' If data is ASCII:
            DataStr = StrConv(DataBuf, vbUnicode)

            ' If data is Unicode:
            ' DataStr = DataBuf

            ' Raise event with data
            RaiseEvent DoCopyData(inWParam, DataBuf, CDS.cbData)
        Else ' Raise event with no data
            RaiseEvent DoCopyData(inWParam, DataBuf, 0&)
        End If

        OnCopyData = 1&
    End If
End Function
'***

The string conversion is different depending on whether you're dealing with ASCII or Unicode data, both versions are
included in the code sample above.
Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
7 Mar 2006 2:56 AM
Richard Jalbert
Show quote Hide quote
On Sun, 5 Mar 2006 13:18:30 -0000, "Mike D Sutton" <ED***@mvps.org>
wrote:

>> What is wrong in the following code:
>>     Dim cds As COPYDATASTRUCT
>>     Dim buf(1 To 255) As Byte
>>
>>     Call CopyMemory(cds, ByVal lParam, Len(cds))
>>     Call CopyMemory(buf(1), ByVal cds.lpData, cds.cbData)
>>     lpString = StrConv(buf, vbUnicode)
>>
>> In the above code lpString is empty, despite the fact that there is data
>> sitting in buf() array.
>> What puzzles me is that I had that code working before.
>
>Try this:
>
>'***
>Private Declare Sub RtlMoveMemory Lib "Kernel32.dll" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As
>Long)
>
>Private Type CopyDataStruct
>    dwData As Long
>    cbData As Long
>    lpData As Long
>End Type
>
>Public Event DoCopyData(ByVal inWnd As Long, ByRef inData() As Byte, ByVal inDataLen As Long)
>
>...
>
>Private Function OnCopyData(ByVal inWParam As Long, ByVal inLParam As Long) As Long
>    Dim CDS As CopyDataStruct
>    Dim DataBuf() As Byte
>    Dim DataStr As String
>
>    If (inLParam) Then ' Copy CopyDataStruct locally
>        Call RtlMoveMemory(CDS, ByVal inLParam, Len(CDS))
>
>        ' Validate CDS data
>        If ((CDS.cbData > 0) And (CDS.lpData <> 0)) Then
>            ReDim DataBuf(0 To CDS.cbData - 1) As Byte
>            Call RtlMoveMemory(DataBuf(0), ByVal CDS.lpData, CDS.cbData)
>
>            ' If data is ASCII:
>            DataStr = StrConv(DataBuf, vbUnicode)
>
>            ' If data is Unicode:
>            ' DataStr = DataBuf
>
>            ' Raise event with data
>            RaiseEvent DoCopyData(inWParam, DataBuf, CDS.cbData)
>        Else ' Raise event with no data
>            RaiseEvent DoCopyData(inWParam, DataBuf, 0&)
>        End If
>
>        OnCopyData = 1&
>    End If
>End Function
>'***
>
>The string conversion is different depending on whether you're dealing with ASCII or Unicode data, both versions are
>included in the code sample above.
>Hope this helps,

If a Unicode string is loaded into a byte array, every second byte
will be zero.


**********************************************************************
Richm***@sympatico.ca

Dog thinks: they feed me, they take care of me: they are gods.
Cat thinks: they feed me, they take care of me: I am god.
http://www3.sympatico.ca/richmann/
http://www.geocities.com/richmannsoft/
**********************************************************************
Author
5 Mar 2006 1:18 PM
Juergen Thuemmler
>    Call CopyMemory(cds, ByVal lParam, Len(cds))
>    Call CopyMemory(buf(1), ByVal cds.lpData, cds.cbData)
>    lpString = StrConv(buf, vbUnicode)

Whe you use a byte array for getting the text, leave the StrConv() out:
lpString = buf()
Alternatively, you can use a string:
lpString = String$(cds.cbData, 0)
Call CopyMemory(ByVal lpString, ByVal cds.lpData, cds.cbData)
lpString = Trim$(StrConv(lpString, vbFromUnicode))

Juergen.