Hello,
I have a problem with subj.
I read file, close handle, then read this file again (file handler appear the same) and get the number of bytes read is zero (return value non zero). OK, MSDN say the file pointer is beyond the end of file, no problem


invoke CreateFile, addr filename,GENERIC_READ,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
cmp eax, -1
jz @@somewhere_else
mov hFile, eax
invoke SetFilePointer, hFile,0,0,FILE_BEGIN
invoke ReadFile, hFile, ADDR buffer, buf_len, ADDR buf_len, 0

Very first time is OK, but next - problem persist.
OS:Win2K
Posted on 2002-03-14 08:18:14 by masquer
When you first open your file it's pointer is at the begining so you don't have to set the file pointer to the begining, for each byte you read the file pointer is being incremented by one so if your ReadFile reads everything then you should change the pointer but if you close and reopen the pointer is at the begining again.
maybe what you want is

CreateFile
ReadFile
SetFilePointer ;begining
ReadFile
Posted on 2002-03-14 09:08:37 by Quantum
masquer are you trying to ReadFile on your handle after you CloseHandle'd it?
That obviously will not work (and if it does - it's a bug ;)).

As for your code, compare to INVALID_HANDLE_VALUE instead of -1,
it's a lot cleaner.
Posted on 2002-03-14 09:20:34 by f0dder
No, it is don't work.
The situation is:
I need to insert one smaller file in another bigger. But there is a situation when I need to insert this file at several offset in the bigger one (optionally), that's why I read this small file, close it, and if I need, I open it again, and there is a problem, as I read in MSDN, with file pointer.

2 f0dder:
No, I'm not read file after close handle. In my proc I createfile, then readfile, then closehandle. Ther first time everything fine, then after readfile data is actually in memory at the data buffer, but nNumberOfBytesToRead is zero :(
Posted on 2002-03-14 09:25:25 by masquer
I don't know what file pointer problem you are talking a bout - after
CreateFile, file pointer is always at file start. So either keep doing
createfile/closehandle, or keep the file open and do setfilepointer
before each read.

Can't see why nNumberOfBytesToRead (buf_len) should end up zero...
*shrug*.

Oh well, another comment on your code... use "OPEN_EXISTING"
instead of "OPEN_ALWAYS" if the file is something you need to
insert (since it logically has to be already existant to be inserted
into another file :)).
Posted on 2002-03-14 09:42:53 by f0dder
I'm too do not understand what is going on. Here is my proc

buf_V, buf_P - buffers of filenames (_V - is bigger)
offs - offset in the _V file, specified by user
When it run second time and only offset is different after the ReadFile bufin_len=0.
After each reading I'm clearing the buf_V, buf_P, and read them from edit box
:confused:


writein proc uses ecx
invoke CreateFile, addr buf_V, GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
cmp eax, INVALID_HANDLE_VALUE
jz @@exit
mov hV, eax

mov ecx, offs ; offset
cmp ecx, -1 ;if offset==-1 then go to the EOF
jz @@bigger

invoke GetFileSize, hV, NULL
mov ecx, offs
cmp eax, ecx
jl @@bigger
push FILE_BEGIN
jmp @@end
@@bigger:
push FILE_END
xor ecx, ecx
@@end:
push 0
push ecx
push hV
call SetFilePointer
cmp eax, -1
jz @@exit

invoke CreateFile, addr buf_P, GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
cmp eax, INVALID_HANDLE_VALUE
jz @@exit
mov hP, eax
@@read:
invoke ReadFile, hP, ADDR bufin, bufin_len, ADDR bufin_len, 0
mov ecx, bufin_len
or ecx, ecx
jnz @@write
jmp @@preexit
@@write:
invoke WriteFile, hV, ADDR bufin, bufin_len, ADDR bufin_len, 0
jmp @@read
@@preexit:
invoke CloseHandle, hP
invoke CloseHandle, hV
mov eax,1
ret
@@exit:
xor eax, eax
ret
writein endp
Posted on 2002-03-14 10:07:36 by masquer
All I can see is -- when your proc is done, bufin_len will be 0. So you will need to reinitialize bufin_len before calling the proc again.
Posted on 2002-03-15 13:53:43 by tenkey
Thank you, but what do you mean reinitialize - it is already zero, it is the condition of stop reading.
Here is the simpler code with tha same effect - after first cycle ReadFile did not work.

Can someone explain what's going on

My text file is 4 byte length


.data
buf_P db "text.txt",0
bufin_len dd 4096
offs dd 10h
.data?
bufin dd 4096 dup (?)
hP dd ?

.code

start:
invoke CreateFile, offset buf_P, GENERIC_READ,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
cmp eax, INVALID_HANDLE_VALUE
jz @@final
mov hP, eax
@@read:
invoke ReadFile, hP, ADDR bufin, bufin_len, ADDR bufin_len, 0
mov ecx, bufin_len
or ecx, ecx
jnz @@read
jmp @@exit
@@exit:
mov bufin_len, ecx
invoke CloseHandle, hP
add offs, 10h
cmp offs, 100h
jz @@final
jmp start
@@final:
invoke ExitProcess,0
end start
Posted on 2002-03-16 03:17:03 by masquer
The first bufin_len in your ReadFile invoke is the maximum number of bytes you want. After the first call to writein, bufin_len is set to zero, so when you call writein a second time, you are asking for zero bytes (unless you reset bufin_len to a nonzero value).

Your last example is different. The second read yields zero bytes because there are no bytes to read the second time. (All four bytes were read the first time.)

I suggest using different variables for bufin_len and the number of bytes actually read.

Another tip: you don't need to jump around a jump in 32-bit code. Long conditional jumps are supported.
Posted on 2002-03-18 13:42:09 by tenkey
Thanks, tenkey.
Mirno makes it faster. I'm just get lost. :)
http://www.asmcommunity.net/board/index.php?topic=4293
Posted on 2002-03-18 14:01:09 by masquer