News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Console IN

Started by bomz, September 01, 2010, 05:24:29 PM

Previous topic - Next topic

bomz

szConIn     DB "CONIN$",0

invoke CreateFile,addr szConIn,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0
   .IF eax!=INVALID_HANDLE_VALUE


no INVALID_HANDLE_VALUE but no result - copy CONIN

Yuri

CreateFile doesn't copy anything. It creates a handle to the console input buffer. This handle should be used with ReadConsole or ReadFile.

This code compiles and works OK for me:

.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\kernel32.lib

.data
    szConIn         DB "CONIN$",0
    szConOut        DB "CONOUT$",0
    szRequest       DB "Enter some text here: "
    szResult        DB "You entered: "
    hIn             DD 0
    hOut            DD 0
    BytesRead       DD 0
    BytesWritten    DD 0
    Buf             DB 100h DUP (0)

.code

start:
   invoke CreateFile, addr szConIn, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0
   mov hIn,eax
   invoke CreateFile, addr szConOut, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0
   mov hOut,eax
   invoke WriteConsole, hOut, addr szRequest, sizeof szRequest, addr BytesWritten, 0
   invoke ReadConsole, hIn, addr Buf, sizeof Buf, addr BytesRead, 0
   invoke WriteConsole, hOut, addr szResult, sizeof szResult, addr BytesWritten, 0
   invoke WriteConsole, hOut, addr Buf, BytesRead, addr BytesWritten, 0
   invoke ExitProcess,0
end start


bomz


bomz

not working - lenth=0
Quote
.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\user32.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

.data
szConIn     DB "CONIN$",0

.data?
Lenth dd ?
hMemory dd ?
pMemory dd ?
Buffer db 65536 dup(?)
String db 65536 dup(?)

hStdIn dd ?
;evEvents word ?
;evBuffer INPUT_RECORD <>

.code
start:

invoke CreateFile, addr szConIn, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0
mov hStdIn,eax
invoke ReadConsole, hStdIn, addr String, sizeof String, addr Lenth, 0
;   .IF Lenth!=0
   invoke OemToChar, addr String, addr Buffer
   dec Lenth
   invoke GlobalAlloc,LMEM_MOVEABLE,Lenth
   mov  hMemory,eax
   invoke GlobalLock,hMemory
   mov pMemory,eax
   invoke lstrcpy,pMemory,addr Buffer
   invoke GlobalUnlock,hMemory
   invoke OpenClipboard,NULL
   invoke EmptyClipboard
   invoke SetClipboardData,CF_TEXT,hMemory
   invoke CloseClipboard
   invoke GlobalFree,hMemory
;   .ENDIF
   invoke ExitProcess,0
end start

bomz

it's mean that STD_INPUT_HANDLE and CONIN$ is different. But How change console buffer?
and differ echo off | MYPROG.EXE

Yuri

Why do you want to change it? What for?

bomz

to know how it's changed.

to get through STDIN more than 4 kb

redskull

"CONIN$ gets a handle to the console input buffer, even if the SetStdHandle function redirects the standard input handle. To get the standard input handle, use the GetStdHandle function."

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

bomz

but how change buffer size before program starts?

redskull

You don't; change the number of character you want to read.  Reduce the amount of events you are reading (from 65535 down to about 100), and then put it into a loop, ending when characters read != sizeof buffer.  If you want input piped to you, you must use ReadFile and not ReadConsole, and the STD_INPUT_HANDLE will work fine

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

Yuri

Here is an example of reading in a loop. Hope I didn't confuse MASM syntax.  ::)

.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\kernel32.lib

.data
    hIn             DD 0
    hOut            DD 0
    BytesRead       DD 0
    BytesWritten    DD 0
    Buf             DB 100h DUP (0)

.code

start:
    invoke GetStdHandle, STD_INPUT_HANDLE
    mov hIn,eax
    invoke GetStdHandle, STD_OUTPUT_HANDLE
    mov hOut,eax
    @@:
        invoke ReadFile, hIn, addr Buf, sizeof Buf, addr BytesRead, NULL
        test eax,eax
        jz @F
            invoke WriteFile, hOut, addr Buf, BytesRead, addr BytesWritten, NULL
    jmp @B
        @@:
    ret   
end start

redskull

Depending on the goal, you can leave the buffer itself sized at FFFF, but read characters into it a few hundred at a time (adjusting a pointer after each read).  That way, after the loop, you still have one big contigious array of all the data to do with what you please.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

bomz


bomz

trying with 1.5 mb , and how to know the size of data in STDIN???

redskull

GetNamedPipeInfo() will tell you the actual size; A quick test on my system shows that a console pipe buffer has a size of 00001000h (4K), for both the input and output buffers.  If you really want to be slick, use that function to get the size, and read that many bytes at a time in your loop for max effeciency.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government