Jump to content
Tuts 4 You

How to control CMD console?


LCF-AT

Recommended Posts

The language is relativ, only understand it

 

normally I agree, but when goal is to produce basic, asm code in as few lines as possible, IMHO anything besides asm/C is bad. C printf() takes just a few lines of asm, but c++ cout << compiles into 70+. Same thing (and worse) w/ C++ vectors vs. C arrays, C strings vs. (very) high level c++ strings, etc. i dont hate c++ (ok maybe I do :) ), but it's a big complex language for big complex projects, not just a few winapi calls. /rant

Link to comment

normally I agree, but when goal is to produce basic, asm code in as few lines as possible, IMHO anything besides asm/C is bad. C printf() takes just a few lines of asm, but c++ cout << compiles into 70+. Same thing (and worse) w/ C++ vectors vs. C arrays, C strings vs. (very) high level c++ strings, etc. i dont hate c++ (ok maybe I do :) ), but it's a big complex language for big complex projects, not just a few winapi calls. /rant

 

Well, obviously I disagree with you :D C++ can do what C can and more if needed (STL).

 

C++ printf() should make the same code as C printf(), there is no difference.

Link to comment

Hi guys,


 


so I think ASM (masm & winasm) is the easiest coder language or not?I thought so or are others easier?


 


@ raggy


 


Try again so I did delete some PMs now.


 


greetz


Link to comment

LCF you make all with Olly and multiasm and you can Asm .


 


So write in Asm :prop:  is easier for you


You can write in Masm or other Assembler without High Level ( If/else)

Edited by ragdog
Link to comment

Well, obviously I disagree with you :D C++ can do what C can and more if needed (STL).

 

C++ printf() should make the same code as C printf(), there is no difference.

 

but printf() isn't c++, it's part of libc. rewrite the code i wrote here earlier in pure c++ & watch how big it gets. "c/c++" is a very misleading terminology. also, printf() will not work w/all c++ compilers.

 

i agree, asm is easiest (but in practice its slow as hell to write large projects in 100% asm and high level stuff like structs, if/else, while, etc make development much faster w/out adding unnecssary instructions), like mentioned do what works best for u, masm, c, c++, html, whatever

Link to comment

so I think ASM (masm & winasm) is the easiest coder language or not?I thought so or are others easier?

If you think those are the best for you then you should keep using them :) An alternative might be PureBasic. See Teddy's great blog about it: https://forum.tuts4you.com/blog/27/entry-146-purebasic-adventures/ 

 

but printf() isn't c++, it's part of libc. rewrite the code i wrote here earlier in pure c++ & watch how big it gets. "c/c++" is a very misleading terminology. also, printf() will not work w/all c++ compilers.

In fact std::printf is part of the C++ standard library: http://en.cppreference.com/w/cpp/io/c/fprintf :)

This is pure c++:

#include <cstdio>int main(){    std::printf("Hello, world!\n");    return 0;}
or if you prefer:
#include <cstdio>using namespace std;// this will retrieve args from the pspint main(int argc, char *argv[]){  printf("%s %s %s\n", argv[0], argv[1], argv[2]);  fflush(stdout);  getchar();	  return 0;}
Edited by Mr. eXoDia
Link to comment

if u compile the c++ code w/ mingw g++, using std::printf() the exe will be 464kb exe . if u compile the same exact printf() C code w/mingw gcc, the exe will be 15.4 kb. both link to msvCrt.... c++ has junk that c doesn't...


 


and no, that isnt pure c++ because you're using Cstdio.h. traditional C++ uses << to write to console, not printf()... is ur point that c++ doesnt produce more junk?


 


fyi - i wasnt even talking about that program, i meant rewriting the masm code in pure c++ to see a whole bunch more unnecssary stuff... ; )


 


http://www.cplusplus.com/reference/cstdio/- <cstdio.h> (stdio.h) - C library to perform Input/Output operations


 


