Jump to content

How to check validity of pointer to DllStruct


Recommended Posts

Hello,

Is there any way to check for validity of a pointer to a DllStruct ?

In the above code, a DllStruct is created from an invalid pointer. How to prevent from doing this ?

local $tStruct = DllStructCreate("int  i")
DllStructSetData($tStruct, "i", 123)
local $pStruct = DllStructGetPtr($tStruct)
$tStruct = 0                                    ; destroy DllStruct ==> $pStruct becomes invalid
; how to check here for validity of $pStruct and prevent from doing the following ?
$tStruct = DllStructCreate("int  i", $pStruct)  ; create DllStruct from invalid pointer...
MsgBox(0, @ScriptName, @error)                  ; displays '0' : invalid pointer is not detected by DllStructCreate()
MsgBox(0, @ScriptName, IsDllStruct($tStruct))   ; displays '1' : tStruct is assumed as a valid DllStruct object, which is not

Thanks for help

Edited by SharpDressedMan
Link to comment
Share on other sites

Check the help file and you will see that the function returns useful information in case of an error. 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

53 minutes ago, water said:

Check the help file and you will see that the function returns useful information in case of an error. 

Thanks for answer,

Unfortunately, there is no error returned. The last call to DllStructCreate (with invalid pointer) does succeed despite of the invalid pointer passed.

I updated the code snippet above to state this.

Link to comment
Share on other sites

3 hours ago, Earthshine said:

the pointer is an address in memory so you tell me how to validate that address. 

A **raw** pointer is an address in memory.

I don't know what kind of pointer DllStructGetPtr returns. If it's raw, then you're right: no way to check for validity,

Howver, AutoIt seams to manage memory using some kinds of shared pointers, so one could expect that DllStructGetPtr returns a weak pointer (which allows to check for validity).

 

 

Edited by SharpDressedMan
Link to comment
Share on other sites

look, you are going to have to look at the object it points to and perhaps write validation code using what you know about the object-- if it's to be done.

so have a look at this

https://msdn.microsoft.com/en-us/library/hh279674.aspx

yeah, once the resource is allocated, the smart pointer owns the raw, so probably same thing. other than testing for not being null, what else could you do but interrogate the object it's pointing to.... 

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

Pointers are dangerous they allow you to shoot your feet..

some things are mentioned here: 

The only way I can think to stop the situation is something like this..

local $tStruct = DllStructCreate("int sentinel;int  i")
DllStructSetData($tStruct, "sentinel", 0xBADE1F)
DllStructSetData($tStruct, "i", 123)
local $pStruct = DllStructGetPtr($tStruct)
$tStruct = 0                                    ; destroy DllStruct ==> $pStruct becomes invalid
; how to check here for validity of $pStruct and prevent from doing the following ?
local $tStruct2 = DllStructCreate("int sentinel;int  i", $pStruct)  ; create DllStruct from invalid pointer...
MsgBox(0, @ScriptName, @error)                  ; displays '0' : invalid pointer is not detected by DllStructCreate()
MsgBox(0, @ScriptName, IsDllStruct($tStruct2) And (DllStructGetData($tStruct2, "sentinel") = 0xBADE1F))

 

Edited by Bilgus
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...