flat assembler
Message board for the users of flat assembler.

Index > Windows > Console Programming with Win32 API

Author
Thread Post new topic Reply to topic
MattBro



Joined: 08 Nov 2003
Posts: 37
MattBro 17 May 2004, 02:36
It is supposedly possible to launch and control a console window from a windows application. Within the API are the functions

invoke CreateConsoleScreenBuffer, GENERIC_WRITE, dword 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL


followed by

invoke SetConsoleActiveScreenBuffer, eax

I have not been able to get these functions to work, or any other console functions (WriteConsole etc.) to work, however, when I use the

format PE GUI 4.0

directive. It seems console apis only apply when I use the directive
format PE console

The application here is when one wants to use a console window for simple text io while doing other GUI chores.


UPDATE: (I answered my own question)
I finally figured out that I need to use the routines AllocConsole and FreeConsole to get the job done. Apparently some kind of GUI or event driven loop needs to be launched as well or else the application just quits. Ill just post my ignorance to this forum in case someone else might be helped by this Windows API question.

Regards,

Matt B.

Embarassed

_________________
-- -------------------------------------------------------
"I am the Way and the Truth and the Light, no one comes to the Father except through me" - Jesus
---------------------------------------------------------
Post 17 May 2004, 02:36
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 17 May 2004, 08:43
Could you give us some code, perhaps somebody (like me :$) didn't get it working following all your hints.
Thanks
Post 17 May 2004, 08:43
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
inskipp



Joined: 23 Jun 2003
Posts: 25
Location: Poland
inskipp 17 May 2004, 14:30
I wrote this small piece of code some time ago, it may be helpful.
Code:
format PE console 4.0
entry start

include '%fasminc%/win32a.inc'

section '.code' code readable executable
start:   
    invoke  CreateConsoleScreenBuffer,GENERIC_WRITE,0,0,1,0
     mov     [hCSB],eax
  inc     eax
 jz      exit            ;if eax=-1 error
    invoke  SetConsoleActiveScreenBuffer,[hCSB]
 
    invoke  FillConsoleOutputCharacter,\
               [hCSB],'Q',\
             80*25,0,Temp

    invoke  Sleep,4000
  invoke  CloseHandle,[hCSB]
exit: 
    invoke  ExitProcess,0

section '.data' data readable writeable

hCSB      dd ?
Temp    dd ?

;include 'csb.imp'
section '.idata' import data readable writeable

  library kernel32,'kernel32.dll'

  import kernel32,\
         CloseHandle,'CloseHandle',\
         CreateConsoleScreenBuffer,'CreateConsoleScreenBuffer',\
         ExitProcess,'ExitProcess',\
         FillConsoleOutputCharacter,'FillConsoleOutputCharacterA',\
         SetConsoleActiveScreenBuffer,'SetConsoleActiveScreenBuffer',\
         Sleep,'Sleep'
    
Post 17 May 2004, 14:30
View user's profile Send private message ICQ Number Reply with quote
MattBro



Joined: 08 Nov 2003
Posts: 37
MattBro 23 Sep 2004, 18:06
OK sorry it took so long to get back to you. Here is an example of a console running from within a normal windows programming context. ie you don't need to use format PE console directives.

Code:

; An example of running a console without compiling in console mode

include '%fasminc%\win32ax.inc'

entry start
; This entry is missing from the windows includes
CONSOLE_TEXTMODE_BUFFER = 1


section '.data' data readable writeable
inHandle dd ? ; DWORD: handle to standard input device
outHandle dd ? ; DWORD handle to standard output device
bytesWritten dd ? ; DWORD: number of bytes written
cons_buflen dd 0 ; length of string in input buffer
cons_inbuff db 0 ; start of buffer
times 1023 db 0
hello  db 'Greetings Earthling ! Do not be alarmed;'
       db  'your death will be painless',10,13
       db 'Enter <cntrl>q to quit',10,13,0
newline db 10,13,0
 _t777:
 times 100 db 0
_gencap db 'MacroFlow',0


section '.code' code readable executable


start:
;
; allocate a console, get its handle and write to it
invoke AllocConsole
invoke GetStdHandle, dword STD_OUTPUT_HANDLE
; invoke CreateConsoleScreenBuffer, GENERIC_WRITE, dword 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL
; Put the address of the handle in ; our variable
; call apifail
mov [outHandle],eax
invoke GetStdHandle, dword STD_INPUT_HANDLE
mov [inHandle], eax
; make the screen buffer 'active'
invoke SetConsoleActiveScreenBuffer, eax
mov eax, hello
call Write2Cons
; debug  step for processing commands, load stack with stuff
;  dpush 7
MFCons.top:
   call ReadCons
   xor edx, edx
   mov dl, byte [eax]
   cmp dl, 11H        ; control Q check
   je MFCons.done

   call Write2Cons

