Jaafar Tribak
Well-known Member
- Joined
- Dec 5, 2002
- Messages
- 9,779
- Office Version
- 2016
- Platform
- 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.
* - 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.
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.
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.