Hi,

I'm trying to make a log in TASM 5.0 so that every time the program is ran "This is a log" will be entered at the EOF of log.txt. I can't get it to work, it never writes to the EOF. Do I need to use SetFilePointer? If so how? Here's my code:

.386
.model flat, stdcall

GENERIC_WRITE equ 40000000h
CREATE_NEW equ 1
fileFlag equ 080h
OPEN_EXISTING equ 3


extrn ExitProcess : PROC
extrn CreateFileA : PROC
extrn WriteFile : PROC
extrn CloseHandle : PROC


.data

file db "log.txt", 0
msg db "This is a log", 0

len equ $-msg
fhandle dd ?
bwrite dd ?

.code
start:
push 0 ;Create file
push fileFlag
push CREATE_NEW
push 0
push 0
push GENERIC_WRITE
push offset file
call CreateFileA

mov fhandle, eax

push fhandle ;Close File
call CloseHandle

push 0 ;Open File
push fileFlag
push OPEN_EXISTING
push 0
push 0
push GENERIC_WRITE
push offset file
call CreateFileA

mov fhandle, eax

push 0 ;Write File
push offset bwrite
push len
push offset msg
push eax
call WriteFile

push fhandle
call CloseHandle

call ExitProcess

end start


Posted on 2006-08-28 16:54:01 by murray732
You need to start with basic error handling, mainly making sure that the values returned from the API calls are valid (not null/invalid). Reference MSDN for the expected values pertaining to those API calls.
Posted on 2006-08-28 17:03:08 by SpooK
To add to the end of file you will need to use SetFilePointer, that is correct.


push FILE_END ; 2h
push 0
push 0
push fhandle
call SetFilePointer
Posted on 2006-08-28 19:12:38 by JimmyClif
Ok, now I have it so that it doesn't create the file in the program, just opens it and writes to the end of the file. The first time i open it, it properly writes to the file "This is a log" The second time I run it it writes to the file "??????g??????g". The third, fourth and fith and so forth it adds "??????g" to the end of the file. Therefor right now it only writes to the log properly when log.txt == 0kb.


.386
.model flat, stdcall

GENERIC_WRITE equ 40000000h
FILE_END equ 2h
fileFlag equ 080h
OPEN_EXISTING equ 3


extrn ExitProcess : PROC
extrn CreateFileA : PROC
extrn WriteFile : PROC
extrn CloseHandle : PROC
extrn SetFilePointer : PROC


.data

file db "log.txt", 0
msg db "This is a log", 0

len equ $-msg
fhandle dd ?
bwrite dd ?

.code
start:

push 0 ;Open File
push fileFlag
push OPEN_EXISTING
push 0
push 0
push GENERIC_WRITE
push offset file
call CreateFileA

mov fhandle, eax

push FILE_END
push 0
push 0
push fhandle
call SetFilePointer

push 0 ;Write File
push offset bwrite
push len
push offset msg
push fhandle
call WriteFile

push fhandle
call CloseHandle

call ExitProcess

end start



Thanks,

Murray
Posted on 2006-08-28 22:22:08 by murray732
I don't have access to TASM, but I used the equivalent NASM code and I couldn't recreate that problem (worked as it was suppose to), at least not on a Windows 2000 machine.

Also, you can look into using "CREATE_ALWAYS" instead of "CREATE_NEW", if you are interested in creating a new log file instead of opening an existing one (OPEN_EXISITING).
Posted on 2006-08-28 23:12:27 by SpooK
I don't see nothing wrong with the code per se. How about that "Second Time" your run it? Are you creating some kind of loop or are you just double clicking on the exe repeatedly?


Also: You forgot to push an exitcode with ExitProcess.
Posted on 2006-08-29 06:01:25 by JimmyClif
dont write the zero byte!

msg db "This is a log",13,10
Posted on 2006-08-29 08:21:25 by drizz
Just figured that out too. Good Job, drizz.

Use as len = len - 1 to cut out the 0.

Funky way notepad does stuff. If you open the file with a hexeditor then you see that the written text is all there.
Posted on 2006-08-29 08:26:03 by JimmyClif

Just figured that out too. Good Job, drizz.

Use as len = len - 1 to cut out the 0.

Funky way notepad does stuff. If you open the file with a hexeditor then you see that the written text is all there.


Yeah, that is what I used to read the log.txt file on my end.

Seems like Notepad reads in strict ASCII only.
Posted on 2006-08-29 23:13:53 by SpooK
Windows XP's (and probably 2000's too) notepad supports UTF8 and UTF16, but the file must be 'marked' at the beginning. I don't remember the code to be added (0xEF, 0xBB, 0xBF for UTF8, iirc), but notepad uses it to figure out whether the text is ascii or unicode. You can't mix ascii with unicode, though (but you can use ascii chars inside a utf8 file since utf8 is compatible with ascii).
Posted on 2006-08-30 21:45:30 by ti_mo_n
Thanks everybody for your replies  :D...i've been very busy and havn't had the time to come on and reply. I did get it working thanks to you guys. Here it is,

.386
.model flat, stdcall

GENERIC_WRITE equ 40000000h
FILE_END equ 2h
fileFlag equ 080h
OPEN_ALWAYS equ 4


extrn ExitProcess : PROC
extrn CreateFileA : PROC
extrn WriteFile : PROC
extrn CloseHandle : PROC
extrn SetFilePointer : PROC


.data

file db "log.txt", 0
msg db "This is a test", 13, 10

len equ $-msg
fhandle dd ?
bwrite dd ?

.code
start:

push 0 ;Open File
push fileFlag
push OPEN_ALWAYS
push 0
push 0
push GENERIC_WRITE
push offset file
call CreateFileA

mov fhandle, eax

push FILE_END
push 0
push 0
push fhandle
call SetFilePointer

push 0 ;Write File
push offset bwrite
push len
push offset msg
push fhandle
call WriteFile

push fhandle
call CloseHandle

call ExitProcess

end start


Thanks,

Murray

Oh, and how come you use , 13, 10 instead of , 0...I thought strings had to end with a NULL?
Posted on 2006-09-04 16:45:35 by murray732
Oh, and how come you use , 13, 10 instead of , 0...I thought strings had to end with a NULL?

Strings yes, but you are making a file there, presumably a text file, text files
dont have to be zero terminated. As suggested before, use a hex editor to
see what you are really writing to file..

Cheers,
drizz
Posted on 2006-09-04 17:10:46 by drizz