Page 1 of 2
passing variables between external programme and PB
Posted: Sun Jun 22, 2003 10:02 pm
by cecilcheah
Hi
Can anyone tell me what is the best way to do the following:
PB will send a command to an external programme to fetch a variable value from that programme and use that value to do some stuff.
Will post message alone do? If i use postMessage, how can PB retrieve the answer from that programme and get the value of some variables?
Can anyone give me some pointers?
Thanks
Cecil
Posted: Sun Jun 22, 2003 11:31 pm
by freak
For Sending long variables, a simple SendMessage_() would do it.
If you send expliztly ro the window of the receiving app (and not to
#HWND_BROADCAST), you can simply choose you own Constant as
the Message Value. Choose something above #WM_USER, as all
message numbers below are reserved for the system, the ones above
#WM_user are free to use by apps.
You can set the lParam and wParam parameters to the VAriables, yopu wish to send.
The App, that will receave the message, should have a WindowCallback()
procedure set to receive it, (is better that catching it with the usual
message loop)
In the Callback,when receiving your message constant, you can work
with the sent values, and then return a Value with ProcedureReturn
(other than the usual #PB_ProcessPureBAsicEvents). This return
value will be the return VAlue of your SendMessage_() call in the first app.
This means, that the first app waits, until the second one has processed
the message in the WindowCallback(), and then return, so you shouldn't
do things that take long in the callback procedure.
Hmm, does sombody understand my explanation, or am i talking nonsense?
Anyway, here is another Example, of how to Send Strings between two Apps:
http://freak.purearea.net/code/
Look at: SendMessageSttring and SendMessageString2 examples.
Hope that helps...
Timo
Posted: Mon Jun 23, 2003 6:59 am
by cecilcheah
Hi
The App, that will receave the message, should have a WindowCallback()
procedure set to receive it, (is better that catching it with the usual
message loop)
You see the external programme is really an external independent programme that i cannot programme. I can programme PB, to send message to this alien programme.
So my PB programme would send a message to this external programme, ask it to execute something and return some variable values (which can be a very long string). The problem is i do not know how to receive this variable values.
I have looked at your SendMessage examples, it works fine with 2 PB programmes, but broken down when one of them is not PB.
Any idea?
Cecil
Posted: Mon Jun 23, 2003 11:45 am
by freak
Then you ned to know, how the other program want's to pass the data.
How should I know that?
There's an endless number of possibilities, how to do that.
You have some description of that program, something to start with?
Maybe I can help you then...
Timo
Posted: Mon Jun 23, 2003 12:33 pm
by cecilcheah
What kind of description do you want?
Cecil
Posted: Mon Jun 23, 2003 1:03 pm
by freak
How does the other program expect the variables/data to be passed, and
how will it return it.
If you want to communicate with that program, you need to know,
how this prohgram handles the communication.
What program do you talk about btw?
Timo
Posted: Mon Jun 23, 2003 4:35 pm
by cecilcheah
Is there not a general way in API that can do this? Can one not trap the message by poking in the memory?
Cecil
Posted: Mon Jun 23, 2003 4:54 pm
by freak
So the other program is not designed to exchange data with your
program, am i right?
Or course you can read the program's memory (if you have the right to),
but how would you know wher to look for your data.
Sending messages to the other program only makes sense, if the other
program knows how to handle them.
There is no standart "give me the contents of variable xyz" message.
Maybe I still don't get what you try to do.
If you tell me what program it is, and what you want to do, maybe i can
help you.
Timo
Posted: Mon Jun 23, 2003 4:55 pm
by Fred
No, each program has it's own memory range, from 0 to 2 GB (other 2GB are for system). So you can't access another memory area except with regular API function like 'ReadProcessMemory_()' and such. It's not like on the Amiga for example.
Posted: Mon Jun 23, 2003 5:21 pm
by GedB
Cecil,
Is this application a windows application with text and edit controls?
Is the data you want in these controls?
Are you sending data in the form of emulated keystrokes and mouse clicks?
If this is the case then there may be a way.
Alternatively, is it a console application?
Posted: Mon Jun 23, 2003 6:14 pm
by cecilcheah
Hi
OK, i try to answer everyone question as best as i can.
So the other program is not designed to exchange data with your
program, am i right?
Yes, you are right. It is just a Windows programme which i want to make a plugin to this programme with PB. The Window application is called Toolbook.
Sending messages to the other program only makes sense, if the other
program knows how to handle them.
I have succeeded in sending messages to this programme. I am able to move a control say a button to another position. Now i want the programme to return some values back to my PB programme after receiving the message from PB.
Is this application a windows application with text and edit controls?
Yes.
Is the data you want in these controls?
Some yes, some not. Like global variables will not be in a control, but in the programme.
Are you sending data in the form of emulated keystrokes and mouse clicks?
No, I use a modified send messages function to send commands which is used by this programme.
is it a console application?
No, full window application
Anyway i can pass variables between the two?
Cecil
Posted: Mon Jun 23, 2003 6:28 pm
by GedB
The data that is held in windows controls you can read data from. Use the following API calls:
WindowFromPoint_()
Use this to get the handle for the control.
GetWindowText_()
Once you have the handle you can get the controls text.
There is no way of accessing the programs internal variables. Just imagine the security consequences if you could!
How did you find out what messages to send the application?
I see that Toolbook has its own language, Openscript. Was the application your trying to talk to written in OpenScript?
It may be that methods of looking at OpenScripts internals exist - find a good OpenScript developers forum and ask for examples in C. Converting theses to Purebasic should be easy enough.
Posted: Mon Jun 23, 2003 7:33 pm
by cecilcheah
Hi
WindowFromPoint_()
Use this to get the handle for the control.
GetWindowText_()
Once you have the handle you can get the controls text.
No, i need to access more properties than this. I know how to do this already.
How did you find out what messages to send the application?
Openscript command, as you say.
I see that Toolbook has its own language, Openscript. Was the application your trying to talk to written in OpenScript?
No, i actually try to access Toolbook stuff itself, not programme developed from Toolbook.
find a good OpenScript developers
I am the expert of it, or one of the experts anyway.
So looks like there is no way of doing it unless i have the internal sozurce code of toolbook itself...
Take a look at this web site:
http://www.webcbt.ch, try the Download - Utility link and see the TBK Capture i have just developed from PB and made available for free for Toolbook Developers.
Thanks
Cecil
Posted: Mon Jun 23, 2003 10:11 pm
by freak
> So you can't access another memory area except with regular API
> function like 'ReadProcessMemory_()'
Of course. That's why i said, if you have the right to.
OpenProcess_(), ReadProcessMemory_() etc...
cecilcheah:
Sorry, i can't help you there, i have no idea how to find the memory area
of that program where, let's say the global variables are stored.
Timo
Posted: Mon Jun 23, 2003 11:50 pm
by ricardo
Cecil,
The situation is more complicated.
Imagine you code the next app
Code: Select all
If OpenWindow(0,100,150,450,200,#PB_Window_SystemMenu,"Test")
CreateGadgetList(WindowID())
StringGadget(1,50,50,350,20,"")
ButtonGadget(2,200,100,50,25,"Test")
Repeat
EventID=WaitWindowEvent()
Select EventID
Case #PB_EventGadget
Select EventGadgetID()
Case 2
MyVar$ = ReplaceString(LCase(GetGadgetText(1)),"a","b",0)
EndSelect
EndSelect
Until EventID=#PB_EventCloseWindow
EndIf
This small app just do something (whatever) to a string and have it in memory.
As you see, the app was not planned to interact with an external one, so if you want to acces the MyVar$ value you have to *hack* it, is NOT easy.
AFAIK the is no any easy API that let you do this.
Some apps, like winamp, was developed having in mind a way to exchange info with external apps and then you can find a winamp API on its documentation that tell you how to do it, because as there is no 'standard' way, every app works in the way that the developers wants (many sometime could use some 'protocol' but other times uses its own).
In resume: Look at the documentation of the app that you want to interacti with, if it DOSENT describe any API to interact with, then you need to *hack* it if you want to do something more that sendkeys, etc.
For security reason (to avoid hacks) every window app uses its own RESTRICTED memory area to store values, etc.
*The opposite model is the dll, its developed to share its procedures, However you must have the documentation to use it properly.