I came across a strange problem with ReadFile(). The textfile is opened correctly but it reads only 4 bytes instead of number of bytes declared in MemorySize. The same code in C is executed properly. Somebody knows what’s wrong?
Regards
.DATA
FileName DB "test.txt",0
MemorySize DWORD 10
.DATA?
hFile HANDLE ?
pMemory DWORD ?
SizeR DWORD ?
.CODE
start:
INVOKE CreateFile, ADDR FileName,\
GENERIC_READ, 0, NULL,\
OPEN_EXISTING, 0, NULL
mov hFile, eax
INVOKE GlobalAlloc, GMEM_FIXED or GMEM_ZEROINIT, MemorySize+1
mov pMemory, eax
INVOKE ReadFile, hFile, ADDR pMemory, MemorySize, ADDR SizeR, NULL
INVOKE MessageBox, 0, ADDR pMemory, 0, 0
INVOKE LocalFree, pMemory
INVOKE CloseHandle, hFile
ret
end start
In these two lines, you are passing a pointer to a pointer for pMemory. GlobalAlloc returns a pointer to memory and ReadFile expects a pointer NOT a pointer to a pointer. Remove ADDR and it should work.
This is wrong also. You cannot do MemorySize + 1 as you would to in a High Level Language. In this case you need to do:
before your call to GlobalAlloc