Page 1 of 2
Pass values (longs) between two PB programs.
Posted: Fri Sep 09, 2005 4:49 pm
by TerryHough
I'm sure I have seen a post explaining how to do this, but I cannot find it
today.
Program A optionally launches Program B
I really need the Program B to be able to access the contents of a
gadget in Program A (the calling program), manipulate it, and then update
the original gadget's contents in the active Program A.
Anybody got a pointer for me?
Posted: Fri Sep 09, 2005 4:53 pm
by Killswitch
I think there's an example of inter-program comminication on Freak's website.
Posted: Fri Sep 09, 2005 6:20 pm
by GedB
Accessing another windows controls is very easy, since controls are publiclly exposed and accessible using SendMessage.
GadgetID() returns the handle needed to identify the gadget.
Compile the following example a A.exe and B.exe.
Code: Select all
;A.pb
If OpenWindow(0, 0, 0, 100, 50, #PB_Window_WindowCentered|#PB_Window_SystemMenu, "Program A")
If CreateGadgetList(WindowID(0))
StringGadget(0, 0, 0, 100, 20, "")
ButtonGadget(1, 0, 30, 100, 20, "Launch B")
EndIf
EndIf
Repeat
e = WaitWindowEvent()
Select e
Case #PB_Event_Gadget
Select EventGadgetID()
Case 1
RunProgram("B.exe", Str(GadgetID(0)), ".")
EndSelect
EndSelect
Until e = #PB_Event_CloseWindow
Code: Select all
;B.pb
Declare.s GetTextFromA()
Awnd = Val(ProgramParameter())
If OpenWindow(0, 0, 0, 100, 50, #PB_Window_WindowCentered|#PB_Window_SystemMenu, "Program B")
If CreateGadgetList(WindowID(0))
TextGadget(0, 0, 0, 100, 20, "")
ButtonGadget(1, 0, 30, 100, 20, "Get Text from A")
EndIf
EndIf
GetTextFromA()
Repeat
e = WaitWindowEvent()
Select e
Case #PB_Event_Gadget
Select EventGadgetID()
Case 1
GetTextFromA()
EndSelect
EndSelect
Until e = #PB_Event_CloseWindow
Procedure.s GetTextFromA()
Shared Awnd
buffer.s = Space(255)
SendMessage_(Awnd, #WM_GETTEXT, 255, @buffer)
SetGadgetText(0, PeekS(@buffer))
EndProcedure
Posted: Fri Sep 09, 2005 7:08 pm
by Droopy
Very cool

Posted: Fri Sep 09, 2005 9:42 pm
by TerryHough
GedB wrote:Accessing another windows controls is very easy, since controls are publiclly exposed and accessible using SendMessage.
Thanks for your code. It works well. Unfortunately, I haven't been able
to get it to work with my programs. I will keep trying.
Terry
Posted: Sat Sep 10, 2005 11:14 am
by GedB
Terry,
Post some code, see if we can help.
Posted: Sat Sep 10, 2005 5:00 pm
by Tranquil
I use normaly the following methode:
I create an invisible window with an unique ID and waiting for messages in his callback.
Another application uses FindWindow_() to get the handle of the recipient window. Then I only use SendMessage_ API to send a user defined Message (#WM_User+x) plus an optional lparam and/ or wparam.
cheers
Mike
Posted: Sat Sep 10, 2005 8:24 pm
by GreenGiant
I'd also reccomend using user-defined messages. But if you are going to use them, don't forget to use RegisterWindowMessage_() to avoid trouble.
Posted: Sun Sep 11, 2005 1:34 pm
by lexvictory
GreenGiant wrote:I'd also reccomend using user-defined messages. But if you are going to use them, don't forget to use RegisterWindowMessage_() to avoid trouble.
i think u only need to use that wen u use #hwnd_broadcast .....
if u know the exact hwnd then u can use #wm_user etc
Posted: Sun Sep 11, 2005 3:43 pm
by GreenGiant
I'm not an expert, but I understood that RegisterWindowMessage should always be used.
Say for example that you are sending a pointer to a string between two programs using #WM_User+17 (directly, not broadcasting it). Suppose another program is broadcasting a pointer to a function and is also using WM_User+17. Then because you didn't use RegisterWindowMessage to get an unique message your program will sometimes be sent the function pointer which could lead to problems if you try to write a string to it.
Posted: Mon Sep 12, 2005 10:17 am
by lexvictory
well, anyone using a #wm_user value in a broadcast message is pretty silly

doing so can cause all sorts of problems - like in ure example greengiant, u can still use registerwindowmessage to send a message to a hwnd that u know is ures, but if u know the hwnd u can use #wm_user+wateva ....
btw greengiant im not an expert either!!
also is this really needed?
cant it be:
?
Posted: Mon Sep 12, 2005 11:21 am
by GedB
I think Terry should walk before he can run.
Once he has the hand of sending standard messages, he can start registering his own.
One the registering of messages the
SDK has the following to say:
Only use RegisterWindowMessage when more than one application must process the same message. For sending private messages within a window class, an application can use any integer in the range WM_USER through 0x7FFF. (Messages in this range are private to a window class, not to an application. For example, predefined control classes such as BUTTON, EDIT, LISTBOX, and COMBOBOX may use values in this range.)
Posted: Mon Sep 12, 2005 11:25 am
by lexvictory
thats wat i meant.... (the wm_user bit)

Posted: Mon Sep 12, 2005 11:28 am
by lexvictory
also, ive found that in purebasic if u use a window callback u can use either sendmessage_() or postmessage_()
but if u dont use a callback it seems as tho u have to use postmessage_() .....
correct me if im wrong please

Posted: Mon Sep 12, 2005 8:25 pm
by TerryHough
GedB wrote:I think Terry should walk before he can run.

I think Terry needs to learn to type or needs glasses!
Thanks, GedB, for the pointer. Works like a charm for me now.
Terry