Jump to content
Tuts 4 You

How to control CMD console?


LCF-AT

Recommended Posts

Hello Kurapica,


 


thanks for the link but I don't think that this is the right thing what I am looking for.Don't wanna use exceptions for this. :) First I have the little problem to find a simple way to read any text files line by line.The other problem I have is how to create a own text file to store information inside (problem is how read it again without to make a fault).Normaly if I wanna create any settings file to store infos about the app like size / choosen options etc its no problem if I use Get & Write PrivateProfileString API so this is easy to use but the problem is if I wanna store TONS of information like entrys for listviews then I can just write them line by line into a file.Lets say I have a LV with "one | two | threee" entrys in one line then I need to write 3 lines for one line into file.



First | Second | Third

First


Second


Third


 


Something like this.Problem now is if the user does change this file then it dosen't work anymore if I do check each line.Thats the problem so I have to keep the file untouched and this way is not so safe and to static.I would like to use / find any better way but how is the question.So I think I can't create a label for entry block where I could then use Get/WritePrivateProfileString API.Maybe I could do this but then I always need to create dynamic app / keynames or something like this you know.Maybe this would be better so then the user could also change the file without to get trouble later during reading the file again.


 


greetz


Link to comment

Hi again,

 

short new question.Does anybody know any simple way to read a text file line by line?Any API or command message or macro I could use instead to check each time manually for 0D0A hex bytes?

 

greetz

 

Guttentag LCF, here's how to do it w/fgets() libc API. Sorry this is x64 nasm I got tired of x86 masm. It's nearly the same thing though. it reads each individual line of a file into a preallocated, r/w buffer

 

I originally posted getline() version, only to find it's linux only, so fgets works on windows.

extern printf

extern fopen

extern fclose

extern exit

extern getline

extern free

extern malloc

extern fgets

section .bss

section .data

DisplayLine: db "%s", 10, 0

DisplayLen: db "Lines is %i bytes long", 10, 0

CharPointerLine dq 1024

Size dq 0

FileToOpen db "/home/coder/Desktop/lines.c",0

Permissions db "r", 0

section .text

global main

main:

push rbp

mov rbp, rsp

sub rsp, 16

;mov qword [rbp - 24], 0

mov edx, Permissions

mov eax, FileToOpen

mov rsi, rdx

mov rdi, rax

call fopen ; open file

mov qword [rbp - 16], rax

cmp qword [rbp - 16],0 ; rbp - 16 = FILE*

jnz .no3xit

mov edi, 1

call exit

.no3xit:

mov qword [rbp - 8], 512

mov rax, qword [rbp - 8]

mov rdi, rax

call malloc

mov [CharPointerLine], rax

jmp .b3ginl00p

.pr1nt:

mov rax, qword [CharPointerLine]

mov rdi, rax

mov eax, 0

call printf

.b3ginl00p:

mov rax, qword [rbp -8]

mov ecx, eax

mov rax, [CharPointerLine]

mov rdx, qword [rbp -16]

mov esi, ecx

mov rdi, rax

call fgets ; fgets will behave similar to getline(), here it will return a

; everything up to the \n

test rax, rax

jnz .pr1nt

mov rax, [CharPointerLine]

test rax, rax

jz .r3turn

mov rax, [CharPointerLine]

mov rdi, rax

call free

.r3turn:

mov eax,0

leave

ret

Been busy the past weeks, but I've seen some of your mentioned problems before...

 

1. About NM_CLICK while subclassing - MS doesn't say so but this is a bug on their part.  You can filter the main message que like you mentioned, or you can also use something like SetGlobalHook(WH_GETMESSAGE), which will filter all messages on all processes, then you can manually do the geometry to calculate exactly where each MSG occured. MS claims to have fixed this w/something called "Message Relfection" in MFC... who knows...

 

2. About your scroll problems, there's several different ways to adjust scrollbars. Don't remember off the top of my head but at least 2 or 3 more functions to manipulate scrollbars if you search

 

3. I was unable to replicate the problem described in post #148, it looks like something easy though.

Edited by simple
Link to comment

Good day too simple. :)


 


Thanks for the code but also this looks a little much and dosen't work in WinASM (need to change almost all lines manually etc you know what I mean).


 


About subclassing: So I did started already a discussion on other coder forum about it + other comming problem with subclassing (API / manifest trouble etc) and the people there couldn't help me to find a solution and I was already on that point to give up but then ragdog did send me a very simple solution how I can do it and its working.So it seems that I only need to fake a WM_NOTIFY command at WM_NOTIFY check in main routine and sending this fake command into my SCs. :) So I couldn't almost believe it but its working 1A and the other problem with APIs / manifest etc are also gone.Very simple solution but also very effective.I was very happy that this problem was solved now and gone from my problem list.Now I send a fake (custom command for WM_Notify) and till now I got no problems with that and I can check NM_CLICK and all others etc in SC.Yes I have also seen in other sources then you could also use GETMESSAGE API / Tranlate API etc which get access in loop but I don't use this code parts in my sources so I do prefer the other basic parts which you can find in the bone / dialog template.


 


