News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Invoking ReadFile ??

Started by dedwardk, January 24, 2013, 09:06:39 AM

Previous topic - Next topic

dedwardk

Im having trouble figuring out how to invoke ReadFile ..

My issue is and understanding how to go about this. I opened ReadFile.asm in Irvines library and got confused. I was thinking I needed to set each value before using INVOKE. But when I look into the ReadFile itself (and running it) I see it ask for some of the data on its own. So how is it I send data when its just gonna ask for it? Send it initialized as (?) ...

1. Protoype of ReadFile
2. ReadFile.asm
3. My main.asm file


ReadFile PROTO,
hFile:HANDLE,
lpBuffer:PTR BYTE,
nNumberOfBytesToRead:DWORD,
lpNumberOfBytesRead:PTR DWORD,
lpOverlapped:PTR DWORD



TITLE Reading a File                      (ReadFile.asm)

; Opens, reads, and displays a text file using
; procedures from Irvine32.lib.

INCLUDE Irvine32.inc
INCLUDE macros.inc

BUFFER_SIZE = 50

.data
buffer BYTE BUFFER_SIZE DUP(?)
filename    BYTE 80 DUP(0)
fileHandle  HANDLE ?

.code
main PROC

; Let user input a filename.
mWrite "Enter an input filename: "
mov edx,OFFSET filename
mov ecx,SIZEOF filename
call ReadString

; Open the file for input.
mov edx,OFFSET filename
call OpenInputFile
mov fileHandle,eax

; Check for errors.
cmp eax,INVALID_HANDLE_VALUE ; error opening file?
jne file_ok ; no: skip
mWrite <"Cannot open file",0dh,0ah>
jmp quit ; and quit
file_ok:

; Read the file into a buffer.
mov edx,OFFSET buffer
mov ecx,BUFFER_SIZE
call ReadFromFile
jnc check_buffer_size ; error reading?
mWrite "Error reading file. " ; yes: show error message
call WriteWindowsMsg
jmp close_file

check_buffer_size:
cmp eax,BUFFER_SIZE ; buffer large enough?
jb buf_size_ok ; yes
mWrite <"Error: Buffer too small for the file",0dh,0ah>
jmp quit ; and quit

buf_size_ok:
mov buffer[eax],0 ; insert null terminator
mWrite "File size: "
call WriteDec ; display file size
call Crlf

; Display the buffer.
mWrite <"Buffer:",0dh,0ah,0dh,0ah>
mov edx,OFFSET buffer ; display the buffer
call WriteString
call Crlf

close_file:
mov eax,fileHandle
call CloseFile


quit:
exit
main ENDP

END main



TITLE MASM Template (main.asm)

; Description:
;
; Revision date:

INCLUDE Irvine32.inc



.data

.code
main PROC
call Clrscr



exit
main ENDP
END main




hfheatherfox07

Hello,
And welcome to the forum.....
That is an Irvine example....We have masm32 Includes and libs here, a little different
What are you trying to do ? Convert that example to masm32 ?
Also that is a Console example not a window you can tell by the use of "mWrite "Enter an input filename: "
same as "invoke WriteFile" or "invoke StdOut"  or "print" in masm32


Here is an example of reading a file by Iczelion from Iczelion's Win32 Assembly Tutorial part 12.
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

hfheatherfox07

Here is a simpler example of "invoke ReadFile" ...it is not a console and the only difference between this and your example ( other than it is message box and not Console app) is:

1. your example requires the user to input the filename , as here the file name is in the .data section
2.  it checks for error while reading the file to buffer
3. checks for error whether the buffer size is big enough for the file

Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

hfheatherfox07

Hello,
I made you a console example that will read a text file .... :biggrin:

It has a check for error (If file is not present than it says -can not open file)

Now see if you can fully convert that Irvine example to masm32
would be good console exercise
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

hfheatherfox07

I made you another console example that will read a text file, but this is similar to Irvine example in that it requires a user input ..... :biggrin:

all you have left to do (if you want) is to add the other errors:

1. check for error while reading the file to buffer
2. check for error whether the buffer size is big enough for the file 

There is a little trick to getting user input in a console app in masm ....

So here is that example
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

dedwardk

WOW !! I just logged on and see that you have been pretty busy .. Thanks for spending some time in here .

I guess im lost in more than one place  :greenclp:  I will go back and re-read from chapter 8 on cause I feel I may have mus-understood the concepts and maybe just didnt understand all together.

So let me start by being a little more clear because I seen you asked what exactly Im trying to do. Sorry im "green" to this and my terminology is def lacking (ill try my best..sometimes I think people just know what im talking about so I need to work on being more clear).

1. I need to use Irvine library as its a requirement
2. modify ReadFile.asm so that it can read files larger than its input buffer
4. reduce buffer size to 1024 bytes
5. use a loop to continue reading and displaying the file until it can read no more data.
6. If you use WriteString remember to insert null byte at end of the buffer data


