Jump to content
HansHenrik

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

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

(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)

Share this post


Link to post
Share on other sites
Posted (edited)

why was this moved to "sample scripts" ? this thread isn't about sample scripts, i'm requesting an enhancement of a core AutoIt function =/

Edited by HansHenrik

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites
Posted (edited)
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

Share this post


Link to post
Share on other sites

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?

 


Skysnake

Why is the snake in the sky?

Share this post


Link to post
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.

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...