flat assembler
Message board for the users of flat assembler.

Index > Windows > ReadFile returns ERROR_INVALID_PARAMETER!

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Hi everyone. I'm stuck on ReadFile API.. I've followed MSDN but seems it's not helpful for me.. I tried so much but every time I'm getting ERROR_INVALID_HANDLE or ERROR_NOACCESS.. Here's code and tell me someone what I'm doing wrong! Thank you.
Code:
format PE GUI 4.0
include 'WIN32AX.INC'
entry main
section '.data' data readable writeable
hFile dd ?
fSize dd ?
buffer dd ?
rbytes dd ?
fname db "test.txt",0
section '.text' code readable executable
proc main
invoke CreateFile,fname,GENERIC_READ,FILE_SHARE_READ+FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED,0
mov [hFile],eax
invoke GetFileSize,eax,0
mov [fSize],eax
invoke ReadFile,[hFile],buffer,[fSize],0,0
invoke MessageBox,0,ebx,ebx,MB_OK+MB_ICONINFORMATION
ret
endp
section '.idata' import data readable
library user32,'user32.dll',\
        kernel32,'kernel32.dll'
include 'API\USER32.INC'
include 'API\KERNEL32.INC'
section '.reloc' fixups data discardable readable    


Last edited by Overflowz on 13 Mar 2011, 20:25; edited 1 time in total
Post 13 Mar 2011, 16:40
View user's profile Send private message Reply with quote
b1528932



Joined: 21 May 2010
Posts: 287
b1528932
it require either overlapped or number_of_transferred. Otherwise how would you know how many data you read?
Post 13 Mar 2011, 17:10
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
same problem with this.
Code:
format PE GUI 4.0
include 'WIN32AX.INC'
entry main
section '.data' data readable writeable
hFile dd ?
fSize dd ?
buffer dd ?
rbytes dd ?
fname db "test.txt",0
section '.text' code readable executable
proc main
invoke CreateFile,fname,GENERIC_READ,FILE_SHARE_READ+FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL+FILE_FLAG_OVERLAPPED,0
mov [hFile],eax
xor ecx,ecx
invoke GetFileSize,eax,ecx
mov [fSize],eax
invoke ReadFile,[hFile],buffer,[fSize],rbytes,0
invoke MessageBox,0,buffer,buffer,MB_OK+MB_ICONINFORMATION
ret
endp
section '.idata' import data readable
library user32,'user32.dll',\
        kernel32,'kernel32.dll'
include 'API\USER32.INC'
include 'API\KERNEL32.INC'
section '.reloc' fixups data discardable readable    
Post 13 Mar 2011, 18:30
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: Paradise Falls
Picnic
Hi,

Unless you dynamically allocate bytes using the api to get a dwpointer, buffer must be delcared.

Code:
section '.data' data readable writeable
hFile dd ?
fSize dd ?
buffer rb 1024
rbytes dd ?
fname db "test.txt",0
section '.text' code readable executable
proc main
invoke CreateFile,fname,GENERIC_READ,FILE_SHARE_READ+FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
mov [hFile],eax
xor ecx,ecx
invoke GetFileSize,eax,ecx
mov [fSize],eax
invoke ReadFile,[hFile],buffer,[fSize],rbytes,0
invoke MessageBox,0,buffer,fname,MB_OK+MB_ICONINFORMATION
ret
endp
    
Post 14 Mar 2011, 09:16
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Picnic
Thank you! I got it now and isn't better way to allocate memory space for file ? like GlobalAlloc or LocalAlloc ? I don't know what difference are between them.. Because I need to work with files more than 100MB+
Post 14 Mar 2011, 10:43
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: Paradise Falls
Picnic
Overflowz wrote:
Picnic
Thank you! I got it now and isn't better way to allocate memory space for file ? like GlobalAlloc or LocalAlloc ? I don't know what difference are between them.. Because I need to work with files more than 100MB+

You're welcome.

Yes i think it's the only way for such big files. Search windows section about dynamic memory allocation to read about the differences.
Don't forget, error checking for each api function!

Code:
section '.data' data readable writeable
hFile dd ?
fSize dd ?
hHeap dd ?
buffer dd ?
rbytes dd ?
fname db "test.txt",0
section '.text' code readable executable
proc main
invoke CreateFile,fname,GENERIC_READ,FILE_SHARE_READ+FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
mov [hFile],eax
xor ecx,ecx
invoke GetFileSize,eax,ecx
mov [fSize],eax
invoke GetProcessHeap
mov [hHeap], eax
invoke HeapAlloc, [hHeap], HEAP_ZERO_MEMORY, [fSize]
mov [buffer], eax
invoke ReadFile,[hFile],[buffer],[fSize],rbytes,0
invoke MessageBox,0,[buffer],fname,MB_OK+MB_ICONINFORMATION
invoke HeapFree, [hHeap], 0, [buffer]
ret
endp
    


p.s. HeapAlloc usage, hope it runs, i'm at the office Smile
Post 14 Mar 2011, 11:16
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
What's difference between HeapAlloc/GlobalAlloc/LocalAlloc functions ?
Post 14 Mar 2011, 17:06
View user's profile Send private message Reply with quote
SFeLi



Joined: 03 Nov 2004
Posts: 140
SFeLi
GlobalAlloc and LocalAlloc functions are using HeapAlloc (which is forwarded to NTDLL.RtlAllocateHeap) to allocate memory. There was a difference between Global/Local in 16-bit world (Win 3.1), but now both do the same thing.
Post 15 Mar 2011, 06:06
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
SFeLi
Got it, thanks for info Smile
Post 15 Mar 2011, 10:54
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.