Pass values (longs) between two PB programs.

Just starting out? Need help? Post your questions and find answers here.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Pass values (longs) between two PB programs.

Post 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?
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

I think there's an example of inter-program comminication on Freak's website.
~I see one problem with your reasoning: the fact is thats not a chicken~
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Very cool :D
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post 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
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Terry,

Post some code, see if we can help.
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post 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
Tranquil
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post 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.
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Post 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 ..... :lol:
if u know the exact hwnd then u can use #wm_user etc
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post 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.
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Post by lexvictory »

well, anyone using a #wm_user value in a broadcast message is pretty silly :lol: 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!! :lol:

also is this really needed?

Code: Select all

SetGadgetText(0, PeekS(@buffer))
cant it be:

Code: Select all

SetGadgetText(0, buffer)
?
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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.)
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Post by lexvictory »

thats wat i meant.... (the wm_user bit) :lol:
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Post 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 :lol:
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

GedB wrote:I think Terry should walk before he can run.
:D I think Terry needs to learn to type or needs glasses!

Thanks, GedB, for the pointer. Works like a charm for me now.

Terry
Post Reply