flat assembler
Message board for the users of flat assembler.

Index > Windows > Help with this, WriteFile everytime returns bool false

Author
Thread Post new topic Reply to topic
MAD_DËMON



Joined: 03 Mar 2006
Posts: 23
MAD_DËMON
I made an attempt to write an utility in windows that resizes small bootimages to fit in a floppy size of 1.44 MB but the applications fails trying to write the output file everytime that WriteFile is called, it returns bool false
I don't know what's going wrong with my code

Code:
format PE GUI
entry start

include 'win32ax.inc'

section '.code' code readable executable

start:

 mov [ofn.lStructSize],sizeof.OPENFILENAME
 mov [ofn.hwndOwner],0
 mov [ofn.hInstance],eax
 mov [ofn.lpstrCustomFilter],NULL
 mov [ofn.nFilterIndex],1
 mov [ofn.nMaxFile],1000h
 mov [ofn.lpstrFileTitle],name_buffer
 mov [ofn.nMaxFileTitle],100h
 mov [ofn.lpstrInitialDir],NULL
 mov [ofn.lpstrDefExt],file_extension

 mov [ofn.lpstrFile],path_buffer
 mov [path_buffer],byte 0
 mov [ofn.lpstrFilter],file_filter
 mov [ofn.Flags],OFN_EXPLORER+OFN_ALLOWMULTISELECT+OFN_FILEMUSTEXIST+OFN_HIDEREADONLY
 mov [ofn.lpstrFileTitle],name_buffer
 mov [ofn.lpstrTitle],NULL
 invoke GetOpenFileName,ofn

 invoke CreateFile,name_buffer,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
 cmp eax,INVALID_HANDLE_VALUE
 jne @f

 jmp Exit

@@:
 mov [HANDLE],eax

 invoke GetFileSize,eax,FileSizeHigh
 cmp eax,-1
 jne @f

 jmp Exit

@@:

 invoke ReadFile, dword[HANDLE], FileBuffer, eax, BytesRead, NULL
 test eax,eax
 jnz @f

 jmp Exit

@@:
 invoke CloseHandle,dword [HANDLE]
 test eax,eax
 jnz @f

 jmp Exit

@@:
 invoke CreateFile,DestFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
 cmp eax,-1
 jne @f

 jmp Exit

@@:
 mov [HANDLE],eax

 invoke WriteFile,eax,FileBuffer,$168000,BytesWritten,NULL
 test eax,eax
 jnz @f

 jmp Exit

@@:
 invoke CloseHandle,dword[HANDLE]

Exit:
 invoke ExitProcess,0

section '.data' data readable writable

ofn OPENFILENAME

file_extension db 'BIN',0

file_filter:
 db 'Binary Files',0,'*.BIN;*.286',0
 db 'All files',0,'*.*',0
 db 0

DestFileName db 'mykernel.bin',0

align 4

HANDLE = $
FileSizeLow = HANDLE+4
FileSizeHigh = FileSizeLow+4
BytesRead    = FileSizeHigh+4
BytesWritten = BytesRead+4
name_buffer  = BytesWritten+4
path_buffer = name_buffer+100h
FileBuffer  = path_buffer+1000h

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL',\
          comdlg32,'COMDLG32.DLL'

  include 'apia\kernel32.inc'
  include 'apia\user32.inc'
  include 'apia\comdlg32.inc'    
Post 29 Sep 2006, 17:30
View user's profile Send private message Visit poster's website Reply with quote
wisepenguin



Joined: 30 Mar 2005
Posts: 129
wisepenguin
ive just done a check and it fails at the first ReadFile on my
comp
Post 29 Sep 2006, 17:37
View user's profile Send private message Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
BytesWritten should be declared as doubleword (dd).
Take a look:
Code:

format PE GUI
entry start

include 'win32ax.inc'

section '.code' code readable executable