edit - http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c- nearly every answer will acknlowledge that printf() is C... and by pure c++ I mean not using anything from libc, instead of c strings w/pointers, use std::wstrings, instead of FILE*, file streams, instead of arrays, vectors, etc and the resulting compiled code will be much larger... hence why C/asm is the defacto official language of anything where performance is important (embedded, kernel drivers, etc)


Edited by simple
Link to comment

This topic is being digressed, I would say the best answers can be found in "The Art of Assembly Language" book :)


 




if u compile the c++ code w/ mingw g++, using std::printf() the exe will be 464kb exe . if u compile the same exact printf() C code w/mingw gcc, the exe will be 15.4 kb. both link to msvCrt.... c++ has junk that c doesn't...




The C++ junk you're talking about can be removed by changing some options before compiling


and almost all of these pieces of code, aka junk, are somewhat beneficial


 


@LCF


I advice u to read Iczelion's tutorial Series ... they r the best ever


and I always prefer to use RadAsm than WinAsm


Link to comment

if u compile the c++ code w/ mingw g++, using std::printf() the exe will be 464kb exe . if u compile the same exact printf() C code w/mingw gcc, the exe will be 15.4 kb. both link to msvCrt.... c++ has junk that c doesn't...

Congratulations, your compiler produces junk. My compiler generates two executables, the C one is 8496 bytes, the C++ one is 8536 bytes...

 

and no, that isnt pure c++ because you're using Cstdio.h. traditional C++ uses << to write to console, not printf()... is ur point that c++ doesnt produce more junk?

If you use std::cout you will probably get some overhead indeed, but that doesn't change anything about the fact that C++ is supposed to be backwards-compatible with C, which means <cstdio> is included in the C++ standard (making it part of C++).

 

fyi - i wasnt even talking about that program, i meant rewriting the masm code in pure c++ to see a whole bunch more unnecssary stuff... ; )

http://www.cplusplus.com/reference/cstdio/- <cstdio.h> (stdio.h) - C library to perform Input/Output operations

Yep, C library is still part of C++.

edit - http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c- nearly every answer will acknlowledge that printf() is C... and by pure c++ I mean not using anything from libc, instead of c strings w/pointers, use std::wstrings, instead of FILE*, file streams, instead of arrays, vectors, etc and the resulting compiled code will be much larger... hence why C/asm is the defacto official language of anything where performance is important (embedded, kernel drivers, etc)

Almost anything you can do with C you can do with C++. I write kernel drivers in C++, produces pretty much the same ASM, except that you can use constructors/destructors to automatically free memory/handles etc. Makes programming a lot safer ;)

If your point was that using <iostream> to output to the console generates overhead you are completely right (although you get other things back from it). Saying <cstdio> is not pure C++ is just incorrect, this is the only part I try to get clear.

Edited by Mr. eXoDia
Link to comment

Hi again,


 


yes I know the Iczelion tutorials but as I said its not really easy for me to understand them completely.Also the tuts are written into any black background (can't read very long on black or other dark colors) so can I change this anyhow to white background etc or do you know any re-write of this tutorials?


http://win32assembly.programminghorizon.com/tutorials.html


 


What is the different between RadAsm / WinASM or MASM or why is RadASM better etc?


 


So PureBasic dosen't look easy too. :) So I think ASM is already ok so far.So I am already happy that I can handle this language more or less.


 


Ok I got a another question:


----------------------------------------------------------------


So I have created a normaly proc with called by DialogBoxParam.Now the proc get always access if I press anything in the Dialog etc (all ok so far).The question I have now is....


 


What is if I wanna create for exsample a clock (like this 19:54:21) and put this into the dialog somewhere,so how do I now update this clock for each second?You know what I mean right?So the clock now gets only updated if I press anything on the dialog then it will access my proc and there it can access the code to update the clock again but if I don't press anything on dialog then clock get not updated etc you know what I mean right.So how to handle this problem?Have I to create any single thread for this and if yes do have a short ASM exsample?Sorry for asking again but I don't know it. :)


 


greetz


Link to comment

 

