Page 1 of 1

Problem with SetProp/GetProp

Posted: Mon Apr 15, 2019 1:40 pm
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.

Re: Problem with SetProp/GetProp

Posted: Mon Apr 15, 2019 1:47 pm
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.

Re: Problem with SetProp/GetProp

Posted: Mon Apr 15, 2019 2:56 pm
by Fred
Just allocate a memory chunk, SetProp() parameter is a pointer

Re: Problem with SetProp/GetProp

Posted: Mon Apr 15, 2019 4:41 pm
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$

Re: Problem with SetProp/GetProp

Posted: Tue Apr 16, 2019 12:43 pm
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. :(

Re: Problem with SetProp/GetProp

Posted: Wed Apr 17, 2019 5:26 am
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$)

Re: Problem with SetProp/GetProp

Posted: Wed Apr 17, 2019 9:54 am
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.

Re: Problem with SetProp/GetProp

Posted: Wed Apr 17, 2019 1:48 pm
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

Re: Problem with SetProp/GetProp

Posted: Wed Apr 17, 2019 9:57 pm
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. :)

Re: Problem with SetProp/GetProp

Posted: Wed Apr 17, 2019 11:11 pm
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$

Re: Problem with SetProp/GetProp

Posted: Thu Apr 18, 2019 8:41 am
by Dude
RASHAD wrote:Hi Dude
Check and report
That works fantastic! I knew you could do it. :D Thank you so much, Rashad.