start:

 mov [ofn.lStructSize],sizeof.OPENFILENAME
 mov [ofn.hwndOwner],0
 mov [ofn.hInstance],eax
 mov [ofn.lpstrCustomFilter],NULL
 mov [ofn.nFilterIndex],1
 mov [ofn.nMaxFile],1000h
 mov [ofn.lpstrFileTitle],name_buffer
 mov [ofn.nMaxFileTitle],100h
 mov [ofn.lpstrInitialDir],NULL
 mov [ofn.lpstrDefExt],file_extension

 mov [ofn.lpstrFile],path_buffer
 mov [path_buffer],byte 0
 mov [ofn.lpstrFilter],file_filter
 mov [ofn.Flags],OFN_EXPLORER+OFN_ALLOWMULTISELECT+OFN_FILEMUSTEXIST+OFN_HIDEREADONLY
 mov [ofn.lpstrFileTitle],name_buffer
 mov [ofn.lpstrTitle],NULL
 invoke GetOpenFileName,ofn

 invoke CreateFile,name_buffer,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
 cmp eax,INVALID_HANDLE_VALUE
 jne @f

 jmp Exit

@@:
 mov [HANDLE],eax

 ;invoke GetFileSize,eax,FileSizeHigh
 invoke GetFileSize,eax,0 ;can be just NULL
 cmp eax,-1
 jne @f

 jmp Exit

@@:

 invoke ReadFile, dword[HANDLE], FileBuffer, eax, BytesRead, NULL
 test eax,eax
 jnz @f

 jmp Exit

@@:
 invoke CloseHandle,dword [HANDLE]
 test eax,eax
 jnz @f

 jmp Exit

@@:
 invoke CreateFile,DestFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
 cmp eax,-1
 jne @f

 jmp Exit

@@:
 mov [HANDLE],eax

 invoke WriteFile,eax,FileBuffer,$168000,BytesWritten,NULL
 test eax,eax
 jnz @f

 jmp Exit

@@:
 invoke CloseHandle,dword[HANDLE]

Exit:
 invoke ExitProcess,0

section '.data' data readable writable

ofn OPENFILENAME

file_extension db 'BIN',0

file_filter:
 db 'Binary Files',0,'*.BIN;*.286',0
 db 'All files',0,'*.*',0
 db 0

DestFileName db 'mykernel.bin',0

align 4

; You should use dd to reserve space to write integer numbers
;HANDLE = $
;FileSizeLow = HANDLE+4
;FileSizeHigh = FileSizeLow+4
;BytesRead    = FileSizeHigh+4
;BytesWritten = BytesRead+4
;name_buffer  = BytesWritten+4
;path_buffer = name_buffer+100h
;FileBuffer  = path_buffer+1000h

HANDLE dd 0 ;Dword-sized to store the File Handle
BytesRead dd 0 ;The API will store the bytes read
BytesWritten dd 0 ;The API will store the bytes written
;name_buffer, path_buffer and FileBuffer must be Dynamic Allocated in your program source-code. Check MSDN for GlobalAlloc() API.
;If you don't want dynamic allocated, you should specify fixex-size reserved data:
;FileBuffer rb 2000 ; Will reserve 2000 bytes

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL',\
          comdlg32,'COMDLG32.DLL'

  include 'apia\kernel32.inc'
  include 'apia\user32.inc'
  include 'apia\comdlg32.inc'
    

Not fully working. You need to add the code to dynamic allocate space for the data. But I think you'll get the idea.
If you have any questions post here.
Post 29 Sep 2006, 17:40
View user's profile Send private message Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
Not working here. It's very strange problem.
If someone knows how to fix it, please post. I'm curious.
Post 29 Sep 2006, 18:24
View user's profile Send private message Reply with quote
wisepenguin



Joined: 30 Mar 2005
Posts: 129
wisepenguin
i posted a working version but deleted it when i seen that you posted a version