tell you what also .. I have a whole new respect for programmers .. and right now I seriously miss C++ ..heck id even settle for my second favorite Java :D

dedwardk

Oh and I may have even got it wrong on INVOKE .. maybe I can just use Call

I was reading up on a different post and maybe pushing it to the stack and simply using Call instead would work better.


http://www.masmforum.com/board/index.php?PHPSESSID=786dd40408172108b65a5a36b09c88c0&topic=17652.0


I will check over your post and thanks again for your help !!


im also using visual studio 2010

hfheatherfox07

Quote from: dedwardk on January 24, 2013, 03:49:22 PM
im also using visual studio 2010
Well you can use what ever IDE ( integrated development environment)you want as long as you set the path to the masm bin,lib, and include

I prefer to use RadASM
http://masm32.com/board/index.php?topic=229.0

The beauty of Asm , you can use what ever IDE you are comfortable with.

Also changing the buffer size , you just need to change the buffer value
I would try taking out that error that checks for buffer size to begin with :biggrin:
I am not at home and on my cell , so can't modify that file right now lol

Once you get the hang of asm I think you will love it !

Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

hfheatherfox07

Quote from: dedwardk on January 24, 2013, 03:43:40 PM

4. reduce buffer size to 1024 bytes


in the example that you posted above you only have 50 bytes , you mean increase?

Like I said ...just change the number to 1024 ....
I do not have the  Irvine libs set up to modify your file for you but basically here is a masm32 version
I set the buffer to 1024 bytes ...
I am reading windows.inc from masm32 v11 which is 977412 bytes
I copied and pasted what my example read and saved it as test.txt.... You will notice that test.txt is 1024 bytes

Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

hfheatherfox07

Quote from: dedwardk on January 24, 2013, 03:49:22 PM
Oh and I may have even got it wrong on INVOKE .. maybe I can just use Call

I was reading up on a different post and maybe pushing it to the stack and simply using Call instead would work better.


it would be a good idea for you to get the Win32 SDK Reference Help (win32.hlp)

you will notice that ReadFile API :

BOOL ReadFile(

    HANDLE hFile, // handle of file to read
    LPVOID lpBuffer, // address of buffer that receives data 
    DWORD nNumberOfBytesToRead, // number of bytes to read
    LPDWORD lpNumberOfBytesRead, // address of number of bytes read
    LPOVERLAPPED lpOverlapped // address of structure for data
   );



invoke ReadFile, hFile, lpBuffer,nNumberOfBytesToRead, lpNumberOfBytesRead, NULL

or

push lpOverlapped
push lpNumberOfBytesRead
push nNumberOfBytesToRead
push lpBuffer
push hFile
call ReadFile

push 0
lea eax,bytesRead
push eax
push SIZEOF buffer
lea eax,buffer
push eax
push hFile
call ReadFile

Same thing in the aspect that you still have to have all those Parameters ...
If you notice Irvine32 does not use ReadFile , Kip Irvine wrote his own lib that uses ReadFromFile proc were he of-course accesses ReadFile API




Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

jj2007

Quote from: dedwardk on January 24, 2013, 03:49:22 PM
Oh and I may have even got it wrong on INVOKE .. maybe I can just use Call

I was reading up on a different post and maybe pushing it to the stack and simply using Call instead would work better.

Very bad idea, sorry :P
Try invoke ReadFile, 123, 456 - and the assembler will tell you that it's the wrong number of parameters.
Try
  push 456
  push 123
  call ReadFile
... and the assembler will be happy but your proggie will mysteriously crash.

Gunther

Hi dedwardk,

welcome to the forum.

jj2007 is right, because the PUSH method doesn't make an appropriate type checking.

Gunther
You have to know the facts before you can distort them.

dedndave

invoke is also easier to read
i do, however, use the push method if i have in-register variables to push for more than one invoke
or, if i want to call other functions while creating the parameter frame

dedwardk

To -  hfheatherfox07 :

you are correct I made a mistake. The book says modify the program on page xxx which listed it as 5000.  I instead opened file in the library which was only different by the buffer value.  Sorry for my mistake.

But I do understand to just change it to 1024.




To -  jj2007:



   thanks for that reminder. I remember hearing that in class but forgot about why you would do so. Makes sense !!








   hopefully by this evening I will have some more code up for you guy to look at . Thanks for working with me !! Sorry its Irvine too :(

hfheatherfox07

Here is the Irvine Version , 1024 Buffer size, and reads until it can not read!
Note : I added :
INCLUDELIB Irvine32.lib
INCLUDELIB \masm32\lib\kernel32.lib
INCLUDELIB \masm32\lib\User32.lib

Because I assembled with MASM32 , You do not need those

Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.