Problem with SetProp/GetProp

Windows specific forum
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Problem with SetProp/GetProp

Post by Dude »

Does SetProp_() and GetProp_() have to be from the same thread or something?

Because when start Notepad and run this standalone snippet, it works correctly:

Code: Select all

t$="hello"

hWnd=FindWindow_(0,"Untitled - Notepad")
Debug SetProp_(hWnd,"zzz",t$) ; 1

a=GetProp_(hWnd,"zzz")
Debug a ; 37094864
If a
  Debug PeekS(a) ; hello
EndIf
But if I split the code into two separate tabs in the PureBasic IDE and run each separately, GetProp_() fails:

Code: Select all

; First tab in IDE

t$="hello"
hWnd=FindWindow_(0,"Untitled - Notepad")
Debug SetProp_(hWnd,"zzz",t$) ; 1

Code: Select all

; Second tab in IDE

hWnd=FindWindow_(0,"Untitled - Notepad")
a=GetProp_(hWnd,"zzz")
Debug a ; 0
If a
  Debug PeekS(a)
EndIf
Why does GetProp_() fail to return the text "hello" unless it's run from the same source code, as demonstrated in my first snippet? Thanks.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Problem with SetProp/GetProp

Post by Dude »

I just realised it doesn't work in the same source if t$ is cleared:

Code: Select all

t$="hello"

hWnd=FindWindow_(0,"Untitled - Notepad")
Debug SetProp_(hWnd,"zzz",@t$) ; 1

t$=""

a=GetProp_(hWnd,"zzz")
Debug a ; 37094864
If a
  Debug PeekS(a) ; ""
EndIf
:( So how can I store a string as data for a window? I want the data to be retained in the window if I quit and restart my app. MSDN seems to indicate that props are used for this purpose, and I'd prefer not to store the data in a file if possible.
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Problem with SetProp/GetProp

Post by Fred »

Just allocate a memory chunk, SetProp() parameter is a pointer
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Problem with SetProp/GetProp

Post by Bisonte »

Code: Select all

t$="hello"
Title$ = "Unbenannt - Editor" ; german ;)
Title$ = "Untitled - Notepad" ; Yours

hWnd=FindWindow_(0, Title$)
If hWnd
  *SetString = UTF8(t$)
  Debug SetProp_(hWnd,"zzz", *SetString) ; 1
EndIf

t$=""

*GetString = GetProp_(hWnd,"zzz")
If *GetString
  t$ = PeekS(*GetString, -1, #PB_UTF8|#PB_ByteLength)
  FreeMemory(*GetString)
EndIf

Debug t$
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Problem with SetProp/GetProp

Post by Dude »

Code: Select all

t$="hello"
Title$ = "Unbenannt - Editor" ; german ;)
Title$ = "Untitled - Notepad" ; Yours

hWnd=FindWindow_(0, Title$)
If hWnd
  *SetString = UTF8(t$)
  Debug SetProp_(hWnd,"zzz", *SetString) ; 1
EndIf

t$=""

*GetString = GetProp_(hWnd,"zzz")
If *GetString
  t$ = PeekS(*GetString, -1, #PB_UTF8|#PB_ByteLength)
  FreeMemory(*GetString)
EndIf

Debug t$
Bisonte, your example doesn't work from separate sources, like mine - it crashes with an invalid memory access error for PeekS() when trying to read the prop. :( So if my app quits and restarts, it won't get the prop again.

@Fred: But if I allocate memory, how will my app know about it when it restarts?

Basically, the situation is this: I want to (for example) set a property to a window that it's been "seen" by my app. In other words, my app has interacted with it at least once. Now, if my app quits and restarts and that window still exists, the new launch of my app has to be able to know that it's "seen" it before. Or, if my app quits and that window has closed, and a new window the same opens (think Calculator), then when my app restarts it has to know that it hasn't "seen" it before, because Calculator is a new window instance.

I could probably save the handles of all seen windows in a file, but this has the problem of potential clashes if a handle of a closed window matches the handle of newly-opened window; plus my app might be running from a non-writable location or slow media; or even if the user re-installs my app then the window data file will be lost.

So, props for windows seem to be the best way, since the props are attached to a specific runtime window instance and are self-deleted when the window closes. But I just can't make this work. :(
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Problem with SetProp/GetProp

Post by RASHAD »

Hi

Code: Select all

flag.l = 1092387645 ;Use your own flag
;Or if you can change your Text to constant no. and back
RunProgram("notepad.exe")
  
;Title$ = "Unbenannt - Editor" ; german ;)
Title$ = "Untitled - Notepad" ; Yours
Repeat
  hWnd=FindWindow_(0, Title$)