jmp MFCons.top
MFCons.done:

invoke FreeConsole
jmp goodbye



; ( -- strlen)
proc StrLength, StringBuff
; get length of string StringBuff
; StringBuff ; pointer to string
;
; StringBuff must be null terminated
; incrementing eax. The value in eax is the string length
;
; returns: eax = length of string StringBuff
enter
mov edi, [StringBuff]
; dpush 0 ; character count
mov eax,0 ; character count
; Aprintf 'finding strlen of %s ', dword [ptrString]
StrLength.top: ; top of loop
cmp [edi], byte 0 ; check for string null termination
je StrLength.done ; if null, jump out of loop and return
inc edi ; pointer to next byte
inc eax ; increment counterc
jmp StrLength.top ; back to top of loop
; jump here to return
StrLength.done:
return
endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Writes a null terminated ASCII string to the console
; String is in (eax)
Write2Cons:
;
; Writes a null-terminated string in bstring to the console window
mov edx, eax
; ()
stdcall StrLength,edx ; return length of string in eax
cld ; clear the direction flag
; must do this before WriteConsole
; consHandle ; console output handle
; edx: points to string
; eax string length
; bytesWritten: returns number of bytes written (ptr)
; Aprintf 'consHandle: %x strlen: %d hello: %s',dword [consHandle], eax, edx
invoke WriteConsole, dword [outHandle], edx, eax, bytesWritten, 0
; Aprintf 'bytes written: %d', dword [bytesWritten]
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Read text from the console (up to 256 bytes)
; The output is a MacroFlow style string
; in eax
ReadCons:
mov eax, 0
invoke ReadConsole, dword [inHandle], cons_inbuff, dword 1023,cons_buflen, 0
; add an extra null termination
mov eax, dword [cons_buflen]
; (buflen)
mov [cons_inbuff + eax], byte 0
; add overhead size to length parameter
; should be equal to tmpa.hsiz defined in mf4.inc
add eax, 6
mov [cons_buflen], eax
mov eax, cons_inbuff
ret

goodbye:
invoke ExitProcess, 0


section '.imp' import data readable writeable

 library kernel32,'KERNEL32.DLL',\
     user,'USER32.DLL'

 import kernel32,\
      AllocConsole,'AllocConsole',\
    CreateConsoleScreenBuffer,'CreateConsoleScreenBuffer',\
  FreeConsole, 'FreeConsole',\
     GetStdHandle,'GetStdHandle',\
    WriteConsole, 'WriteConsoleA',\
  ReadConsole, 'ReadConsoleA',\
    ExitProcess, 'ExitProcess',\
     SetConsoleActiveScreenBuffer, 'SetConsoleActiveScreenBuffer'

import user,\
        MessageBox,'MessageBoxA',\
       wsprintf,'wsprintfA'


    


The program reads what you typed and then echos it back to the console.
A good starting point for generating console interactive io from a regular windows program.

Regards,
-Matt
Post 23 Sep 2004, 18:06
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex 23 Sep 2004, 19:57
Hutch's masm32 package provides very usefull functions for console applications. The simple example below prints 10 times a string.
Code:
format PE CONSOLE 4.0
entry start

Include '%fasminc%\win32a.inc'
Include '%fasminc%\macro\if.inc'
Include 'Struct.inc'

section '.data' data readable writeable

msg       db 'Console application',0

section '.code' code readable executable
start:
        call    ClearScreen
 push    esi
 xor     esi,esi
@@:
  inc     esi
 stdcall locate,esi,esi
      stdcall StdOut,msg
  cmp     esi,10
      jne     @b
  pop     esi     
    invoke  ExitProcess,0

include 'Locate.asm'
include 'Clearscr.asm'
include 'Strlen.asm'
include 'Stdout.asm'

section '.idata' import data readable writeable

  library kernel32,'kernel32.dll'

  import kernel32,\
         ExitProcess,'ExitProcess',\
         FillConsoleOutputCharacter,'FillConsoleOutputCharacterA',\
         GetConsoleScreenBufferInfo,'GetConsoleScreenBufferInfo',\
         GetStdHandle,'GetStdHandle',\
         SetConsoleCursorPosition,'SetConsoleCursorPosition',\
         WriteFile,'WriteFile'

    


Description:
Download
Filename: Console.zip
Filesize: 3.03 KB
Downloaded: 408 Time(s)


_________________
Code it... That's all...
Post 23 Sep 2004, 19:57
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.