Jump to content
Sign in to follow this  
jmichae3

consolewrite doesn't work, need console print function

Recommended Posts

jmichae3

3.3.8.1, windows 7 64-bit

what does consolewrite actually write to? it doesn't write to stdout because I see nothing in the cmd shell. it doesn't write to the console. so the function must be named wrong. yes? does it only write to scite? should it be called scitewrite? having cmd shell "stdout" console application type of output would be very useful to me. there are a set of win32 console functions specifically for this. they are also available as console. in .net you can look them up on msdn.

help turning these into (a) usable function(s) would be appreciated.

I wanted simply a function called consoleprint(str) which

1.gets the console of the parent/calling process which should be cmd.exe and it should

2.write str to the console without any bling or fanfare (you can add attributes like colors if you want) and

3.find the current horizontal cursor position

4.fill in the rest of the row with spaces. this will put the cursor on the next line, which is the equivalent of @crlf. with console output, you can only simulate @crlf.

this function assumes you are running the script from a cmd.exe window. I suppose you could *create* a dummy console window and write str to that if a parent cannot be found. you would need a window title and a

benefit: you can copy the output to the clipboard and paste them anywhere.

I know there are win32 console mode functions for I/O. I would be interested in using those through a function if possible. having something I can simply #include from autoit would be wonderful.

but I am not used to autoit's way of handling dll's. and I could not get them to work. and I am clueless about autoit's structures (I am more used to c++). but I need something which can do GUIs and C++ doesn't.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683175%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683193%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683163%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms687410%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682663(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682122(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683150(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686050(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686039(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686044(v=vs.85).aspx

and in case you need it,

http://msdn.microsoft.com/en-us/library/windows/desktop/ms681952(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073%28v=vs.85%29.aspx

I would like to be able to write lines. this involves writing a full row of chars.

Share this post


Link to post
Share on other sites
JohnOne

It writes to the scite console, so there is nothing wrong with the naming.

I think AutoIt scripts are window based by default, so you would have to specify you want a CUI by preprocessor "#" directive I believe.

See help file regarding CUI.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
jchd

It writes to stdout.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
MHz

On a Gui application, the streams can be closed initially if not used, though as you have noticed that Scite is setup to use these streams and so you may see them in Scites console. You can do a similar trick with CMD by piping the output to More. Or you can redirect the streams by using > to a file.

e.g. 1 line in a script and compile to compiledscript.exe as a GUI.

ConsoleWrite('Hello World' & @CRLF)

Now, in the command prompt at the working directory to the compiled script, type

compiledscript.exe | more

Now the compiled script executed should print out to the CMD prompt

Hello World

Now, if you want that Stdout behavior by default, then you can compile to CUI as JohnOne suggests. You can create GUIs from a console application though it could be challenging to do it the opposite way. So ConsoleWrite goes to Stdout and ConsoleWriteError goes to Stderr if you enable the use of the pipes. :)

Share this post


Link to post
Share on other sites
mLipok

I wonder if EDIT: you we can get around this limitation as follows:

This is a description of what I think needs to be done:
create two scripts

starter.au3 - as CUI
and
program.au3 - as GUI

"Starter.exe"
must run "program.exe"

"program.exe" and "Starter.exe" communicate with one another.

"Starter.exe" remains active until the time when, "program.exe" is completed

EDIT: "Starter.exe" writes the console (CMD) all provide him "program.exe"

EDIT: "Starter.exe" writes the console (CMD) all messages transmitted by the "program.exe"

How do you think this is a good idea?

Edited by mlipok

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2018-03-17

Share this post


Link to post
Share on other sites
mLipok

try this:

build/compile both attached au3 files

 


how to use and results in cmd.exe

 

L:TOOLsMacrotest>starter.exe test_consoleout.exe

TEST ConsoleWrite
 
Exiting...
 
L:TOOLsMacrotest>

 

starter.au3

test_consoleout.au3


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2018-03-17

Share this post


Link to post
Share on other sites
jmichae3

On a Gui application, the streams can be closed initially if not used, though as you have noticed that Scite is setup to use these streams and so you may see them in Scites console. You can do a similar trick with CMD by piping the output to More. Or you can redirect the streams by using > to a file.

e.g. 1 line in a script and compile to compiledscript.exe as a GUI.

ConsoleWrite('Hello World' & @CRLF)

Now, in the command prompt at the working directory to the compiled script, type

compiledscript.exe | more

Now the compiled script executed should print out to the CMD prompt

Hello World

Now, if you want that Stdout behavior by default, then you can compile to CUI as JohnOne suggests. You can create GUIs from a console application though it could be challenging to do it the opposite way. So ConsoleWrite goes to Stdout and ConsoleWriteError goes to Stderr if you enable the use of the pipes. :)

you mean I can't do this by running the .au3 file (which calls the autoit executable)?

so far, my attempts have been unsuccessful (of course, I have never tried console code before, I am just guessing).

I wanted console output from a GUI. all of my autoit applications are GUIs.

I even tried AttachConsole($ATTACH_PARENT_PROCESS).

but this didn't help, and I also tried SetConsoleActiveScreenBuffer($hstd)

consoleprint.au3 below includes a lot of the Win32 console portion. I am also making one for processes.

consoleprint.au3

process.au3

Edited by jmichae3

Share this post


Link to post
Share on other sites
jmichae3

I am thinking I would need to do an AttachConsole with the process id of the cmd shell that executed it, I had thought this would be a parent process. maybe it's not? I would have a hard time figuring out what the exact instance of cmd shell would be that executed the interpreter...

it's like coding ProcessExplorer.

there are examples from stackoverflow and codesomethingorother listed on msdn for parent process id, but they are licensed. :-(

most of the time I am not at the point of compiling, I am still in development/unit testing/testing/debugging stages. I am there right now on one of my projects.

Edited by jmichae3

Share this post


Link to post
Share on other sites
jmichae3

I am getting incorrect error messages from au3check again. getprocess.au3 which is what I renamed it to and made some corrections, must have thrown au3check for a loop.

  • line numbers are wrong
  • the code shown for the line numbers is wrong
  • the error message could be correct, but I have no way of knowing where it's at.
  • it's consistent for this instance of file.

 

Tue 09/24/2013 22:55:10.05|d:prjlibconsoleprint|>au3check getprocess.au3
AutoIt3 Syntax Checker v1.54.22  Copyright © 2007-2011 Tylo & AutoIt Team

d:prjlibconsoleprintgetprocess.au3(172,47) : ERROR: syntax error
global $IO_COUNTERS=dllstructcreate(ULONGLONG ReadOperationCount
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
d:prjlibconsoleprintgetprocess.au3(174,51) : WARNING: $lpIoCounters: possibly used before declaration.
func GetProcessIoCounters($hProcess, $lpIoCounters)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
d:prjlibconsoleprintgetprocess.au3(176,85) : WARNING: $hProcess: possibly used before declaration.
        return dllcall("kernel32.dll", "DWORD", "GetProcessIoCounters", "HANDLE", $hProcess,
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
d:prjlibconsoleprintgetprocess.au3(176,108) : ERROR: 'Return' not allowed from global scope.
        return dllcall("kernel32.dll", "DWORD", "GetProcessIoCounters", "HANDLE", $hProcess, "ptr", $lpIoCounters)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
d:prjlibconsoleprintgetprocess.au3(177,1) : ERROR: syntax error
endfunc
^
d:prjlibconsoleprintgetprocess.au3(174,51) : ERROR: $lpIoCounters: undeclared global variable.
func GetProcessIoCounters($hProcess, $lpIoCounters)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
d:prjlibconsoleprintgetprocess.au3(176,85) : ERROR: $hProcess: undeclared global variable.
        return dllcall("kernel32.dll", "DWORD", "GetProcessIoCounters", "HANDLE", $hProcess,
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
getprocess.au3 - 5 error(s), 2 warning(s)

Tue 09/24/2013 22:55:15.10|d:prjlibconsoleprint|>

I am using 3.3.8.1 unicode on win7ult64sp1. I have run into this problem before, it's staring me in the face, and I can't debug my script without some proper error messages. again, running the script produces proper error messages.

getprocess.au3

getprocess-bogus-errs.txt

Share this post


Link to post
Share on other sites
jmichae3

here's the latest versions. I got them past the syntax check problems. I tested the above against the latest beta and no change.

I still have not gotten anything to work. so far, anything that calls a dll in autoit has not worked for me. any clues? do I need to put the _ in front of the dll function names?

why does _WinAPI_AttachConsole()'s return value accessed like an array (indeed pretty much all of dllcall stuff) when clearly the return value of win32's AttachConsole() is a BOOL and not a ptr type?

I am curious, because either WinAPI.au3 is implemented wrong, or I have implemented mine wrong. what exactly does DLLCall() return if it's not the data type specified? what happens if the dllcall returns an LPTSTR?

 

it looks like dllcall always returns an array.

getprocess.au3

consoleprint.au3

Edited by jmichae3

Share this post


Link to post
Share on other sites
JohnOne

Yes DllCall Returns an array, the return of that call is in $Array[0].

The documentation is very clear about this.

If the function call fails then @error is set to 1. Otherwise an array is returned that contains the function return value and a copy of all the parameters (including parameters that the function may have modified when passed by reference).

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
jmichae3

could that info please be listed under "return value" in the docs? otherwise everyone is going to assume it just returns a normal value.

Share this post


Link to post
Share on other sites
jmichae3

I did get it to work. consolewrite  I can't use, it doesn't work. I am using _WinAPI_WriteFile($hstd) to write output to the console.

call to SetConsoleActiveScreenBuffer($hstd) is not needed.

I created a struct with the string in it first of course.

and I fixed my header files so they work. I removed the microsoft-copyrighted example code for newlines and scrolling which are not needed. it's all handled internally. 

 

enjoy.

Author:Jim Michaels
name:ConsolePrint()
arguments:$str:string to output to console
Abstract:writes str to parent console of window or creates new console and writes str to the new console.
        only ASCII/ANSI works. @crlf is handled appropriately.
        the cmd shell's prompt will not show after this is excecuted,
        but it is there, you can run a cmd shell command and it will go.
        if you want a prompt, hit Enter.
        appends @crlf to string and sends to ConsoleOut()
Created:9/25/2013
 

Author:Jim Michaels
name:ConsoleOut()
arguments:$str:string to output to console
Abstract:writes str to parent console of window or creates new console and writes str to the new console
        only ASCII/ANSI works. @crlf is handled appropriately.
        the cmd shell's prompt will not show after this is excecuted,
        but it is there, you can run a cmd shell command and it will go.
        if you want a prompt, hit Enter.
Created:9/25/2013
 

there are many many more functions from the win32 api added whihc would be useful for system info about processes and such.

 

getprocess.au3

consoleprint.au3

Share this post


Link to post
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
Sign in to follow this  

×