|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Problem converting bytes array into stringHello,
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
Show quote
Hide quote
"Jack" <replyTo@newsgroup> wrote in message Not sure but Len should be LenB in there but that's prolly not the 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. difference. Show quoteHide quote > Jack > > > What is wrong in the following code: Try this:> 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. '*** 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/
Show quote
Hide quote
On Sun, 5 Mar 2006 13:18:30 -0000, "Mike D Sutton" <ED***@mvps.org> If a Unicode string is loaded into a byte array, every second bytewrote: >> 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, 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/ ********************************************************************** > Call CopyMemory(cds, ByVal lParam, Len(cds)) Whe you use a byte array for getting the text, leave the StrConv() out:> Call CopyMemory(buf(1), ByVal cds.lpData, cds.cbData) > lpString = StrConv(buf, vbUnicode) 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. |
|||||||||||||||||||||||