Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

CopyMemory crash app... Please Help!!
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



Report this thread to moderator Post Follow-up to this message
Old Post
Francesco Ranieri
09-30-04 08:55 PM


Re: CopyMemory crash app... Please Help!!
Francesco 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

Report this thread to moderator Post Follow-up to this message
Old Post
Sinna
09-30-04 08:55 PM


Re: CopyMemory crash app... Please Help!!
Thanks 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



Report this thread to moderator Post Follow-up to this message
Old Post
Francesco Ranieri
09-30-04 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Visual Basic archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:42 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.