i will find it and post it in a jiffy. all i changed was the variable declarations
like you did and $ to 0x


Last edited by wisepenguin on 29 Sep 2006, 18:30; edited 1 time in total
Post 29 Sep 2006, 18:29
View user's profile Send private message Reply with quote
wisepenguin



Joined: 30 Mar 2005
Posts: 129
wisepenguin
Code:
format PE GUI
entry start 

include 'win32ax.inc' 

section '.code' code readable executable 

start: 

 mov [ofn.lStructSize],sizeof.OPENFILENAME 
 mov [ofn.hwndOwner],0 
 mov [ofn.hInstance],eax 
 mov [ofn.lpstrCustomFilter],NULL 
 mov [ofn.nFilterIndex],1 
 mov [ofn.nMaxFile],1000h 
 mov [ofn.lpstrFileTitle],name_buffer 
 mov [ofn.nMaxFileTitle],100h 
 mov [ofn.lpstrInitialDir],NULL 
 mov [ofn.lpstrDefExt],file_extension 

 mov [ofn.lpstrFile],path_buffer 
 mov [path_buffer],byte 0 
 mov [ofn.lpstrFilter],file_filter 
 mov [ofn.Flags],OFN_EXPLORER+OFN_ALLOWMULTISELECT+OFN_FILEMUSTEXIST+OFN_HIDEREADONLY 
 mov [ofn.lpstrFileTitle],name_buffer 
 mov [ofn.lpstrTitle],NULL 
 invoke GetOpenFileName,ofn 

 invoke CreateFile,name_buffer,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL 
 cmp eax,INVALID_HANDLE_VALUE 
 jne @f

 jmp Exit 

@@: 
 mov [HANDLE],eax 

 invoke GetFileSize,eax,FileSizeHigh 
 cmp eax,-1 
 jne @f 

 jmp Exit 

@@: 

 invoke ReadFile, dword[HANDLE], FileBuffer, eax, BytesRead, NULL 
 test eax,eax 
 jnz @f

 jmp Exit 

@@: 
 invoke CloseHandle,dword [HANDLE] 
 test eax,eax 
 jnz @f 

 jmp Exit 

@@: 
 invoke CreateFile,DestFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL 
 cmp eax,-1 
 jne @f 

 jmp Exit 

@@: 
 mov [HANDLE],eax 

 invoke WriteFile,eax,FileBuffer,0x168000,BytesWritten,NULL
 test eax,eax 
 jnz @f 

 jmp Exit 

@@: 
 invoke CloseHandle,dword[HANDLE] 

Exit: 
 invoke ExitProcess,0 

section '.data' data readable writable 

ofn OPENFILENAME 

file_extension db 'BIN',0 

file_filter: 
 db 'Binary Files',0,'*.BIN;*.286',0 
 db 'All files',0,'*.*',0 
 db 0 

DestFileName db 'mykernel.bin',0 

align 4 

HANDLE rd 1 ;HANDLE = $
FileSizeLow rd 1 ;FileSizeLow = HANDLE+4
FileSizeHigh rd 1 ;FileSizeHigh = FileSizeLow+4
BytesRead rd 1 ;BytesRead    = FileSizeHigh+4
BytesWritten rd 1 ;BytesWritten = BytesRead+4
name_buffer rb 256 ;name_buffer  = BytesWritten+4
path_buffer rb 256 ;path_buffer = name_buffer+100h
FileBuffer rb 0x168000 * 10 ; reserves 10 floppy disks of space, FileBuffer  = path_buffer+1000h

section '.idata' import data readable writeable 

  library kernel32,'KERNEL32.DLL',\ 
          user32,'USER32.DLL',\ 
          comdlg32,'COMDLG32.DLL' 

  include 'apia\kernel32.inc' 
  include 'apia\user32.inc' 
  include 'apia\comdlg32.inc'
    
Post 29 Sep 2006, 18:29
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.