Jump to content

writing to parent DOS window


Emolas
 Share

Recommended Posts

I have a batch file calling a compiled autoit file and I want the autoit to write to the DOS window that the batch file opened.

For instance:

test.bat contains the following

     start /wait script.exe

script.exe contains the following:

     ConsoleWrite("this is a test")
     Exit

I know consolewrite isn't the way to do it but I can't seem to find a way to write to the window that ran the script.  Lots of ways to write to windows that the script opens but nothing the other way around.

Link to comment
Share on other sites

As Nisteo said, if your AutoIt script launches the exe, it can read the output from the launched script:

#AutoIt3Wrapper_Change2CUI=y
#include <AutoItConstants.au3>

Local $iPid = Run("test.exe", @ScriptDir, @SW_HIDE, $STDERR_MERGED)
ProcessWaitClose($iPid)
; Read the Stdout stream of the PID returned by Run. This can also be done in a while loop. Look at the example for StderrRead.
Local $sOutput = StdoutRead($iPid)
ConsoleWrite($sOutput & @CRLF)

However I'm not aware of a way for the child/launched process to read output from the main/launcher script. If you want to communicate between processes, check out the IPC methods: https://www.autoitscript.com/wiki/User_Defined_Functions#Inter_Process_Communications

 

Is there something specific that you're looking to do? Maybe there's a solution besides reading output from either process.

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

You need to call it directly instead of using start!

For example:

test.bat contains the following

  script.exe

script.exe compiled with the code:

#AutoIt3Wrapper_Change2CUI=y
#pragma compile(Console, true)
Opt('TrayMenuMode',1)

ConsoleWrite("this is a test")
Sleep(2000)  ; Stop the screen to let you see !
ConsoleWrite("this is a test 2")
Sleep(5000)   ; Stop the screen to let you see !
Exit

 

Regards,
 

Link to comment
Share on other sites

You do it this way.

tst00.cmd: 

start "CmdIdentifierString" /wait tst00.exe

tst00.au3: 

#AutoIt3Wrapper_Change2CUI=Y

;#RequireAdmin ; Maybe you need #RequireAdmin rights?
; Maybe exclude exe/folder from antivirus program?

DllCall( "Kernel32.dll", "int", "AttachConsole", "dword", WinGetProcess( "CmdIdentifierString" ) )

Func CmdConsoleWrite( $sStr )
  DllCall( "Kernel32.dll", "bool", "WriteConsoleW", "handle", _
    DllCall( "Kernel32.dll", "handle", "GetStdHandle", "int", -11 )[0], _ ; -11 = STD_OUTPUT_HANDLE
    "wstr", $sStr, "dword", StringLen( $sStr ), "dword*", 0, "ptr", 0 )
EndFunc

CmdConsoleWrite( @CRLF )

For $i = 1 To 5
  CmdConsoleWrite( "This is test number " & $i & @CRLF )
  If $i < 5 Then Sleep( 3000 )
Next

Exit

Run tst00.cmd in a Command Prompt or double-click.

Tested on Windows 7 as 32/64 bit code.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...