import clrThe RunMeCallBack function starts the tested application the same way as we showed in the previous part. We create an independent thread and run this function in it so it does not block the console. The thread finishes its work when the tested application terminates (the main form of the application is closed) or when the console is closed.
clr.AddReference('System')
clr.AddReference("System.Windows.Forms")
from System import *
from System.Reflection import *
from System.Threading import *
from System.Windows.Forms import Application
from time import sleep
def RunMeCallBack(var):
global App
asm = Assembly.LoadFrom('GUIAT_PoC.exe')
asm_type = asm.GetType('GUIAT_PoC.frmGUIAT')
App = Activator.CreateInstance(asm_type)
Application.Run(App)
App = None
ThreadPool.QueueUserWorkItem(WaitCallback(RunMeCallBack))
while not App:
sleep(0.2)
The important part is line
App = Activator.CreateInstance(asm_type)Here we remember the instance of the main form in the variable App. We have access to the whole application thanks to the App variable! The while cycle at the end ensures waiting until the App variable is not None. Which only happens when our tested application is up and running.
The App variable is our Holy Grail. Let's explore what is inside:
>>> AppBasically, we have access to all public properties and methods. Try dir(App) and you'll see. With a trick, we can even access private properties and methods (using the power of reflection).
<GUIAT_PoC.frmGUIAT object at 0x000000000000002B
[GUIAT_PoC.frmGUIAT, Text: GUIAT - Proof of Concept]>
>>> App.Text
'GUIAT - Proof of Concept'
To find what components are on the main form, iterate through the Controls collection:
>>> for c in App.Controls:To find out what text is in the text box, try the following:
... print c.Name, c.GetType()
...
btnAddItem System.Windows.Forms.Button
lblNewItem System.Windows.Forms.Label
txtNewItem System.Windows.Forms.TextBox
lbxItems System.Windows.Forms.ListBox
btnQuit System.Windows.Forms.Button
>>> App.Controls[2].TextNow, write something directly into the text box in the tested application and call the statement again:
''
>>> App.Controls[2].TextCool, isn't it? ;-)
'something'
Next time, I show you how to simulate user interaction programatically - how to send a text or click to the tested application.