What is the different between RadAsm / WinASM or MASM or why is RadASM better etc?

 

Masm is only a compiler == Ml

 

Winasm /Radasm is only a Editor like Notepad or other editor

You can coding masm in Visual studio or Notepad (++) or other Text editor

 

Radasm ist better than Winasm? no is only a other Ide and support other function and languages.

But i find Radasm better

 

But that have i tell you 100x :prop:

Edited by ragdog
Link to comment

Ah ok thanks again for the remembering but you know I did forgot it again for 100x too. :) But I think this time it will working better to stay on the ball with WinASM and to keep some more in my head than anytime before.Maybe at the end of this year I could code almost good so that I also can create any more complex stuff (Maybe a Unpacker etc). ;)


 


greetz


Link to comment

So I have created a normaly proc with called by DialogBoxParam.Now the proc get always access if I press anything in the Dialog etc (all ok so far).The question I have now is....

 

What is if I wanna create for exsample a clock (like this 19:54:21) and put this into the dialog somewhere,so how do I now update this clock for each second?You know what I mean right?So the clock now gets only updated if I press anything on the dialog then it will access my proc and there it can access the code to update the clock again but if I don't press anything on dialog then clock get not updated etc you know what I mean right.So how to handle this problem?Have I to create any single thread for this and if yes do have a short ASM exsample?Sorry for asking again but I don't know it. :)

 

greetz

You should use CreateThread for this.

#include <windows.h>#include <commctrl.h>#include "resource.h"HINSTANCE hInst;static DWORD WINAPI TimeThread(void* Param){    HWND hwndLabel = (HWND)Param;    while(true) //infinite loop    {        SYSTEMTIME time;        GetLocalTime(&time);        char timeText[16]="";        wsprintfA(timeText, "%02d:%02d:%02d",                  time.wHour,                  time.wMinute,                  time.wSecond);        SetWindowText(hwndLabel, timeText);        Sleep(1000); //sleep one second    }    return 0;}static BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam){    switch(uMsg)    {    case WM_INITDIALOG:    {    }    return TRUE;    case WM_CLOSE:    {        EndDialog(hwndDlg, 0);    }    return TRUE;    case WM_COMMAND:    {        switch(LOWORD(wParam))        {        case IDC_BTN_START:        {            HWND hwndButton = GetDlgItem(hwndDlg,                                         IDC_BTN_START);            EnableWindow(hwndButton, FALSE);            HWND hwndLabel = GetDlgItem(hwndDlg,                                        IDC_LBL_TIME);            CreateThread(NULL, 0,                         TimeThread,                         hwndLabel,                         0, NULL);        }        break;        }    }    return TRUE;    }    return FALSE;}int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){    hInst=hInstance;    InitCommonControls();    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);}
See my attached example code (C++), you can see in olly how it work with assembler.

test_thread.zip

Edited by Mr. eXoDia
Link to comment

Hi again,


 


thanks for the exsample code also if its written in C but after some time I got it working now.Maybe not written very good but it works....



STIME SYSTEMTIME <> CLOCKTHREAD proto timeText db 40h dup (0)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CLOCK proc invoke GetDlgItem,HWNDPROC,1169
mov hwndLabel,eax
invoke CreateThread,0,0,addr CLOCKTHREAD,hwndLabel,0,0
Ret
CLOCK endp ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CLOCKTHREAD proc
LOOPA:
invoke GetLocalTime,addr STIME xor eax,eax
mov ax, [STIME.wSecond]
push eax
xor eax,eax
mov ax, [STIME.wMinute]
push eax
xor eax,eax
mov ax, [STIME.wHour]
push eax invoke wsprintf, addr timeText,chr$("%02d:%02d:%02d")
add esp, 0Ch
invoke SetWindowText,hwndLabel,addr timeText
invoke Sleep,1000
jmp LOOPA Ret
CLOCKTHREAD endp end main