About scrollbars: So the basic scrollbar you get in Tabconrtol are just 2 arrow buttons very close together like this (<| |>).So you just have the buttons without any bar which you could move to left / right side but this is bad if you have tons of tabs and then you only can keep pressed the button on it to scroll.The same problem you can also find in the MultiASM Olly plugin too and its not very handy to find something in my almost 1000 tabs I got.The only thing what I could and have done is to create a another scrollbar control which I did connect with the tabcontrol and now I can move it very quick as I wanted. :) Was not so easy for me to write the code for this part but after a while I got it and its working ok.


 


About problem post 148: Problem was again subclassing and using SendMessage API + my SC proc.On first view it was working but then I couldn't move the lines anymore what you can see on the pictures I attached.If I use PostMessage API or any other one then it was working again but then I got other problems on else parts.So I did spend very much time and found much problems if I had used this method and other methods so I was getting almost crazy.But anyway and as I told before,after using the fake method all was working again so that I can think that this method is a good and working one.


 


By the way I have another small question so I found a little problem today about my Listviews.Normaly if I enter something into any line then it popup a dialogbox with some edit controls where I can enter something and now I see that the editcontrols are limited with 256 bytes only.Hhmmmm,problem now is that I need to enter MORE! :) In notepad I can see that you can enter 1024 bytes per line.Now I am not sure whether only my used buffer was to low (I think so or).But also if I use 1024 buffers then I get the problem with saving the line later.Lets say I do write something before on same line then it will use 2 entire lines instead of one and if I later use the file again line by line then it will get a problem.You know what I mean right?So I think I need to find any smarter solution to read / save the informations but what could be a smart solution is the question.I think I need to think again about it and should remove my actually line by line read / save code so this should be not safe enough.


 


Thanks again and till later.


 


greetz


Link to comment

I think 95% of your problems can be solved by moving to a bit higher language like C instead of asm.


 


File manipulation and user interface can be a real nightmare in asm.


 


you can save much time if you move to C for example and it won't make such a noticeable difference if you are worried about performance.


Link to comment

Hi,


 


so I hear this almost the entire time to switch from ASM to C. :) At the end I decided already to code in ASM also if I need to handle everything by myself.Maybe later if I got more advanced with ASM coding so I think its also a good practice to find & handle a lot of problem situation manually.


 


greetz


Link to comment

Thanks for the code but also this looks a little much and dosen't work in WinASM (need to change almost all lines manually etc you know what I mean).

 

I should have known ; ) :sorry:


; ml /c /Coff /cl target.asm

; link /subsystem:console target.obj

.486p

.model flat, stdcall

option casemap: none

include \masm32\include\msvcrt.inc

includelib \masm32\lib\msvcrt.lib

.data

FileName db "C:\target.asm", 0

Permissions db "r", 0

.data?

.code

start proc

local FilePointer:DWORD

local CharPointer:DWORD

local AllocSize:DWORD

local LoopCheck:DWORD

invoke crt_fopen, addr FileName, addr Permissions

mov FilePointer, eax

.if (FilePointer == 0)

jmp f1n1sh

.endif

mov AllocSize, 512 ; if u need manage lines > 512 bytes then change this

invoke crt_malloc, AllocSize

mov CharPointer, eax

mov LoopCheck, 1

.while (LoopCheck != 0)

invoke crt_fgets, CharPointer, AllocSize, FilePointer

mov LoopCheck, eax

invoke crt_printf, CharPointer

.endw

invoke crt_free, CharPointer

invoke crt_fclose, FilePointer

f1n1sh:

mov eax, 0

leave

retn

start endp

end start

Glad to hear u got it done, unfortunatley hacks like that are often normal in winapi GUI coding.

 

About the edit controls, look into EM_SETLIMITTEXT and you can change that. U can definately get a lot more than 256 bytes.

 

Not sure I understand ur problem, this may/may not help u but generally when u deal w/large ammounts of dynamic changing data u use functions like malloc, calloc, etc and store each value as an element in an array

Link to comment

Hi again simple,


 


thanks for the new ASM code now it works. :) Ok I can get now line by line + 0A byte at end.Seems that I also need to check this too then later.But not bad so far so thanks for this code example.


 


Yes you're right so I just set the buffer higher I did used in my struct to 1024 so before I used 256 but wasn't enough.


 


