Jump to content
SharpDressedMan

How to check validity of pointer to DllStruct

Recommended Posts

SharpDressedMan

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

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
SharpDressedMan
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.

Share this post


Link to post
Share on other sites
Earthshine

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

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
SharpDressedMan
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

Share this post


Link to post
Share on other sites
Earthshine

What is a weak pointer?


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
SharpDressedMan
1 hour ago, Earthshine said:

What is a weak pointer?

it is an object that includes a reference to a resource managed by a shared pointer.

See C++11 implementation for example: https://en.cppreference.com/w/cpp/memory/weak_ptr

Share this post


Link to post
Share on other sites
Earthshine

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

 

Share this post


Link to post
Share on other sites
Bilgus

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

Share this post


Link to post
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

  • Similar Content

    • gahhon
      By gahhon
      Hi,
      I have a program that is read data from the .txt file, since the .txt file is only readable, viewable and editable for the admin user.
      But how could I lock it with password, or some other technique that can helps to achieve this?
      Thanks for the advanced information.
    • gahhon
      By gahhon
      Hi,
      How can I the trigger the another button functions without waiting the previous function to finish execute?
      Any advise?
      I couldn't find any relevant topics via google.
      Thanks a lot.
    • SharpDressedMan
      By SharpDressedMan
      Hi all,
      I need to use ControlMove() on some controls of a hidden GUI window.
      This works properly on a GUI window created with default style, but does not work on a GUI window created with style $WS_OVERLAPPEDWINDOW
      func GUItest($bOverlapped) $m = GUICreate("test", 200, 100, -1, -1, $bOverlapped ? $WS_OVERLAPPEDWINDOW : -1) $g = GUICtrlCreateButton("test", 0, 0) ControlMove($m, "", $g, 30, 30) GUISetState() endfunc GuiTest(false) ; button 'test' properly moved to (30,30) GuiTest(true) ; button 'test' not moved and still sitting at (0,0) Any reason for this unexpected behavior ?
      Any workaround to fix this ?
      Thanks for any help
    • 2Toes
      By 2Toes
      Hi all..
      I'm having slight issue that has be a bit puzzled.
      I have an Input control.. After a certain function runs, it Disables that Input control, and sets it to ReadOnly.
      When running another certain function, it re-Enables that Input control, and sets the style back to Default.
      Which does work, however, when typing into the Input control after re-Enabling it, the text appears as a light gray color, rather than its original black color.
      I'm sure there's a simple solution here.. But what that is, appears to be beyond me.
      Here is a simplified example code:
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= Global $Form1 = GUICreate("Form1", 245, 215, 192, 124) Global $disableBTN = GUICtrlCreateButton("Disable", 32, 64, 75, 25) Global $enableBTN = GUICtrlCreateButton("Enable", 128, 64, 75, 25) Global $Input1 = GUICtrlCreateInput("", 32, 120, 169, 21) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $disableBTN disable() Case $enableBTN enable() EndSwitch WEnd Func disable() GUICtrlSetState($Input1, $GUI_DISABLE) GUICtrlSetStyle($Input1, $ES_READONLY) EndFunc Func enable() GUICtrlSetState($Input1, $GUI_ENABLE) GUICtrlSetStyle($Input1, $GUI_SS_DEFAULT_INPUT) EndFunc  
      Any help would be greatly appreciated
      Thank you in advance!
    • jantograaf
      By jantograaf
      Hi all,
      I'm trying to create a script that runs a JSON-query and then can retrieve some variables out of the returned, decoded object using JSON.au3. I have tried some other examples on this forum, but I'm stuck at one point. My query works perfectly and gets loaded into the variable $data. Then, decoding $data to $object seems to work as well. But then I can't get the date-field out of this JSON-structure with my script.
      The JSON-structure returned looks like this:
      { "content": [ { "id": "451ec583-8f27-4926-82a3-a2d85e57a110", "createdDate": "2018-08-08T08:40:57.449004Z", "updatedDate": "2018-08-08T08:40:57.449004Z", "lastOpenedDate": "2018-08-08T08:40:57.449004Z", "date": "2018-04-26T00:00:00", "description": "X-Ray Exam", "patient": { "id": "f857238a-c75d-4760-b8d1-8f50f8f9bbfa", "createdDate": "2018-08-08T08:40:37.623976Z", "updatedDate": "2018-08-08T08:40:37.623976Z", "lastOpenedDate": "2018-08-08T08:40:37.623976Z", "name": "Fuerstonia", "birthDate": "2014-06-08", "breed": "", "chip": "", "color": "", "damsire": "", "sire": "", "neutered": false, "orthancUuid": "", "sex": "U", "species": "Paard", "ueln": "De 431310762114", "pmsReference": "", "origin": "" }, "type": "study", "accessionNumber": "KME201806960467", "instanceUid": null, "orthancUuid": "", "sent": false, "seriesCount": 0, "modalityType": "RX", "typeAndModality": "study RX", "client": { "id": "be627195-8458-4927-8446-f1ef37b917a4", "createdDate": "2018-08-08T08:40:31.433968Z", "updatedDate": "2018-08-08T08:40:31.433968Z", "lastOpenedDate": "2018-08-08T09:26:49.512298Z", "via": "", "extraInfo": "", "pmsReference": "", "contact": { "id": "406cc555-c491-4c29-b6bb-8d903f0e35a9", "createdDate": "2018-08-08T08:40:31.428968Z", "updatedDate": "2018-08-08T08:40:31.428968Z", "lastName": "Client 1", "firstName": "", "company": "", "email": "", "language": "nl", "phone": "", "address": { "id": "6fc7703c-137a-4e0a-ba96-8c7f38f2044b", "city": "", "country": "", "line": "", "postalCode": "" } } } }, My script looks like this:
      #RequireAdmin #include <json.au3> #include <inet.au3> #include <File.au3> ;Create a handle to a logfile (will be created if it doesnt exist) Global $logfile = FileOpen("C:\VSOL\VSTK\Logs\MigrationFix\MigrationFix.log",9) FileWriteLine($logfile,"Start script") ;Create the URL with the JSON-query $URL = "http://localhost:8080/v0/studies/?seriesCount=1" ;Catch the query output into a variable $data = _INetGetSource($URL) ;Check if there is any data at all in this variable, if not, exit If Not $data Then MsgBox(1,"Error","No answer. Server is probably not running.") Exit EndIf ;For debugging purposes FileWriteLine($logfile,$data) ;Decode the JSON_string into a useable object $object = Json_Decode($data,1000) If @error Then FileWriteLine($logfile,"Error decoding JSON") Exit EndIf Local $i = 0 ;Start a loop to retrieve the study date of each study... While 1 $study_date = json_get($object,'[' & $i & '].date') If @error Then FileWriteLine($logfile,"Study-Date retrieval error") ExitLoop EndIf $i = $i + 1 WEnd ;Close the logfile FileWriteLine($logfile,"Stop script") FileClose($logfile) ;Open the logfile for quick reference ShellExecute("C:\VSOL\VSTK\Logs\MigrationFix\MigrationFix.log") If believe it has something to do with my json_get($object...)-command. Anyone who can point me in the right direction?
      Thanks in advance!
      Kind regards
×