It is currently Wed Sep 18, 2019 2:29 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Problem with SetProp/GetProp
PostPosted: Mon Apr 15, 2019 1:40 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1896
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:
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:
; First tab in IDE

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

Code:
; 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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Problem with SetProp/GetProp
PostPosted: Mon Apr 15, 2019 1:47 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1896
I just realised it doesn't work in the same source if t$ is cleared:

Code:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Problem with SetProp/GetProp
PostPosted: Mon Apr 15, 2019 2:56 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 13612
Location: France
Just allocate a memory chunk, SetProp() parameter is a pointer


Top
 Profile  
Reply with quote  
 Post subject: Re: Problem with SetProp/GetProp
PostPosted: Mon Apr 15, 2019 4:41 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Oct 09, 2007 2:15 am
Posts: 1082
Code:
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 5.70 LTS (Windows x86/x64) | Windows10 Pro x64 | Z370 Extreme4 | i7 8770k | 32GB RAM | iChill GeForce GTX 980 X4 Ultra | HAF XF Evo​​
English is not my native language... (I often use DeepL to translate my texts.)


Top
 Profile  
Reply with quote  
 Post subject: Re: Problem with SetProp/GetProp
PostPosted: Tue Apr 16, 2019 12:43 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1896
Code:
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. :(


Top
 Profile  
Reply with quote  
 Post subject: Re: Problem with SetProp/GetProp
PostPosted: Wed Apr 17, 2019 5:26 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3395
Hi
Code:
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Problem with SetProp/GetProp
PostPosted: Wed Apr 17, 2019 9:54 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1896
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Problem with SetProp/GetProp
PostPosted: Wed Apr 17, 2019 1:48 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1903
Location: Germany
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 / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Problem with SetProp/GetProp
PostPosted: Wed Apr 17, 2019 9:57 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1896
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. :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Problem with SetProp/GetProp
PostPosted: Wed Apr 17, 2019 11:11 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3395
Hi Dude
Check and report
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Problem with SetProp/GetProp
PostPosted: Thu Apr 18, 2019 8:41 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1896
RASHAD wrote:
Hi Dude
Check and report

That works fantastic! I knew you could do it. :D Thank you so much, Rashad.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye