Jump to content

ConsoleWrite should get a optional binary mode (like ConsoleRead ) - (Moved)


HansHenrik
 Share

Recommended Posts

this is the ConsoleRead signature:

ConsoleRead ( [peek = False [, binary = False]] )

and here is ConsoleWrite:

ConsoleWrite ( "data" )

and ConsoleWrite will stop writing on the first null byte, 

i think ConsoleWrite should get a (optional) binary mode like ConsoleRead,

ConsoleWrite ( "data" [, binary = False] )

- while it is possible to create a binary ConsoleWrite in userland, it's more difficult (and slower, and more memory hungry, making a full copy of the data before writing) than it should be, imo, my implementation:

; binary safe ConsoleWrite()  ( AutoIt's builtin ConsoleWrite() is not binary safe, it chokes if you try to write a null byte to stdout)
Func ConsoleWriteBinary( $str)
   Local $bytesWritten=0;
   Local $stdoutHandle=_WinAPI_GetStdHandle(1);
   Local $tBuffer = DllStructCreate("byte[" & StringLen($str) & "]");
   DllStructSetData($tBuffer, 1, $str);
   _WinAPI_WriteFile ( $stdoutHandle, $tBuffer, StringLen($str), $bytesWritten);
   return $bytesWritten;
EndFunc

(idk if there's a better way to do it, but even if there's a better way, there wouldn't be any need if ConsoleWrite had a binary mode)

Edited by HansHenrik
remove Const ByRef
Link to comment
Share on other sites

  • Moderators

HansHenrik,

If you want to request a new feature in AutoIt, please open a Trac ticket.

As it is you have provided a UDF as a work-around and so the thread was moved to the Examples section which is where such things should be posted.

M23

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

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

...at times you may want to have a CUI and not stop due to a NULL or something. Makes sense.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WinAPIHObj.au3>

If Not @Compiled Then Exit MsgBox(262144, @ScriptName, "for this to showcase the code, run compied.", 60)

Global $test="";
for $i = 0 To 255
   $test = $test & Chr($i);
Next
$test &= @CRLF
ConsoleWrite("Test # 1: ""ConsoleWrite()""" & @CRLF);
ConsoleWrite($test);
ConsoleWrite("Test # 2: ""ConsoleWriteBinary()""" & @CRLF);
ConsoleWriteBinary($test);
ConsoleWrite("Done." & @CRLF);
sleep(2000)
; binary safe ConsoleWrite()  ( AutoIt's builtin ConsoleWrite() is not binary safe, it chokes if you try to write a null byte to stdout)
Func ConsoleWriteBinary( Const ByRef $str)
   Local $bytesWritten=0;
   Local $stdoutHandle=_WinAPI_GetStdHandle(1);
   Local $tBuffer = DllStructCreate("byte[" & StringLen($str) & "]");
   DllStructSetData($tBuffer, 1, $str);
   _WinAPI_WriteFile ( $stdoutHandle, $tBuffer, StringLen($str), $bytesWritten);
   return $bytesWritten;
EndFunc

 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

2 hours ago, Skysnake said:

@HansHenrik I can see the application, but what stops you from using something like

ConsoleWrite("String as HEX " & Hex($var) & @CRLF)

 

sometimes writing it in hex is not sufficient. 

let's say you want to implement the Unix Cat program in AutoIt ( https://en.wikipedia.org/wiki/Cat_(Unix) ), a program available on all Linux and MacOS systems, but not shipped with Windows,

without thinking too  much about it, i would probably write it as: 

; PS! this code is *intentionally* bugged, do you see the bug?
; PS! this code must be compiled with Aut2exe.exe with the /console argument!
; first read stdin
Local $stdinData
While True
    $stdinData = ConsoleRead(False,True);
    If @error Then ExitLoop
    If(StringLen($stdinData) == 0) Then ExitLoop
    ConsoleWrite($stdinData);
WEnd

; then read all files in arguments
For $i = 1 To $cmdLine[0] Step 1
    ConsoleWrite(FileRead($cmdLine[$i]));
Next

simple enough, right? 
but the code has a bug  (maybe it has more than 1 bug, but it has *at least* 1 bug, that i see)

do you see the bug?
 

Edited by HansHenrik
compile instructions
Link to comment
Share on other sites

NULLs have a history, so that is that. As far as ConsoleWrite a NUL, I guess no one care to use it the way he will.
I too use PHP with AutoIt but I use TCP due to the overhead of loading an EXE as CGI.
But even if the Ticket don't get adopted, the function he wrote is good to have.

The $str is not modified so it will not be added twice in AutoIt's memory, so the "Const ByRef $str" can be dropped, adding flexibility to the function.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • 2 months later...
On 5/31/2020 at 1:18 PM, Skysnake said:

I see. But... Is this a matter of using the right tool for the job? Is this a bug or was AutoIt simply never intended to do this?

 

well.. "The AutoIt language was never intended to be flexible enough to allow implementations of programs such as Unix Cat"
doesn't really sound right to me, but after reading "Jpm"'s response to https://www.autoitscript.com/trac/autoit/ticket/3764 , 
maybe you're actually right

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...