Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

StdIn()


  • Please log in to reply
5 replies to this topic
Lexikos
  • Administrators
  • 9445 posts
  • Last active:
  • Joined: 17 Oct 2006
StdIn(max_chars = 0xfff)
Reads up to max_chars bytes from the script's standard input handle.
StdIn(max_chars=0xfff)
{
    static hStdIn=-1

    if (hStdIn = -1)
    {
        hStdIn := DllCall("GetStdHandle", "UInt", -10) ; -10=STD_INPUT_HANDLE
        if ErrorLevel
            return 0
    }

    max_chars := VarSetCapacity(text, max_chars, 0)

    ret := DllCall("ReadFile"
        , "UInt", hStdIn        ; hFile
        ,  "Str", text          ; lpBuffer
        , "UInt", max_chars     ; nNumberOfBytesToRead
        , "UIntP", bytesRead    ; lpNumberOfBytesRead
        , "UInt", 0)            ; lpOverlapped

    return text
}
Not tested for Unicode compatibility. Requested by tinku99 here.

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Here you go:
StdIn(max_chars=0xfff)
{
    static hStdIn=-1
    [color=orange]; The following is for vanilla compatibility
    ptrtype := (A_PtrSize = 8) ? "ptr" : "uint"[/color]

    if (hStdIn = -1)
    {
        hStdIn := DllCall("GetStdHandle", "UInt", -10[color=orange],  ptrtype[/color]) ; -10=STD_INPUT_HANDLE
        if ErrorLevel
            return 0
    }

    max_chars := VarSetCapacity(text, max_chars[color=orange]*(!!A_IsUnicode+1)[/color], 0)

    ret := DllCall("ReadFile"
        ,  [color=orange]ptrtype[/color], hStdIn        ; hFile
        ,  "Str", text          ; lpBuffer
        , "UInt", max_chars[color=orange]*(!!A_IsUnicode+1)[/color]     ; nNumberOfBytesToRead
        , "UInt*", bytesRead    ; lpNumberOfBytesRead
        ,  [color=orange]ptrtype[/color], 0)            ; lpOverlapped

    return text
}

Pretty easy to fix. Works (hopefully) on vanilla AutoHotkey, ANSI/Unicode AutoHotkey_L and ANSI/Unicode AutoHotkey64. That's what you call portable code ;p

tinku99
  • Members
  • 560 posts
  • Last active: Nov 18 2013 02:43 AM
  • Joined: 03 Aug 2007
thanks lexikos.

earlsd
  • Members
  • 50 posts
  • Last active: Sep 02 2011 07:24 PM
  • Joined: 13 Apr 2011
I can't seem to get this function to return more than 4107 bytes no matter what I set max_chars to. Any ideas what I'm doing wrong?

Lexikos
  • Administrators
  • 9445 posts
  • Last active:
  • Joined: 17 Oct 2006
If there's data available, it will return it. It won't wait until your buffer is full. If you want more data, call the function again.

earlsd
  • Members
  • 50 posts
  • Last active: Sep 02 2011 07:24 PM
  • Joined: 13 Apr 2011

If there's data available, it will return it. It won't wait until your buffer is full. If you want more data, call the function again.



FYI: I was able to loop until the function stopped returning data then break. I guess I just didn't know how to use it:

loop
{
    sleep 100 ;Seems to need to wait for data
	PipeBuffer:=StdIn()
    if PipeBuffer is space
        break
    PipeText:=PipeText . PipeBuffer
}