5 Replies - 1000 Views - Last Post: 05 January 2013 - 01:40 PM Rate Topic: -----

#1 RAFAAJ2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-November 11

Checking if a Pointer is valid .

Posted 29 December 2012 - 11:25 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Checking if a Pointer is valid .

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,792
  • Joined: 12-December 12

Re: Checking if a Pointer is valid .

Posted 29 December 2012 - 11:42 AM

Does lPtr return 0 (or Nothing) on failure? Otherwise, you may be able to make use of TypeName().

I found the following code if it is useful to you.

Attribute VB_Name = "PtrObj"

' PtrObj Function
'
' This function (PtrObj) is the reverse of the undocumented ObjPtr
' function.  PtrObj converts a long integer into an object reference.
' Copyright David Pinch; http://www.thoughtproject.com

Option Explicit

' ===================================================================
' Win32: RtlMoveMemory
' ===================================================================

Private Declare Sub RtlMoveMemory _
  Lib "kernel32" _
 (ByRef Target As Any, _
  ByRef Source As Any, _
  ByVal Length As Long)

' ===================================================================
' PtrObj
' ===================================================================

Public Function PtrObj(ByVal Pointer As Long) As Object

  Dim SoftRef As Object
  
  ' Exit immediately it the Pointer is Null.  The function
  ' will return the default value of Nothing.
  
  If Pointer = 0 Then
    Exit Function
  End If
  
  ' Get a soft reference to the object
  
  RtlMoveMemory SoftRef, Pointer, 4
  
  ' Get a legal reference to the object
  
  Set PtrObj = SoftRef
  
  ' Destroy the illegal reference
  
  RtlMoveMemory SoftRef, 0&, 4

FunctionExit:
  Exit Function
  
Functionerror:
  Set PtrObj = Nothing
  Resume FunctionExit

End Function


Was This Post Helpful? 0
  • +
  • -

#3 RAFAAJ2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-November 11

Re: Checking if a Pointer is valid .

Posted 29 December 2012 - 12:51 PM

Thanks for the answer but the Function you have provided doesn't really solve the problem.... The code below which uses you function causes an immediate crash of the application.

It seems that there is no way to trap the error so that it can be handled.... The error just causes the application to crash .

There 's got to be a solution somewhere :)


Sub Test()

    Dim lPtr As Long

    lPtr = 1111 ' Random Long number
    
    MsgBox TypeName(PtrObj(lPtr)) ' <---------Crash ici

End Sub


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,792
  • Joined: 12-December 12

Re: Checking if a Pointer is valid .

Posted 29 December 2012 - 01:49 PM

Yes, I'm not surprised it crashes :). Try just

lPtr = objPtr(YourObject)
If lPtr = 0 Then
    Msgbox "Object (of the correct type) failed!"
End If

Was This Post Helpful? 0
  • +
  • -

#5 RAFAAJ2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-November 11

Re: Checking if a Pointer is valid .

Posted 29 December 2012 - 02:15 PM

As I said, when passing an invalid pointer to the CopyMemory API crashes whithout giving a chance to handle the error.

I hope someone has an answer to this perplexing stuff.
Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Checking if a Pointer is valid .

Posted 05 January 2013 - 01:40 PM

CopyMemory does indeed assume that the Long value it's receiving is legit, so you have to trap the error before you call it. ObjPtr(myObjectReference) will return the actual Long value. I'm not quite sure why it's possible for you not to know whether or not you have a good object pointer independently of your function, but perhaps this will help. Anyway, here is more info on it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1