Until hWnd

If hWnd
  Debug SetProp_(hWnd,"zzz", flag) ; 1
EndIf

;****************************************

;Title$ = "Unbenannt - Editor" ; german ;)
Title$ = "Untitled - Notepad" ; Yours
Repeat
  hWnd=FindWindow_(0, Title$)
Until hWnd

If hWnd  
  Getprop = GetProp_(hWnd,"zzz")
EndIf

If Getprop = 1092387645
  Debug "hello Dude"
EndIf
# 2:
Accepts only 2 characters

Code: Select all

t$ = "Hi"
flag.i = PeekI(@t$)

RunProgram("notepad.exe")
 
;Title$ = "Unbenannt - Editor" ; german ;)
Title$ = "Untitled - Notepad" ; Yours
Repeat
  hWnd=FindWindow_(0, Title$)
Until hWnd

If hWnd
  SetProp_(hWnd,"zzz", flag) ; 1
EndIf

;****************************************

;Title$ = "Unbenannt - Editor" ; german ;)
Title$ = "Untitled - Notepad" ; Yours
Repeat
  hWnd=FindWindow_(0, Title$)
Until hWnd

If hWnd 
  Getprop = GetProp_(hWnd,"zzz")
EndIf

t$ = Space(8)
PokeI(@t$,Getprop)
Debug PeekS(@t$)
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Problem with SetProp/GetProp

Post by Dude »

RASHAD wrote:# 2:
Accepts only 2 characters
Thanks... almost there, but is there a way to get at least 255 characters? I need to store long text in the prop, too.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Problem with SetProp/GetProp

Post by mk-soft »

None of this is gonna work.
Each program has its own storage area. When you quit the program, it will be released again.
So you can't save a pointer on a string and read it again when restarting the program.

One possibility is to work with FileMapping.
You start a help program and save the string there.

Link to the SharedMemory
https://www.purebasic.fr/german/viewtop ... =8&t=16659
https://www.purebasic.fr/german/viewtop ... 10#p331758
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Problem with SetProp/GetProp

Post by Dude »

mk-soft wrote:Each program has its own storage area. When you quit the program, it will be released again.
I know that. But window props are supposed to be the official Microsoft solution for that, so that when my app restarts, it can get the window data again, without having to resort to files. Rashad's solution can do it with 2 characters only, so it works, but I need a little bit longer than that. :)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Problem with SetProp/GetProp

Post by RASHAD »

Hi Dude
Check and report

Code: Select all

t$ = "How are you Dude ? Are you OK"

flag = GlobalAddAtom_(@t$)

RunProgram("notepad.exe")
 
;Title$ = "Unbenannt - Editor" ; german ;)
Title$ = "Untitled - Notepad" ; Yours
Repeat
  hWnd=FindWindow_(0, Title$)
Until hWnd

If hWnd
  SetProp_(hWnd,"zzz", flag) ; 1
EndIf

; ;********************************************

;Title$ = "Unbenannt - Editor" ; german ;)
Title$ = "Untitled - Notepad" ; Yours
Repeat
  hWnd=FindWindow_(0, Title$)
Until hWnd

If hWnd 
  Getprop = GetProp_(hWnd,"zzz")
EndIf
text$ = Space(#MAX_PATH)
GlobalGetAtomName_(Getprop,@text$,#MAX_PATH)
Debug text$
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Problem with SetProp/GetProp

Post by Dude »

RASHAD wrote:Hi Dude
Check and report
That works fantastic! I knew you could do it. :D Thank you so much, Rashad.
Post Reply