Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Checking if a Pointer is valid .

$
0
0
Hi all and happy XMass.

I have been searching all over for a way to know if a Long variable has a valid Object Pointer in it.... I need to knwo the contents of the variable before passing it to the CopyMemory API in order to prevent the application from crashing.

* - Here is an example that works because the lPtr variable correctly points to the Application Object. ( Test done in Excel )

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
pDest As Any, pSrc As Any, ByVal ByteLen As Long)

Sub Test()

    Dim lPtr As Long
    Dim oTempObject As Object

    lPtr = objPtr(Application)
    CopyMemory oTempObject, lPtr, 4
        MsgBox oTempObject.Name
    CopyMemory oTempObject, 0&, 4

End Sub


* - Here is an example that shows how passing a invalid Pointer in the lPtr variable ( in this case I have randomly chosen the number 1111 ) to the CoyMemory API causes a GPF :

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
pDest As Any, pSrc As Any, ByVal ByteLen As Long)

Sub Test()

    Dim lPtr As Long
    Dim oTempObject As Object

    lPtr = 1111 '<------ invalid Pointer.
    CopyMemory oTempObject, lPtr, 4 ' <--------- CRASH HERE !!!!!
        MsgBox oTempObject.Name
    CopyMemory oTempObject, 0&, 4

End Sub


In order to avoid this problem , I have tried using the following APIs : IsBadReadPtr ;IsBadWritePtr and IsBadCodePtr but without any luck so far.

Any help much appreciated.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>