4 Replies - 1028 Views - Last Post: 21 October 2012 - 03:53 PM

#1 Latias  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 28-June 12

Problem with ReadFile

Posted 11 October 2012 - 11:16 AM

have to reopen this thread because i got a another problem.

i created a simple txt reader in masm, everything works fine but when i open a filled txt file, some other chars from buffer or memory will be displayed too.
like in the txt file is written: "12345", then after opening it shows as text "12345!>-"

so my ask is what is wrong with this code?

.Data
NamePathBuffer	db 265 dup (0)

.Data?
hFile				DWORD		? 
-------------


Open_File Proc hWin:DWORD, id_edit:UINT, buf:DWORD, buf_size:DWORD ,nFileSize : DWORD,hMem :DWORD


	invoke RtlZeroMemory, Addr buf, SIZEOF buf
	invoke RtlZeroMemory, Addr hMem, SIZEOF hMem
	invoke RtlZeroMemory, Addr hFile, SIZEOF hFile
	invoke RtlZeroMemory, Addr nFileSize, SIZEOF nFileSize
	invoke RtlZeroMemory, Addr NamePathBuffer, SIZEOF NamePathBuffer
	invoke RtlZeroMemory, Addr buf_size, SIZEOF buf_size

	invoke GetOpenFileName, Offset OFNstruc
		.if eax== TRUE
			invoke CreateFile, addr NamePathBuffer,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL
				.if eax == -1
					Call GetLastError
						.if eax == ERROR_FILE_NOT_FOUND
							invoke MessageBox, NULL, Addr Message, Addr Message, MB_OK
						.elseif eax == ERROR_ACCESS_DENIED
							invoke MessageBox, NULL, Addr Message2, Addr Message2, MB_OK
						.endif
				.endif
			mov	hFile,eax
			invoke GetFileSize, hFile, NULL
			mov nFileSize, eax
			invoke GlobalAlloc,GPTR, nFileSize
			mov hMem,eax
			push	NULL
			mov	eax,esp
			invoke ReadFile,hFile,hMem,nFileSize,eax, NULL
			pop eax
			invoke CloseHandle,hFile
			invoke SetDlgItemText, hWin,id_edit, hMem
			invoke GlobalFree, hMem
		.endif
			invoke RtlZeroMemory, Addr buf, SIZEOF buf
	ret
Open_File endp





Is This A Good Question/Topic? 0
  • +

Replies To: Problem with ReadFile

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: Problem with ReadFile

Posted 11 October 2012 - 05:03 PM

I moved your last post to a new thread. New problem, new thread.

You posted a snippet of code and not the surrounding code, so I have no idea of what those parameters are. I do know you do not need all those parameters to your procedure. I had to create a sample project to test it with.

This works for me:
OpenMyFile Proc uses esi edi ebx hWin:DWORD, id_edit:DWORD 

    invoke  RtlZeroMemory, offset NamePathBuffer, SIZEOF NamePathBuffer
    invoke  RtlZeroMemory, offset ofn, sizeof OPENFILENAME
    
	mov		ofn.lStructSize, sizeof OPENFILENAME
	push	hWin
	pop		ofn.hwndOwner
	mov		ofn.lpstrFile, offset NamePathBuffer
	mov		ofn.nMaxFile, sizeof NamePathBuffer
				
    invoke  GetOpenFileName, Offset ofn
	.if eax== TRUE
		invoke CreateFile, offset NamePathBuffer,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL
		.if eax == -1
			Call GetLastError
				.if eax == ERROR_FILE_NOT_FOUND
				    invoke MessageBox, NULL, Addr Message, Addr Message, MB_OK
				.elseif eax == ERROR_ACCESS_DENIED
				    invoke MessageBox, NULL, Addr Message2, Addr Message2, MB_OK
				.endif
        jmp     Done
		.endif
		mov     esi, eax

		invoke GetFileSize, esi, NULL
		inc     eax  ; <<<<<<<<<<<<<<<<  need 1 byte larger than file size for NULL!!!!
		mov     edi, eax

		invoke  HeapAlloc, hHeap, HEAP_ZERO_MEMORY, edi
        mov     ebx, eax		

        push    NULL
		invoke  ReadFile, esi, ebx, edi, esp, NULL
		pop     eax
		
		invoke  CloseHandle, esi
		invoke  SetDlgItemText, hWin, id_edit, ebx
		invoke  HeapFree, hHeap, 0, ebx
	.endif

Done:	
    ret
OpenMyFile endp


Call from a button press:
invoke  OpenMyFile, hWin, 1002


.data?
hHeap               dword   ?
ofn                 OPENFILENAME <?>
NamePathBuffer      db MAX_PATH  + 1 dup (?)

Moved your buffer for the file name to the .data? (uninitialized) section. When you put a buffer in the .data (initialized) section, it makes your final exe that many bytes bigger.

At program startup you need to get the process heap -
    invoke  GetProcessHeap
    mov     hHeap, eax


The Global* memory functions are old and slow so we use Heap* or Virtual* memory functions.

You need to zero out the OPENFILENAME structure or you will run into mysterious bugs. Also, when you create a buffer for the file contents using the file size, there is no room for a NULL terminator. You are then setting text without a null terminator which is what windows uses to find the end of the string. So, add 1 to the filesize before you create a buffer for the contents.
Was This Post Helpful? 1
  • +
  • -

#3 Latias  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 28-June 12

Re: Problem with ReadFile

Posted 12 October 2012 - 08:50 AM

ah, so the problem was just a simple INC EAX to define the null terminator of the string :stuart:

