Jump to content
Tuts 4 You

Writefile


starzboy

Recommended Posts

Posted

hello

i am making a stupid log file ... but i am facing problems...

.data

filename db "log.log",0

.data?

hdir dd ?

htemp dd ?

buffer db 512 dup(?)

buffer1 db 512 dup(?)

i was making a log file .. kindof

invoke GetCurrentDirectory,sizeof hdir ,addr hdir

invoke lstrcat,addr hdir,offset filename

invoke CreateFile,addr hdir,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0

push eax

invoke WriteFile,eax,addr buffer,sizeof buffer,addr htemp,0

pop eax

invoke WriteFile,eax,addr buffer,sizeof buffer1,addr htemp,0

pop eax

invoke CloseHandle,eax

i want buffer1 to write on next line ... but i am not able to do so ...

hope to get some help

ty

Posted

You need to write newline character after first line then. In windows thats 0x0A,0x0D

so make a variable

nl db 0Ah,0Dh

and write those 2 bytes whenever you need new line.

Posted

you mean ....

push eax

invoke WriteFile,eax,addr buffer,sizeof buffer,addr htemp,0

pop eax

invoke WriteFile,eax,addr nl,sizeof nl,addr htemp,0

pop eax

invoke WriteFile,eax,addr buffer1,sizeof buffer1,addr htemp,0

pop eax

if so ... not work

Posted

First of all, don't use sizeof for buffers. Use it ONLY for primitive data types. Those are byte, word, dword etc (depends on programming language).

I'm don't write stuff in asm so I don't know what sizeof will return in this case, but I'm guessing 1. In this case just hardcode length for newline. Put 2 there. If you read strings into buffer, don't write sizeof buffer, but get length of a string and write length bytes to file. If it's binary buffer this is ok, but since you're setting new lines I guess these are string variables.

Posted

u could do something like this:

add in .data

szNextLine db 0Dh,0Ah,00h

the code should be this:

invoke CreateFile, addr hdir, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
push eax
invoke WriteFile, eax, addr buffer, sizeof buffer, addr htemp, 0
pop eax
invoke WriteFile, eax, addr szNextLine, 2, addr htemp, 0
pop eax
invoke WriteFile, eax, addr buffer, sizeof buffer1, addr htemp, 0
pop eax
invoke CloseHandle, eax

;)

Posted (edited)

I can't imagine how much you've corrupted the stack by popping eax three times after pushing it only once, TBH i'm astounded you didn't get any kind of fatal error. However, excluding that mistake, Ox87k is correct. Though, if you did a bit of searching, you would also find that there are several write-line type functions for masm, or you could use the crt.

As an afterthought, you can alleviate the need for calling GetCurrentDirectory by specifying only the filename in CreateFile, it will assume you want the file created in the cwd, see here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/creating_and_opening_files.asp

Ehtyar.

Edited by Ehtyar
Posted (edited)
.data
filename db "log.log",0
szNextLine db 0Dh,0Ah,00h.data?
buffer db 512 dup(?)
buffer1 db 512 dup(?)
hFileWr DWORD ?invoke CreateFile, ADDR filename, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
mov [hFileWr], eax
invoke lstrlen, ADDR buffer
invoke WriteFile, [hFileWr], ADDR buffer, eax, ADDR htemp, 0
invoke WriteFile, [hFileWr], ADDR szNextLine, 2, ADDR htemp, 0
invoke lstrlen, ADDR buffer1
invoke WriteFile, [hFileWr], ADDR buffer1, eax, ADDR htemp, 0
invoke CloseHandle, [hFileWr]

maybe now is more clean :P

Edited by Ox87k
Posted

just add the new line bytes to the buffer

CODE

.data

filename db "log.log",0

szNextLine db 0Dh,0Ah,00h

.data?

buffer db 512 dup(?)

buffer1 db 512 dup(?)

bWrite db 1024 dup(?)

hFileWr DWORD ?

hFileSize DWORD ?

invoke lstrcpy,addr bWrite,addr buffer

invoke lstrcat,addr bWrite,addr szNextLine

invoke lstrcat,addr bWrite,addr buffer1

invoke lstrlen,addr bWrite

mov hFileSize,eax

