Code Comments
Programming Forum and web based access to our favorite programming groups.Hi to all, I'm trying to write an application that communicates with another via UDP. The problem is that, sometime, the application that receives messages crashes! I've inspected the code writing onto disk the trace of execution, to mark the point where could be the instruction that causes the crash. It seems to me that it occurs with combination of CopyMemory execution! Here the code: Public Function GetCMSMessageID(strPRMMessage As String) As Long Dim lngMessageID As Long Dim bytBuffer(1 To MESSAGES_MAX_LENGTH) As Byte On Error Resume Next lngMessageID = 0 'Avoid the access to an empty string If Trim(strPRMMessage) <> "" Then 'Avoid the access to string that can't have enough length If LenB(strPRMMessage) >= 8 Then 'Trace the point before CopyMemory execution Open "E:\log.txt" For Append As #100: Print #100, "* GetCMSMessageID=>": Close #100 'Copy the received message into the byte array Call CopyMemory(bytBuffer(1), _ ByVal strPRMMessage, _ MESSAGES_MAX_LENGTH) 'Read the first four bytes that contain the message ID Call CopyMemoryRead(lngMessageID, _ VarPtr(bytBuffer(1)), _ 4) 'Trace the point after CopyMemory execution Open "E:\log.txt" For Append As #100: Print #100, "* <=GetCMSMessageID": Close #100 End If End If 'Return the message ID GetCMSMessageID = lngMessageID End Function where: Public Declare Sub CopyMemory Lib "kernel32" _ Alias "RtlMoveMemory" (anyPRMDestination As Any, _ anyPRMSource As Any, _ ByVal lngPRMLength As Long) Public Declare Sub CopyMemoryRead _ Lib "kernel32" _ Alias "RtlMoveMemory" (anyPRMDestinationVariable As Any, _ ByVal lngPRMSourceAddress As Long, _ ByVal lngPRMLength As Long) The error is (translated from italian to englis.....) "The memory couldn't be read!!!" The function that calls the GetCMSMessageID looks like: ............... Call UDPMessagesReceiver(strMessageReceived) lngMessageIdentificator = GetCMSMessageID(strMessageReceived) ............... Any idea about what's appening and how could I avoid the application crash? Thanks in advance Francesco
Post Follow-up to this messageFrancesco Ranieri wrote: > Hi to all, > I'm trying to write an application that communicates with another via UDP. > The problem is that, sometime, the application that receives messages > crashes! > I've inspected the code writing onto disk the trace of execution, to mark > the point where could be the instruction that causes the crash. > It seems to me that it occurs with combination of CopyMemory execution! He re > the code: > > Public Function GetCMSMessageID(strPRMMessage As String) As Long > Dim lngMessageID As Long > Dim bytBuffer(1 To MESSAGES_MAX_LENGTH) As Byte > > On Error Resume Next > > lngMessageID = 0 > 'Avoid the access to an empty string > If Trim(strPRMMessage) <> "" Then > 'Avoid the access to string that can't have enough length > If LenB(strPRMMessage) >= 8 Then > 'Trace the point before CopyMemory execution > Open "E:\log.txt" For Append As #100: Print #100, "* GetCMSMessageID=>": > Close #100 > 'Copy the received message into the byte array > Call CopyMemory(bytBuffer(1), _ > ByVal strPRMMessage, _ > MESSAGES_MAX_LENGTH) > 'Read the first four bytes that contain the message ID > Call CopyMemoryRead(lngMessageID, _ > VarPtr(bytBuffer(1)), _ > 4) > 'Trace the point after CopyMemory execution > Open "E:\log.txt" For Append As #100: Print #100, "* <=GetCMSMessageID": > Close #100 > End If > End If > 'Return the message ID > GetCMSMessageID = lngMessageID > End Function > > where: > > Public Declare Sub CopyMemory > Lib "kernel32" _ > Alias "RtlMoveMemory" (anyPRMDestination As Any, _ > anyPRMSourc e > As Any, _ > ByVal > lngPRMLength As Long) > Public Declare Sub CopyMemoryRead _ > Lib "kernel32" _ > Alias "RtlMoveMemory" (anyPRMDestinationVariable As > Any, _ > ByVal > lngPRMSourceAddress As Long, _ > ByVal > lngPRMLength As Long) > > The error is (translated from italian to englis.....) > > "The memory couldn't be read!!!" > > The function that calls the GetCMSMessageID looks like: > > ............... > Call UDPMessagesReceiver(strMessageReceived) > lngMessageIdentificator = GetCMSMessageID(strMessageReceived) > ............... > > Any idea about what's appening and how could I avoid the application crash ? > Thanks in advance > Francesco > > Hi Francesco, A first look on your code shows me that you're trying to copy too much using the CopyMemory API. You always copy MESSAGES_MAX_LENGTH bytes, while you don't check if the string strPRMMessage is long enough! So, you'd better write: Dim bytBuffer() As Byte Dim intPRMMessageLength As Integer intPRMMessasgeLength = LenB(strPRMMessage) If intPRMMessageLength > 1 Then Redim bytBuffer(1 to intPRMMessageLength) As Byte CopyMemory bytBuffer(1), ByVal strPRMMessage, intPRMMessageLength) End If Note that you don't even need the CopyMemory instruction here! You can write it this way: Dim bytBuffer() As Byte bytBuffer = StrConv(strPRMMessage, vbFromUnicode) Pay attention that in this case bytBuffer is 0-based instead of 1-based. Why should you even copy the whole string into a ByteBuffer to extract only the first four bytes? So: CopyMemory lngMessageID, Byval strPRMMessage, 4 should do the trick (assumed your string is at least 4 bytes long). Sinna
Post Follow-up to this messageThanks for your reply! With your suggestion, now, my question seems to be very stupid!! Thanks again... "Sinna" <news4sinna_NOSPAM@hotpop.com> ha scritto nel messaggio news:OY75z4upEHA.592@TK2MSFTNGP11.phx.gbl... > Francesco Ranieri wrote: UDP. mark Here _ anyPRMSource As crash? > Hi Francesco, > > A first look on your code shows me that you're trying to copy too much > using the CopyMemory API. > You always copy MESSAGES_MAX_LENGTH bytes, while you don't check if the > string strPRMMessage is long enough! > So, you'd better write: > > Dim bytBuffer() As Byte > Dim intPRMMessageLength As Integer > > intPRMMessasgeLength = LenB(strPRMMessage) > If intPRMMessageLength > 1 Then > Redim bytBuffer(1 to intPRMMessageLength) As Byte > CopyMemory bytBuffer(1), ByVal strPRMMessage, intPRMMessageLength) > End If > > Note that you don't even need the CopyMemory instruction here! > You can write it this way: > > Dim bytBuffer() As Byte > bytBuffer = StrConv(strPRMMessage, vbFromUnicode) > > Pay attention that in this case bytBuffer is 0-based instead of 1-based. > > Why should you even copy the whole string into a ByteBuffer to extract > only the first four bytes? > So: > > CopyMemory lngMessageID, Byval strPRMMessage, 4 > > should do the trick (assumed your string is at least 4 bytes long). > > > Sinna
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.