News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

WriteConsoleInput

Started by jj2007, January 03, 2013, 09:04:53 AM

Previous topic - Next topic

jj2007

A while ago we played with console I/O here.

The core code is this, and it works fine to prefill the console buffer:

@@:        dec ebx        ; write the prefill string to the console input buffer
        js @F
        movzx eax, byte ptr [ecx+ebx]        ; read backwards, start with Len-1
        inc edi        ; count the records
       
push 0        ; dwControlKeyState
       
push ax        ; UnicodeChar
        push ax        ; wVirtualKeyCode
        push 0        ; looks unprofessional but it works better without MapVirtualKey
        push 1        ; bKeyDown
        push KEY_EVENT
        jmp @B
@@:        invoke GetStdHandle, STD_INPUT_HANDLE        ; Google GetStdHandle WriteConsoleInput FILE_TYPE_PIPE
        xchg eax, ebx
        invoke SetLastError, 1
        mov ebx, hConin                ; Conin$ throws no errors for WriteConsoleInputA but no effect...
;        call GetStInfo
;        xchg eax, ebx
        mov edx, esp                ; buffer start
        push edx                ; create a slot for chars written
        invoke WriteConsoleInputA, ebx, edx, edi, esp        ; ml chokes without the A

Except for pipes... no effect. Googling didn't help, it seems most people use STD_INPUT_HANDLE successfully, but in my code it says invalid handle. Using CONIN$ does not throw an error, but it doesn't fill the buffer either. Any ideas?

Thanks, jj

qWord

maybe the problem is that you only push the keys (bKeyDown=1), but never release them?
MREAL macros - when you need floating point arithmetic while assembling!

nidud

#2
deleted

jj2007

Thanks, qWord. However, it seems not important. The code works perfectly in non-pipe mode.

WriteConsoleInputA returns invalid handle when used with GetStdHandle(STD_INPUT_HANDLE), but only in piped mode. The other console functions work just fine.

WriteConsoleInputA returns no error when used with CreateFile(CONIN$), and it works fine in non-pipe mode with that handle (which is different from std input handle). But the console input buffer doesn't get filled in pipe mode.

@nidud: No, the order is correct. hConin is obtained through CreateFile, not shown here. If I comment it out, WriteConsoleInputA uses std input handle and throws invalid handle error. Both work fine in non-pipe mode.

I attach the code and executables. Latest MasmBasic of today required if you want to assemble it.

qWord

MREAL macros - when you need floating point arithmetic while assembling!

jj2007

No, just CreatePipe. The normal case for passing handles to child processes.

qWord

Just for testing purpose, I would try it with releasing the keys...
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

No change, exactly as before...

Line 37ff of SendStringsOnDemand.asc:
@@:        dec ebx        ; write the prefill string to the console input buffer
        js @F
        movzx eax, byte ptr [ecx+ebx]        ; read backwards, start with Len-1

        inc edi        ; count the records
       
push 0        ; dwControlKeyState
       
push ax        ; UnicodeChar
        push ax        ; wVirtualKeyCode
        push 0        ; looks unprofessional but it works better without MapVirtualKey
        push 0        ; bKeyUp
        push KEY_EVENT

        inc edi        ; count the records
       
push 0        ; dwControlKeyState
       
push ax        ; UnicodeChar
        push ax        ; wVirtualKeyCode
        push 0        ; looks unprofessional but it works better without MapVirtualKey
        push 1        ; bKeyDown
        push KEY_EVENT
        jmp @B

qWord

can you make a short description how I can reproduce the described bug with the two EXEs you've upload.
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

SendStringsOnDemand.exe "standalone" shows
Enter x, Date or Time: >date
(the stuff before is debug info that I forgot to take out - new attachment below has switched that off)

When launched from CmdGUI_full.exe, it shows

D:\Masm32\CmdGUI>SendStringsOnDemand
..
Ready
Enter x, Date or Time: >[NO DATE HERE...]

The attachment above lacked the *.dat files, here is a complete one.

qWord

did you try it with SetConsoleMode->ENABLE_ECHO_INPUT for the pipe handle?
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

Good idea, I tried in various places but the answer is always "invalid handle"...

        invoke SetHandleInformation, hWriteOut, hflags, hflags         ; we need only hWriteOut
        invoke SetHandleInformation, hReadIn, hflags, hflags         ; and hReadIn
        invoke SetConsoleMode, hWriteOut, ENABLE_ECHO_INPUT
        deb 4, "SW2", eax, $Err$()
        invoke SetConsoleMode, hReadIn, ENABLE_ECHO_INPUT
        deb 4, "SW2", eax, $Err$()

qWord

Well, I can't compile your source (SendStringsOnDemand) with MASM. Also, when using jWasm, I get an executable that behaviors differently than the EXE you supplied - I get directly an error "invalid handle".
Also, I've seen some "strange" comments in your code:
LOCAL PipeBytes, buffer[8000]:BYTE ; 8102 works, 8104 crashes, slow for small buffers (e.g. 100 bytes) How should one interpret that?
MREAL macros - when you need floating point arithmetic while assembling!

sinsi

"push KEY_EVENT" is that pushing a word?

WORD  EventType;

qWord

Quote from: sinsi on January 03, 2013, 09:16:41 PM
"push KEY_EVENT" is that pushing a word?

WORD  EventType;
the union must be aligned to 4 (size of the greatest member that is not lager than 8 ) so it should be right.
MREAL macros - when you need floating point arithmetic while assembling!