Is ok so or?But I had to push the 3 times manually + adding 0C bytes too after wsprintf API.I tried to set the 3 time paramters on the wsprintf API but it will just push words insread of dwords etc you know.Also I couldn't use a while / endif command to make the infini loop (no idea why) and there I just used jmp label.The HWNDPROC I did copy before from the DlgProc main proc.If you know how to write this better or more un-complex then just tell me.


 


greetz


Link to comment

This is my code, but I'm not an expert in masm:

 

.486.model	flat, stdcalloption	casemap :none   ; case sensitiveinclude		base.inc.codestart:	invoke	GetModuleHandle, NULL	mov	hInstance, eax	invoke	DialogBoxParam, hInstance, 101, 0, ADDR DlgProc, 0	invoke	ExitProcess, eax; -----------------------------------------------------------------------SetCurTime proc hLabel : HWND	local timeString[16]:byte	local timeData:SYSTEMTIME	invoke GetLocalTime, addr timeData	movzx eax, timeData.wHour	movzx ecx, timeData.wMinute	movzx edx, timeData.wSecond			invoke wsprintf,addr timeString, addr szFormat, eax, ecx, edx	invoke SetWindowText,hLabel, addr timeString	RetSetCurTime EndPTimeThread proc hLabel : HWND	RepeatLoop:		invoke SetCurTime,hLabel		invoke Sleep,1000	jmp RepeatLoop	RetTimeThread EndPDlgProc	proc	hWin	:DWORD,		uMsg	:DWORD,		wParam	:DWORD,		lParam	:DWORD	.if	uMsg == WM_COMMAND		.if	wParam == IDC_BTN_START			;disable button			invoke GetDlgItem,hWin, IDC_BTN_START			invoke EnableWindow,eax,FALSE			;start label thread			invoke GetDlgItem,hWin, IDC_LBL_TIME			invoke CreateThread,NULL, 0, addr TimeThread, eax, 0, NULL		.endif	.elseif	uMsg == WM_CLOSE		invoke	EndDialog,hWin,0	.elseif uMsg == WM_INITDIALOG		invoke GetDlgItem, hWin, IDC_LBL_TIME		invoke SetCurTime,eax	.endif	xor	eax,eax	retDlgProc	endpend start
See attached for full.

Infinite loop with while:

.while eax == eax        ; code here.endw

threadstuff.rar

Edited by Mr. eXoDia
  • Like 1
Link to comment

lcfat - im not a masm expert either, but if u push args manually (w/out using invoke) then "call" manually, otherwise ml.exe (the compiler) will not adjust the stack pointer properly after call & who knows whatll happen. so either use invoke w/all arguments like mr x code or do it like this, but dont mix them



.while (1)
invoke GetLocalTime, addr Stime lea eax, [Format]
lea esi, [Lander]
movsx ebx, Stime.wSecond; or mov bx, Stime.wSecond
mov cx, Stime.wMinute
movsx edx, Stime.wHour
push ebx
push ecx
push edx
push eax
push esi
call crt_sprintf ; this is the same as - invoke crt_sprintf, esi, eax, edx, ecx, ebx
add esp, 14h ; or invoke crt_sprintf, addr Lander, addr Format, [Stime.wSecond], etc, etc
; esp is adjusted according to how many args. if u give invoke 2 args, stack pointer
; will be set to 2 args (8) when true value is 5 (14) push 1000
call Sleep etc etc etc .endw

mr x - here's what one of the last living creators of modern technology thinks of ur opinion of c++ ; ) - http://harmful.cat-v.org/software/c++/linus


  • Like 1
Link to comment

Hi guys,


 


thanks again for the example codes. :)


 


One question about the hWin....



invoke GetDlgItem,hWin, IDC_LBL_TIME

...so what is if I have this code in any other proc XY and not in same proc as DlgProc.At the moment I have to copy this hWin into any variable which I then use in my other proc.Is there also a other way to use any hWin etc from proc xy also in any other procs yx?


 


PS: Yes I have also added extra that stack values 0C of 3 pushes before but I also have changed this now and call all with the API. :)


 