invoke CreateFile,ADDR buffer,GENERIC_READ or GENERIC_WRITE ,\

FILE_SHARE_READ or FILE_SHARE_WRITE,\

NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL

mov hFileWr,eax

mov eax,hFileSize

invoke WriteFile,hFileWr,bWrite,eax,ADDR htemp,NULL

invoke CloseHandle,hFileWr

Posted

@Ehtyar ...

nah bro ... it was a mistype in that post ...

btw Napalm/iCu finall helped me out with it ...

mov eax, offset buffer0
mov word ptr [eax], 0A0Dh
add eax, 2
invoke GetDlgItemText, hwnd, 10036, eax, 509 ; copy to buffer
mov eax, offset buffer1
mov word ptr [eax], 0A0Dh
add eax, 2
invoke GetDlgItemText, hwnd, 10040, eax, 509 ; copy to buffer
invoke GetCurrentDirectory, sizeof hdir, addr hdir ; current dir
invoke lstrcat, addr hdir, offset filename ; cat filename on
invoke CreateFile, addr hdir, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0
push eax
push eax
push eax
mov ebx, eax
invoke lstrlen, addr buffer0
invoke WriteFile, ebx, addr registrationdata,sizeof registrationdata, addr ddTemp, 0; Write to file
pop ebx
invoke lstrlen, addr buffer0
invoke WriteFile, ebx, addr buffer0, eax, addr ddTemp, 0; Write to file
pop ebx
invoke lstrlen, addr buffer1
invoke WriteFile, ebx, addr buffer1, eax, addr ddTemp, 0; Write to file
pop ebx
invoke CloseHandle, ebx; close handle
  • 3 months later...
Posted
.data
filename db "log.log",0
szNextLine db 0Dh,0Ah,00h.data?
buffer db 512 dup(?)
buffer1 db 512 dup(?)
hFileWr DWORD ?invoke CreateFile, ADDR filename, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
mov [hFileWr], eax
invoke lstrlen, ADDR buffer
invoke WriteFile, [hFileWr], ADDR buffer, eax, ADDR htemp, 0
invoke WriteFile, [hFileWr], ADDR szNextLine, 2, ADDR htemp, 0
invoke lstrlen, ADDR buffer1
invoke WriteFile, [hFileWr], ADDR buffer1, eax, ADDR htemp, 0
invoke CloseHandle, [hFileWr]

maybe now is more clean :P

I try to compile this code and i get errors :S

error A2133: register value overwritten by INVOKE

error A2133: register value overwritten by INVOKE

im not sure why and do not understand this

I added all the buffers and data

and narrowed it down to 2 unsolvable errors

Posted

It may happen..So it's always better to use registers which are not usually changed with calls like ebx or esi,edi

So save the return value from "invoke lstrlen, ADDR buffer" to ebx and then use it and similar for others where you get errors

Posted (edited)

ok I think i may have it now

htemp was not defined anywhere in the example

so I assumed it was a LOCAL

when I add it to .data? it works

you need to add this to the .data? to compile correctly

htemp DWORD ?

now i will try to finish the code and dump information from a dialog item to the log :D

EDIT:

I got it working now it read information from my dialog item and dumps it to a log file like i wanted

but im having problems with my dialog items

I want it to read each line from the dialog Item and insert it to another dialog in the same format

once it is in the second dialog i save it to a file

but i cant get it to move down a line

instead of;

Line1

Line2

Line3

I get;

Line1Line2Line3

I know i will have to use 0Ah,0Dh to define a new line but im not sure how I would do that dynamically

I mean i need to read dialog item 1 then add the number of lines found to buffer copy data to buffer2

and from there add to dialog item 2

but i cant figure out how to read line by line and set dialog text line by line

I also cant seem to clear buffers correctly I try
invoke SetDlgItemText,hWnd,buffer,NULL
but it doesnt work
I think its because the buffer is not a dialog item
is there any way to null the buffer after im done with it?

nvm the clear buffer thing I solved it I use this code to clear out the buffer

push eax

push ecx

push edi

xor eax, eax

mov ecx, 512/4

mov edi, offset buffer

rep stosd

pop edi

pop ecx

pop eax

works great for me just a little lengthy

any help would be much appreciated

Edited by shot

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