How can I ensure a unique buffer pointer for each variable? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
pedro45_vs
Posts: 39
Joined: 28 Jun 2020, 18:46

How can I ensure a unique buffer pointer for each variable?

Post by pedro45_vs » 18 Dec 2023, 06:47

I need to create 5 ByRef COM output variables to capture the result of the GetRect function.

However, the results are not matching as expected and the reason is that SOMETIMES the buffer is being created with the same pointer.

How can I ensure a unique buffer pointer for each variable?

Code: Select all

pLeft   := ComValue(VT_BYREF := 0x4000 | VT_I4 := 3, A := Buffer(4).ptr)
pTop    := ComValue(VT_BYREF := 0x4000 | VT_I4 := 3, B := Buffer(4).ptr)
pRight  := ComValue(VT_BYREF := 0x4000 | VT_I4 := 3, C := Buffer(4).ptr)
pBottom := ComValue(VT_BYREF := 0x4000 | VT_I4 := 3, D := Buffer(4).ptr)
pHit    := ComValue(VT_BYREF := 0x4000 | VT_I4 := 3, E := Buffer(4).ptr)


MsgBox(A '`n' B '`n' C '`n' D '`n' E)

; The function I need is GetRect of type ITextDocument.Range
; https://learn.microsoft.com/en-us/windows/win32/api/tom/nf-tom-itextrange2-getrect
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How can I ensure a unique buffer pointer for each variable?  Topic is solved

Post by teadrinker » 18 Dec 2023, 08:48

In the example you gave, the buffers themselves are not saved to variables, you only save their pointers. So it is theoretically possible that a new buffer will be created with the same pointer as any of the previous ones, although I have not been able to reproduce this.
You need to save buffers themselves, not pointers to buffers, into variables:

Code: Select all

A := Buffer(4)
pLeft := ComValue(VT_BYREF := 0x4000 | VT_I4 := 3, A.ptr)
pedro45_vs
Posts: 39
Joined: 28 Jun 2020, 18:46

Re: How can I ensure a unique buffer pointer for each variable?

Post by pedro45_vs » 18 Dec 2023, 10:11

Thank you so very much @teadrinker. Problem solved!
Post Reply

Return to “Ask for Help (v2)”