now i edited the OPEN_File procedure and it seems to be working now.
but now i used the same method for correcting the SAVE_File procedure. it working too but, when I open a file, save a file, clear the editbox so its empty, and then saves a empty editbox, the txtfile is still filled.

I dont know how that is possible because i cleared all buffers with rtlZeroMemory....

Save_File Proc hWin:DWORD, id_edit:UINT

	LOCAL buffer [256] :BYTE

	mov ofn.lStructSize,SIZEOF ofn
	push hWin
	pop ofn.hwndOwner
	push hInstance
	pop ofn.hInstance
	mov ofn.lpstrFilter, OFFSET strFilter
	mov ofn.lpstrFile, OFFSET NamePathBuffer
	mov ofn.nMaxFile,260
	mov ofn.lpstrDefExt,OFFSET strDefExt
	mov ofn.Flags, OFN_FILEMUSTEXIST or OFN_PATHMUSTEXIST or OFN_LONGNAMES or OFN_EXPLORER or OFN_HIDEREADONLY

	invoke GetSaveFileName, Addr ofn
	invoke GetDlgItemText,hWin, id_edit, addr buffer,SIZEOF buffer
	invoke CreateFile, Addr NamePathBuffer,GENERIC_READ or GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
	mov edi, eax
	invoke  HeapAlloc, hHeap, HEAP_ZERO_MEMORY, buffer
	mov esi, eax
	invoke WriteFile, edi, Addr buffer, SIZEOF buffer, esi, NULL

	invoke HeapFree, hHeap, NULL, esi
	invoke CloseHandle, edi
	invoke RtlZeroMemory, Addr NamePathBuffer, SIZEOF NamePathBuffer
	invoke RtlZeroMemory, Addr buffer, SIZEOF buffer
	invoke RtlZeroMemory, offset ofn, SIZEOF OPENFILENAME
	ret
Save_File endp



Open_File Proc hWin:DWORD, id_edit:UINT

		mov ofn.lStructSize,SIZEOF ofn
		push hWin
		pop ofn.hwndOwner
		push hInstance
		pop ofn.hInstance
		mov ofn.lpstrFilter, OFFSET strFilter
		mov ofn.lpstrFile, OFFSET NamePathBuffer
		mov ofn.nMaxFile,260
		mov ofn.lpstrDefExt,OFFSET strDefExt
		mov ofn.Flags, OFN_FILEMUSTEXIST or OFN_PATHMUSTEXIST or OFN_LONGNAMES or OFN_EXPLORER or OFN_HIDEREADONLY 

	invoke GetOpenFileName, Offset ofn
		.if eax== TRUE
			invoke CreateFile, addr NamePathBuffer,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL
				.if eax == -1
					Call GetLastError
						.if eax == ERROR_FILE_NOT_FOUND
							invoke MessageBox, NULL, Addr Message, Addr Message, MB_OK
						.elseif eax == ERROR_ACCESS_DENIED
							invoke MessageBox, NULL, Addr Message2, Addr Message2, MB_OK
						.endif
				.endif
				
			mov esi, eax
			invoke GetFileSize, esi, NULL
			inc eax
			mov edi, eax
			invoke  HeapAlloc, hHeap, HEAP_ZERO_MEMORY, edi
			mov ebx, eax		
			push NULL
			invoke ReadFile, esi, ebx, edi, esp, NULL
			pop eax
			invoke CloseHandle, esi
			invoke SetDlgItemText, hWin, id_edit, ebx
			invoke HeapFree, hHeap, NULL, ebx
		.endif
		
	invoke  RtlZeroMemory, offset NamePathBuffer, SIZEOF NamePathBuffer
	invoke  RtlZeroMemory, offset ofn, SIZEOF OPENFILENAME
	ret
Open_File endp

Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: Problem with ReadFile

Posted 12 October 2012 - 04:23 PM

Your Save_File routine:
Your buffer is created on the stack when the routine starts, and is destroyed when the routine returns. You do not need to zero the buffers memory right before the function returns, you *should* zero the memory at the beginning of the function *before* you use buffer. Since this is a location on the stack, buffer will contain garbage of whatever was at that location prior to the function prologue. This is why it is important to zero a "local" byte buffer before usage. Actually you should put your 3 ZeroMemory calls right after LOCAL buffer [256] :BYTE
invoke RtlZeroMemory, Addr buffer, SIZEOF buffer

Should be anywhere before the call to GetDlgItemText
invoke RtlZeroMemory, Addr NamePathBuffer, SIZEOF NamePathBuffer
invoke RtlZeroMemory, offset ofn, SIZEOF OPENFILENAME

Should be right before you start filling in the OPENFILENAME structure.

What is this?!!!!!
invoke  HeapAlloc, hHeap, HEAP_ZERO_MEMORY, buffer

The last parameter is the size of memory to allocate, you are telling it to use the value in buffer. Being it is on the stack, it could, eh, never mind. You don't need to allocate space on the heap for the bytes written, instead create a dword var on the stack: local lpNumberOfBytesWritten:DWORD and use that instead.

Your open file routine: In the code I provided, I have ZeroMemory at the beginning for a reason, yet you moved it to the end where it is of no use, why?
Was This Post Helpful? 1
  • +
  • -

#5 Latias  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 28-June 12

Re: Problem with ReadFile

Posted 21 October 2012 - 03:53 PM

pc was broken so for long time couldn answer but the great good news is that its working now :bananaman:

thank you verryyy much sir i like you :smartass: :rolleyes:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1