hi,
in the following code i just want to read out the contents of a file into memory, change some stuff and then write the whole memory block back to the file. the problem is that ReadFile doesn't work correctly. GetLastError returns 5 (ERROR_ACCESS_DENIED). i don't know what the problem could be. writing to the file back works great, but of course the rest of the file is filled out with zeroes (because of the error reading the file).



GetANDWrite proc uses ecx esi edi ebx edx dlghandle:DWORD
invoke CreateFile,ADDR filename,\
GENERIC_WRITE, ;| Open output file if
FILE_SHARE_READ, ;| it exist, if not create
NULL, OPEN_ALWAYS, ;| it...
FILE_ATTRIBUTE_NORMAL, ;|
NULL ;|
mov hFile,eax
invoke SetFilePointer,hFile,0,0,FILE_END
mov filesize,eax
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,eax
mov hMemory,eax
invoke GlobalLock,hMemory
mov pMemory,eax
invoke SetFilePointer,hFile,0,0,FILE_BEGIN
mov eax,filesize
dec eax
invoke ReadFile,hFile,pMemory,eax,addr bytesread,0
xor esi,esi
xor edi,edi
invoke GetDlgItemText,dlghandle,5000,addr bufforstring,32
.if eax!=0
mov ecx,eax
mov edi,pMemory
add edi,724
lea esi,bufforstring
rep movsb
.endif
invoke RtlZeroMemory,addr bufforstring,32
xor esi,esi
xor edi,edi
invoke GetDlgItemText,dlghandle,5001,addr bufforstring,32
.if eax!=0
mov ecx,eax
mov edi,pMemory
add edi,936
lea esi,bufforstring
rep movsb
.endif
invoke RtlZeroMemory,addr bufforstring,32
xor esi,esi
xor edi,edi
invoke GetDlgItemText,dlghandle,5002,addr bufforstring,32
.if eax!=0
mov ecx,eax
mov edi,pMemory
add edi,1151
lea esi,bufforstring
rep movsb
.endif
invoke SetFilePointer,hFile,0,0,FILE_BEGIN
invoke WriteFile,hFile,pMemory,filesize,addr byteswritten,0
@@:
invoke CloseHandle,hFile
invoke GlobalUnlock,pMemory
invoke GlobalFree,hMemory
ret
GetANDWrite endp


thanks for any help!

cya
Posted on 2001-09-01 04:56:47 by NOP-erator
GENERIC_WRITE should be GENERIC_WRITE + GENERIC_READ

Thomas
Posted on 2001-09-01 04:58:37 by Thomas
hi Thomas,
thank you for the very fast answer. about 10 seconds after i posted my question. good to have so much people that see such little faults!

it worked. thanks!

cya
Posted on 2001-09-01 05:01:38 by NOP-erator