One another thing: So the notepad.exe is in all Windows version in system folder to find right?So I mean you can call notepad.exe directly without path (same as all other files which are stored in system / 32 folder).So I made this short code what should work on all systems (XP and higher)



invoke GetStartupInfo,addr StartInfo
invoke CreateProcess,0,chr$("notepad.exe Logfile.txt"),NULL,NULL,TRUE,NULL,NULL,NULL,addr StartInfo,addr PI

Just using direct names without paths.Logfile is in same folder as my app and notepad (same name on all systems) I can call from anywhere not only from XP right.So then I don't need to use extra path datas.


 


Thanks


Link to comment

Hi guys,

 

thanks again for the example codes. :)

 

One question about the hWin....

invoke GetDlgItem,hWin, IDC_LBL_TIME
...so what is if I have this code in any other proc XY and not in same proc as DlgProc.At the moment I have to copy this hWin into any variable which I then use in my other proc.Is there also a other way to use any hWin etc from proc xy also in any other procs yx?

You can do this with a global variable. Put something like this:

hWinGlobal dd ?

One another thing: So the notepad.exe is in all Windows version in system folder to find right?So I mean you can call notepad.exe directly without path (same as all other files which are stored in system / 32 folder).So I made this short code what should work on all systems (XP and higher)

invoke GetStartupInfo,addr StartInfoinvoke CreateProcess,0,chr$("notepad.exe Logfile.txt"),NULL,NULL,TRUE,NULL,NULL,NULL,addr StartInfo,addr PI  
Just using direct names without paths.Logfile is in same folder as my app and notepad (same name on all systems) I can call from anywhere not only from XP right.So then I don't need to use extra path datas.

 

Thanks

Yes, it will work on other versions of windows too.

Link to comment

Hi again,


 


ok thanks again guys.


 


So how does hWinGlobal work?How to use / declare it?I mean I use a dialog resource and a main DLG proc "DlgProc proc hWnd:HWND, uMsg:HWND, wParam:WPARAM, lParam:LPARAM" and the hWnd is for this main Dialog which I just can use in this same proc only.How to make it global now?At the moment I have this...



DlgProc proc hWnd:HWND, uMsg:HWND, wParam:WPARAM, lParam:LPARAM
mov eax, hWnd
mov HWNDPROC, eax ; make a copy
.............
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CLOCK proc
invoke GetDlgItem,HWNDPROC,1169 ; use copy in other proc
mov hwndLabel,eax
invoke CreateThread,0,0,addr CLOCKTHREAD,hwndLabel,0,0
Ret
CLOCK endp

greetz


Link to comment

.data?

hMain dd ?

hwndLabel dd ?

.code

DlgProc proc hWnd:HWND, uMsg:HWND, wParam:WPARAM, lParam:LPARAM

push hWnd

pop hMain; make a copy

.............

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

CLOCK proc

invoke GetDlgItem,hMain,1169 ; use copy in other proc

mov hwndLabel,eax

invoke CreateThread,0,0,addr CLOCKTHREAD,hwndLabel,0,0

Ret

CLOCK endp

  • Like 1
Link to comment

 

so can I change this anyhow to white background etc or do you know any re-write of this tutorials?

Sure, you can do so by copying them into MS Word files (offline) or into Google Docs (online).

 

 

 

is RadASM better ?

A question of personal preference; however, I think that RadASM is so handy.

 

 

 

if u push args manually (w/out using invoke) then "call" manually

Why do we call them manually since we have invoke which cleans the stack automatically ?? It's even more readable !

Link to comment

Hi again,


 


today I wanna try the RadASM tool (found also a version on my HDD) but I have problems to find the resources editor.I see no visual Mode on/off button as I have in WInASM.So how to get a visual mode to see in RadASM?


 


greetz


Link to comment

Hi,


 


hmmm no visual mode!Why this?How to build then any dialogs etc? :) Ok if RadASM has no visual mode as WinASM then its nothing for me so I can't build my resources blind etc.


 


greetz


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...