Checking if a Pointer is valid .

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,779
Office Version
  1. 2016
Platform
  1. Windows
Hi all .

Strictly speaking, this is not necessarly an excel related question but I thought I would post it here anyway as this forum has many bright members .... (I have already posted this question on a few VB forums but found no solution.)

I need to know the contents of a variable before passing it to the CopyMemory API in order to prevent the application from crashing.

* - To illustrate the problem, here is the first example that works because the lPtr variable correctly points to the Application Object.

Code:
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 the 2nd 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 and crashes the application.
Code:
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/thoughts much appreciated.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top