check if destination memory is readable/writeable

Share your advanced PureBasic knowledge/code with the community.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

check if destination memory is readable/writeable

Post by newbie »

Code updated For 5.20+


Hi,

recently I was blocked on my code because for some weird reason all of a sudden the destination memory (which was part of a structure) was not readable and the program was crashing.
Just the fact to try to manually check if there was something at this memory was making the program to crash.

By digging throught MSDN I found a set of API very usefull :o

Code: Select all

; before a CopyMemory, want to check that destination memory is writeable ?
if IsBadWritePtr_(*dst_addr, size) = 0
    debug "memory writeable, let's go"
    CopyMemory(*src_addr, *dst_addr, size)
else
    debug "memory not writeable, give up"
Endif

Code: Select all

; before reading memory, want to check that it is readable ?
if IsBadCodePtr_(*dst_addr) = 0
    debug "memory readable, let's go"
    var$ = PeekS(*dst_addr)
else
    debug "memory not readable, give up"
Endif

; or if you need to check a defined size, use this instead :
if IsBadReadPtr_(*dst_addr, size) = 0
    debug "memory writeable, let's go"
    var$ = PeekS(*dst_addr, size)
else
    debug "memory not readable, give up"
Endif
I hope it can help ;)
- Registered PB user -

Using PB 4.00