flat assembler
Message board for the users of flat assembler.

Index > Windows > Having trouble about WriteFile.

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 13 Sep 2010, 19:51
Hello everyone. I'm trying to write hex strings as ascii in file but I fail. here's simple code and can someone fix ? actually problem is program is crashing but strings are written in file too. I tried repne scasb method, getting size of string and putting in DWORD value but it seems doesnt work. Can anyone tell me how to convert DWORD to Byte ? or how to make this code work ? Thanks. Smile
Code:
format pe console 4.0
include 'WIN32AX.INC'
entry main
.data
buf db 65,65,65
size dd ?
.code
mov ecx,-1
xor al,al
mov edi,buf
repne scasb
not ecx
dec ecx
mov [size],ecx
proc main
invoke CreateFile,'test.txt',GENERIC_WRITE,0,0,4,FILE_ATTRIBUTE_NORMAL,0
invoke WriteFile,eax,buf,size,0,0
invoke ExitProcess,0
endp
data import
library kernel32,'kernel32.dll'
include 'API\KERNEL32.INC'
end data    
Post 13 Sep 2010, 19:51
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 13 Sep 2010, 20:11
This works for me:
Code:
format pe console 4.0
include 'WIN32AX.INC'

.data
      buf db 65,65,65
     bufsize = $ - buf
   byteswritten dd ?
.code
  main:
   invoke  CreateFile, 'test.txt', GENERIC_WRITE, 0, 0, 4, FILE_ATTRIBUTE_NORMAL, 0
  invoke  WriteFile, eax, buf, bufsize, byteswritten, 0
       invoke  ExitProcess, 0

.end main    
Fourth parameter of 'WriteFile' function can be NULL if the last one is not NULL. Otherwise you have to specify dword variable named, for example, 'byteswritten' where this function returns number of bytes written. Smile
Post 13 Sep 2010, 20:11
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 13 Sep 2010, 20:23
Oh, Thank you mate! Smile I understand now. TY! Smile
Post 13 Sep 2010, 20:23
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1403
Location: Piraeus, Greece
Picnic 13 Sep 2010, 20:53
Here a poor AppendToFile() procedure, just to decorate topic Smile
Code:
proc AppendToFile uses ecx ebx edx, lpFileName, lpBuffer

            local nBytes:DWORD

            mov [nBytes], INVALID_HANDLE_VALUE
            
            invoke CreateFile, [lpFileName], GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
            cmp eax, INVALID_HANDLE_VALUE
            jz .finish
            
            mov ebx, eax

            invoke SetFilePointer, eax, 0, 0, FILE_END
            cmp eax, INVALID_HANDLE_VALUE
            jz .finish

            invoke lstrlen, [lpBuffer]
            invoke WriteFile, ebx, [lpBuffer], eax, addr nBytes, 0
            invoke CloseHandle, ebx

.finish:    mov eax, [nBytes]
            ret
endp

    
Post 13 Sep 2010, 20:53
View user's profile Send private message Visit poster's website Reply with quote
b1528932



Joined: 21 May 2010
Posts: 287
b1528932 13 Sep 2010, 21:13
either numberofbyteswritten or overlapped argument must be valid. They both point to 0 address, wich i guess windows tread as something special.
Post 13 Sep 2010, 21:13
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 13 Sep 2010, 21:46
Nope, just did like MHajduk wrote and everything seems work fine! Smile
Post 13 Sep 2010, 21:46
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.