viewpoint-particle

Author Topic: Control WinBatch from .NET and return results.  (Read 113 times)

chris-taylor

  • Newbie
  • *
  • Posts: 3
Control WinBatch from .NET and return results.
« on: January 21, 2023, 08:18:27 am »
Hello.  I need to initiate a WinBatch script from a .NET application.  The WinBatch script will control another desktop application and should pass back to the parent .NET process details about what it is doing and finding.  For example it should pass back to .NET the text it discovers in certain text-boxes of the Desktop application.  How can I do this please?

ChuckC

  • Sr. Member
  • ****
  • Posts: 407
Re: Control WinBatch from .NET and return results.
« Reply #1 on: January 21, 2023, 10:49:49 am »
You will need to learn about a concept called "Inter-Process Communication", or IPC for short, and then learn about the specific details about how to use the IPC mechanisms that are present on Windows.

The most basic thing you could do is have the WinBatch script write status information to a file and have the .NET application poll a directory for changes and then read the status files as they are written.

A more sophisticated way would be to create a "named pipe" as a server in the .NET application and then have the WinBatch script open the same named pipe as a client.  From that point, the two programs would then communicate a full-duplex bi-directional manner passing messages back & forth.  It's entirely up to you to define the format & content of those messages.  I'd strongly suggest something simple and text-based like JSON.

Alternatively, you could investigate how to use System.Windows.Automation in .NET and control the target application directly from the .NET application and eliminate the need for a separate WinBatch script.  And, likewise, if WinBatch can perform the same functionality as the .NET application, then there is the possibility of eliminating the .NET application and doing everything from within a single WinBatch script.  WinBatch has access to nearly all of the functionality of .NET via it's ability to utilize .NET's CLR [Common Language Runtime].


stanl

  • Pundit
  • *****
  • Posts: 1707
Re: Control WinBatch from .NET and return results.
« Reply #2 on: January 21, 2023, 03:13:21 pm »
Perfect Analysis.  Would hope OP would choose door #3 and ask could be explored with CLR.


chris-taylor

  • Newbie
  • *
  • Posts: 3
Re: Control WinBatch from .NET and return results.
« Reply #3 on: January 21, 2023, 11:42:06 pm »
Thank you.  For the sake of completeness how about any of the following:

Can a WinBatch script be converted to a DLL so .NET could call methods in the DLL?
Can a WinBatch script expose a COM interface for .NET to use?
Can a WinBatch script write to stdout, for .NET to consume?

stanl

  • Pundit
  • *****
  • Posts: 1707
Re: Control WinBatch from .NET and return results.
« Reply #4 on: January 22, 2023, 06:05:07 am »
Tony or Chuck should answer your latest questions. What is interesting is you are asking about Winbatch being called from .NET, rather than the other way around. Or, I misread your ask.

ChuckC

  • Sr. Member
  • ****
  • Posts: 407
Re: Control WinBatch from .NET and return results.
« Reply #5 on: January 22, 2023, 07:05:49 am »
I'm not aware of any possible options for building a WinBatch script into a native DLL library or into an executable that is registered & runs as an out-of-process COM server.

The 3rd option may be possible, though.  Current versions of WinBatch can be built into a program that runs as a console application in a console window instead of as a GUI application.  If the parent process can interact with the console application, then there may be some possibility of making the "connection" that you're looking to make.  Prior to being able to build a WinBatch script into a console application as a built-in feature of WinBatch+Compiler, there were multiple approached contributed to the tech support db by various WinBatch users who used the DllCall() mechanism to directly call Win32 API functions that allowed for cobbling together some STDIN/STDOUT communication between a script and another process.  In most cases, it was just tying the GUI script process to an instance of CONHOST.exe to effectively allow the script to have a console window to interact with.  But, if you try going that route, it's not exactly a reliable means of communication to have one process screen-scraping a console window to interact with the process associated with the console window.  You'd find yourself right back where you started with having expended far more effort than would have been required to go down the Named Pipe path for IPC.

Others will hopefully chime in with any additional ideas & suggestions that come to mind in case I've missed anything topical & pertinent.

td

  • Tech Support
  • *****
  • Posts: 4208
    • WinBatch
Re: Control WinBatch from .NET and return results.
« Reply #6 on: January 22, 2023, 08:46:07 am »
WinBatch has a console version that runs in its own isolated console environment. There is no compiler option to create a console application just the ability to run scripts in an isolated console. There are techniques for writing to a stdout in the WinBatch Tech database. Still, they likely will not help with interprocess communication since WinBatch is a Windows application and not a console application.

Your question implies that you have control over the ".Net" application so the cleanest approach may be to simply use System.Windows.Automation as Chuck suggested.

Also, Chuck's named pipe suggestion has merit but WinBatch named pipes have data size limits and take some time to understand and implement.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

chris-taylor

  • Newbie
  • *
  • Posts: 3
Re: Control WinBatch from .NET and return results.
« Reply #7 on: January 24, 2023, 02:35:17 am »
I really appreciate all of your comments.