# Re: interacting with another program

**URL:** <https://www.ruby-forum.com/t/re-interacting-with-another-program/105270>\
**Category:** Ruby\
**Created:** [May 28, 2007, 10:27am UTC](https://www.ruby-forum.com/t/re-interacting-with-another-program/105270 "2007-05-28T10:27:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scoundrel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@scoundrel](https://www.ruby-forum.com/u/scoundrel)\
**Post date:** [May 28, 2007, 10:27am UTC](https://www.ruby-forum.com/t/re-interacting-with-another-program/105270/1 "2007-05-28T10:27:44Z")

</div>

> To summarize:
> 
> 1. Read data from another program by ruby program
> 2. Data analysis by ruby program
> 3. action perform in the other program by ruby program

As far as I understand you want to interact with Win32 GUI application.  
Try to use Windows messages to get data and perform actions on its GUI  
controls (text area, buttons).

All you need is:

1. Find the program window (== get it’s handle) by title
2. Find the GUI control you want to work with (== get it’s handle)
3. Send message to this control (for example - to get text from text box  
you need something like WM\_GETTEXT message (see MSDN for details), to  
push a button WM\_LBUTTONDOWN,…)

For step 1, 2 I cannot remember Win API functions by name, but you  
shouldn’t have any problems finding one (See MSDN, section about Window  
functions).

It’s very brief explanation. If you have more specific question - feel  
free to ask.

---

<div class="post-metadata">

**Author:** ![scoundrel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@scoundrel](https://www.ruby-forum.com/u/scoundrel)\
**Post date:** [May 28, 2007, 10:52am UTC](https://www.ruby-forum.com/t/re-interacting-with-another-program/105270/2 "2007-05-28T10:52:47Z")

</div>

If you use Windows as an OS to perform the interaction  
with another program, you might have a look at AutoIt and how to use  
it from Ruby:

> **[WebMD - Better information. Better health.](https://www.webmd.com/?AutoIt_For_Windows)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![scoundrel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@scoundrel](https://www.ruby-forum.com/u/scoundrel)\
**Post date:** [May 28, 2007, 6:17pm UTC](https://www.ruby-forum.com/t/re-interacting-with-another-program/105270/3 "2007-05-28T18:17:11Z")

</div>

Alexey K. wrote:

> All you need is:
> 
> 1. Find the program window (== get it’s handle) by title
> 2. Find the GUI control you want to work with (== get it’s handle)
> 3. Send message to this control (for example - to get text from text box  
> you need something like WM\_GETTEXT message (see MSDN for details), to  
> push a button WM\_LBUTTONDOWN,…)
> 
> For step 1, 2 I cannot remember Win API functions by name, but you  
> shouldn’t have any problems finding one (See MSDN, section about Window  
> functions).
> 
> It’s very brief explanation. If you have more specific question - feel  
> free to ask.

1. 

This should be the function to find the window handle:

> **[FindWindowA function (winuser.h) - Win32 apps](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowa?redirectedfrom=MSDN)**
>
> Retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search. (ANSI)

For this I also need the name of the class, but I have installed a tool  
that can show me that.

1. 

I couldn’t find a function on MSDN to find a controls handle, so I’m  
kinda stuck on this one. Please help me out here.

1. 

To push a button I’ll need  
BM\_CLICK([BM\_CLICK message (Winuser.h) - Win32 apps | Microsoft Learn](http://msdn2.microsoft.com/en-us/library/ms673557.aspx))?

For textfield i want to get the text from, I think I have to use  
EM\_GETTEXTEX ([EM\_GETTEXTEX message (Richedit.h) - Win32 apps | Microsoft Learn](http://msdn2.microsoft.com/en-us/library/ms652050.aspx))  
right?

---

<div class="post-metadata">

**Author:** ![scoundrel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@scoundrel](https://www.ruby-forum.com/u/scoundrel)\
**Post date:** [May 30, 2007, 4:59am UTC](https://www.ruby-forum.com/t/re-interacting-with-another-program/105270/4 "2007-05-30T04:59:14Z")

</div>

Jeppe J. wrote:

> 1. 
> 
> To push a button I’ll need  
> BM\_CLICK([BM\_CLICK message (Winuser.h) - Win32 apps | Microsoft Learn](http://msdn2.microsoft.com/en-us/library/ms673557.aspx))?
> 
> For textfield i want to get the text from, I think I have to use  
> EM\_GETTEXTEX ([EM\_GETTEXTEX message (Richedit.h) - Win32 apps | Microsoft Learn](http://msdn2.microsoft.com/en-us/library/ms652050.aspx))  
> right?

I believe this should solve most of your problems.  
[http://raa.ruby-lang.org/project/win32-guitest/](http://raa.ruby-lang.org/project/win32-guitest/)  
Hope it helps.  
Regards,  
Manish

---

<div class="post-metadata">

**Author:** ![scoundrel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@scoundrel](https://www.ruby-forum.com/u/scoundrel)\
**Post date:** [May 30, 2007, 6:30am UTC](https://www.ruby-forum.com/t/re-interacting-with-another-program/105270/5 "2007-05-30T06:30:30Z")

</div>

Manish S. wrote:

> Jeppe J. wrote:
> 
> > 1. 
> > 
> > To push a button I’ll need  
> > BM\_CLICK([BM\_CLICK message (Winuser.h) - Win32 apps | Microsoft Learn](http://msdn2.microsoft.com/en-us/library/ms673557.aspx))?
> > 
> > For textfield i want to get the text from, I think I have to use  
> > EM\_GETTEXTEX ([EM\_GETTEXTEX message (Richedit.h) - Win32 apps | Microsoft Learn](http://msdn2.microsoft.com/en-us/library/ms652050.aspx))  
> > right?
> 
> I believe this should solve most of your problems.  
> [http://raa.ruby-lang.org/project/win32-guitest/](http://raa.ruby-lang.org/project/win32-guitest/)  
> Hope it helps.  
> Regards,  
> Manish

Thanks

I’ve actually looked a bit into autoit for ruby and it solved most of my  
problems, but maybe this will come in handy if I have any trouble with  
autoit.

My biggest problem right now is that the program I want to automate is  
using some web based elements, which means that there is some text I  
cant seem to get from the program. I’m considering using some kind of  
screenscrape to regonize the text I need, if such solution is available  
(or even nessecary).