About storing values.So I played a little bit more with Get/WritePrivateProfileString API and I think I get it working to create dynamic key / section values.



[Tab=1]
Tabname=Tab 1
L1=456
L2=456
[Tab=2]
Tabname=Test Tab
L1=AAA
L2=BBB
L3=
L4=12312
L5=234
L6=00

Just using counters for Tab=X key and LX and adding them and first L = 2. row entry and second L = 3. entry.Each line = 2 entrys to store so the first I use just as counter for the lines.Seems to work and should be better now than before so now I can also move some lines above or higher without to get any trouble so I don't need to read each line in static mode.


 


Thanks again so far and till later.


 


greetz


Link to comment
  • 4 weeks later...

Hi guys its me again,


 


so now I have just a very simple question but I can't find a solution at the moment and now I stuck during coding. :) Ok so the problem is that I can't find a way (don't remeber now) how to get the handle of a other / another Dialogbox which was not shown yet.You know what I mean right?Problem is I have created more Dialogboxes with stuff inside (edits etc) and now I don't get the handle of ID I created into this DBox like these edits and for using API GetDlgItem I need the handle of the Dialogbox where the edits belong to.So is there a simple quick way to get the handle of this DBox which was not access?


 


Sorry for asking again for such a zero question but I don't remember now and find also nothing simple on internet quickly.So if you read this and you know the answer then reply quick if possible. :)


 


Thank you


Link to comment

Hi again,


 


so I think its not possible to get the actually handle of any IDs if the IDs / resources etc do belong to any Dialog which was not init (WM_INITDIALOG) yet.Also if I got the handle of Dialog its not working.Ok maybe it was again my fault to think it would be possible.Anyway and sorry for asking again.


 


greetz


Link to comment

I dont know what u r trying to do but u can get handles to controls even if it's in another process. As long as it was created w/an ID - u can enumerate it's handle. It doesn't have to be vreated in INITDIALOG, WM_CREATE, etc.


Link to comment

Sub-classing a control in an other process will require using CreateRemoteThread or injecting your own DLL into that process.


 


an application like "ShowWin" does that to enumerate the properties of controls in other processes.

Link to comment

Hi again,


 


no no so I wanted just do some simple thing.I wanted only to get the handles of some edit controls of a another dialogbox I made which was not init yet and there I wanted to fill some paths inside of my ini file.Thats all already.But now I have seen thats not possible to do this if the dialogbox isn't init / called yet so I tried it directly in Olly with right handle of 2. dialogbox but I get no handle of the controls which does belong to this DBox so its just working after WM_INITDIALOG to get the handles of controls.So I think it was just a stupid idea of me,no idea why I thought it would be possible. :)


 


greetz


Link to comment

Hi again,


 


short other quick question.How was the method to change the text color of normal buttons?I tried to catch WM_CTLCOLORBTN and use SetTextColor API but isn't working.Do you know it?


 


greetz


 


Link to comment

For WM_CTLCOLORBTN you have to draw the control yourself. If you search around there's a lot of different codes to get that effect though w/out so much overhead.


Link to comment

So my goal was it only to change the button text color without to change anything about the button itself you know.Normal text color of buttons are black and I wanted some in other text colors without big coding for this.I thought it would be possible to handle this on a simple way etc.


 


greetz


Link to comment

Well those are normal gdi, not gdi+, they're very different. Those codes are bad too.


 


I'd recomend doing a screenshot of the button, loading it in mspaint, manually changing the text colors to what you want, save it as a bitmap. This will be your button.


 


Put the bitmap in your .rc file,  use BS_BITMAP flag in your CreateWindow("BUTTON") call, then use LoadImage() or LoadBitmap(), then SendMessage(BM_SETIMAGE). Works for me.


Link to comment

Hi again,


 


ok setting a bitmap would work but its again more work to create any special bitmaps for the buttons but as alternative method its ok. :) Thanks again.


 


I have another simple question.Now I wanna set a limit of my main target dialog of width & height so that lower sizes are not allowed to set in realtime.I want to prevent that some controls does overlap each other.So how do I set & check this?



Minimum should be....
370 witdh (dec)
445 height (dec)

If this limit gets below durring moving then the window should block any lower sizes (you know what I mean).How to do this?


 


Thanks


Link to comment

Ok thanks again simple it works so far on my first tests only to use WindowSize function.One question about that function.So I just wanna limit the min sizes but not the max sizes (they can be so large as user want or till max screen resolution) so what do I enter there if I wanna set no limit for max?I can enter 99999 for exsample.....so I forgot again the API to get used screen resolution to enter the right actually max W & H or is it no problem to choose any high value as 999999?Just wanna prevent possible problems later thats why I am asking again.


 


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...