Jump to content

Question on UI Automation and C# integration


zenocon
 Share

Recommended Posts

I have a script I write as .au3 which uses the IUIA wrapper functions.  It is working, but I need to invoke it from a C# WinForms app.  I see there is AutoITX dll that exposes the scripting language to C#/.NET, but I think I can import UDF functions like IUI wrapper using that, can I?

I think the best option I suppose is to just Invoke the built .au3 executable from C#.  This only presents a challenge to get the return values from the script.  The script scrapes info out of a legacy Windows app that I need.  I can probably dump this to a file (ugly) or perhaps pass the info through stdio stream (less ugly).  

What are other people doing who need to integrate with .NET apps?

Link to comment
Share on other sites

  1. More background on why will help so a better answer can be given
  2. Automation namespaces of .NET can be a better choice: https://msdn.microsoft.com/en-us/library/system.windows.automation(v=vs.110).aspx
  3. Why not make your UI within AutoIt?
  4. The more advanced research topics from AutoIt are
    Use objects and expose them thru Running Object Table
    Expand your AU3 EXE script with some kind of tcp/ip client server communication and send commands (you have to build your own logic)

 

  • AutoItX.Dll is as far as I know not able to include au3 libraries (never tried, and never seen examples of it)
  • What are other people doing who need to integrate with .NET apps?
    Check for CLR.AU3 in forums. Integrates .NET into AU3 scripts.
    Other way around is harder 

 

Link to comment
Share on other sites

Thank you @junkew I have looked at the automation namespace in .NET, and while I think I could probably accomplish the same with that, it is easier for me to write .au3 script and test right now -- it's a faster development cycle with my situation at the moment.

The UI I'm scraping is a legacy application that I have no control over and very limited access to -- making it difficult to verify the scripts.  The legacy application does not provide any proper API for integration, so this is the path we're going to do an integration, after only having moderate success with an OCR approach.

Link to comment
Share on other sites

if you have Visual Studio Enterprise 2015 or 2017 you could record tests in C#. Called a Coded UI Project. I made the switch to this. then I can whip up a host app in c# that brings up all those tests using the Coded UI dll you generate. They don't recommend you modify the code, though you can, but then it won't automatically manage it for you. If changes, re-record, or just record and maintain from there. your choice.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

I don't have Enterprise.  Nonetheless, I am proficient in C#/.NET so I'm thinking probably the best thing to do is to just build a lib that does the scraping.  AutoIT / IUIA proved very useful for me to learn the API, and prototype proof-of-concept that showed we are able to scrape the info we want out of the legacy API.  In addition, it was an easy / quick dev lifecycle to just rebuild the script to an exe and remote it to their desktop to test it, run inspect.exe to analyze problems and rinse and repeat.  

But now that I have the script working, I think I probably will convert it to a .NET library using the official API.

Link to comment
Share on other sites

LOL, I think my first problem is finding the app handle.  With AutoIT I'm using a regular expression to nab the title, which is very custom and dynamic depending on what screen they have open.

Local $hWindow = WinGetHandle("[REGEXPTITLE:^\(SVC - .*\) - App-Name$]")

; then convert it to UIElement, etc.

With .NET API it's something like this:

var app = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, " no regexp here :( "));

Link to comment
Share on other sites

No, it's slow b/c I'm using a TreeWalker in the UI Automation API to iterate through children of desktop element to then check name property against Regex.  It takes 5-7 sec which is ridiculous.

I'm now trying a different approach using "user32.dll"

Edited by zenocon
Link to comment
Share on other sites

25 minutes ago, zenocon said:

249 ms to find it now with this little useful snippet: https://code.msdn.microsoft.com/windowsdesktop/Enumerate-top-level-9aa9d7c1/sourcecode?fileId=44683&pathId=1184961558

Bulk of that might be in test setup, since I'm testing it in a unit test.  But it is definitely much speedier.  

 

can you change the title of the thread by editing your first post to say SOLVED? this is great and I will probably need this as well! thanks for posting!

you can't beat C# dude. it's why I switched. super fast, easy to program.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...