News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

ReadFile

Started by Nilrem, January 24, 2005, 04:48:23 PM

Previous topic - Next topic

Nilrem

invoke ReadFile, CurrentFile, buffer2, sizeof(DWORD)*256, dwNumRead, NULL
    invoke StdOut, ADDR FileRead
    mov lpstring, input(); Wait for user input
    invoke CloseHandle,CurrentFile; bTest will be true if the file closed correctly.
        ret


Upon running this code the program just excits, I found this strange because it should have waitd for user input before exiting to the desktop. So I ran my debugger (OllyDbg) and upon executing the api ReadFile it becomes unreadable to the debugger. Can anybody please help?
See the attachment for a screenshot of the debugger; here is the variable setups:

.data
CurrentFile DB ".\Resource\Read.txt",0
FileRead DB "File was read.",0
nBytesToRead DW 256
dwNumRead DW ?

.data?
nBytesRead db 512 dup(?)

Thankyou.

http://intechhosting.com/~access/forums/index.php?act=Attach&type=post&id=198

P.S. Here is the full proc just incase it is needed:
FileList proc

LOCAL lpstring[128]:DWORD; Used for input()
LOCAL buffer[128]:BYTE; Used for putting strings together
LOCAL buffer2[512]:BYTE;
LOCAL buffer3[512]:BYTE

invoke CreateFile, ADDR CurrentFile, GENERIC_READ, FILE_SHARE_READ,
        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL;
       
        invoke GetLastError; Get the return value of CreateFile
        xor ebx,ebx; Set ebx to 0
        mov ebx,eax; Move the value of GetLastError to ebx
        .if ebx != 0; If the file does not exist
        strcat ADDR buffer, ADDR CurrentFile, ADDR OpenFileError; Put the strings together
        invoke StdOut, addr buffer; Print the new combined string to the screen
        mov lpstring, input(); Wait for user input
        ret
    .endif
   
    ;invoke CloseHandle,CurrentFile; bTest will be true if the file closed correctly.
   
    invoke ReadFile, CurrentFile, buffer2, sizeof(DWORD)*256, dwNumRead, NULL
    invoke StdOut, ADDR FileRead
    mov lpstring, input(); Wait for user input
    invoke CloseHandle,CurrentFile; bTest will be true if the file closed correctly.
        ret

FileList endp

Nilrem

#1
I decided to try and rectify the situation and now have the following (still not working) code:

.data

OpenFileError DB " Could not be found.",0
CurrentFile DB ".\Resource\Read.txt",0
FileRead DB "File was read.",0
nBytesToRead DW 256
;dwNumRead DW ?
;lpstring DW 128
       
.data?

Numb dd ?
buffer2 db 2 dup(?)
hFile HANDLE ?

.code
FileList proc

LOCAL lpstring[128]:DWORD; Used for input()
LOCAL buffer[128]:BYTE; Used for putting strings together
;LOCAL buffer2[512]:BYTE;
LOCAL buffer3[512]:BYTE

invoke CreateFile, ADDR CurrentFile, GENERIC_READ, FILE_SHARE_READ,
        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL;
       
        mov hFile, eax
        invoke GetLastError; Get the return value of CreateFile
        xor ebx,ebx; Set ebx to 0
        mov ebx,eax; Move the value of GetLastError to ebx
        .if ebx != 0; If the file does not exist
        strcat ADDR buffer, ADDR CurrentFile, ADDR OpenFileError; Put the strings together
        invoke StdOut, addr buffer; Print the new combined string to the screen
        mov lpstring, input(); Wait for user input
        ret
    .endif
   
    ;invoke CloseHandle,CurrentFile; bTest will be true if the file closed correctly.
   
    invoke ReadFile, hFile, buffer2, 2, ADDR Numb, NULL
    invoke StdOut, ADDR FileRead
    mov lpstring, input(); Wait for user input
    invoke CloseHandle,hFile; bTest will be true if the file closed correctly.
        ret

FileList endp

MichaelW

In your first version the call to ReadFile fails because you are you are passing the first byte of CurrentFile instead of the file handle, the first byte of Buffer2 instead of the address of Buffer2, and the value of dwNumRead instead of the address of dwNumRead. It looks like you corrected only two of these problems in the second version. You don't need a debugger to find simple problems like this. I found this one by guessing which statement might be causing the problem, commenting it out, rebuilding, and running the program. The ReadFile statement was my second guess.
eschew obfuscation

Nilrem

Thanks, but debuggers are useful no matter how simple the explanation. I just have to get used to the syntax of asm.

Nilrem

I'm having trouble printing to the screen what I have read in from a file.

My code:

invoke ReadFile, ADDR hFile, ADDR buffer2, 2, ADDR Numb, NULL
    invoke StdOut, ADDR buffer2
    mov lpstring, input(); Wait for user input
    invoke CloseHandle, ADDR hFile; Close the file
        ret
however the screen remains blank. Obviously something small I am overlooking.

Nilrem

#5
Sorry for bumping, but this thread was shown as read to me, when it shouldn't have been marked like that, so I was thinking it might have happened to other users. One more thing I have buffer2 in the '.data?' section as the following:
buffer2 db 256 dup(?)

MichaelW

I don't know what is in the file, but you are only reading 2 bytes of it.

BOOL ReadFile(
  HANDLE hFile,
  LPVOID lpBuffer,
  DWORD nNumberOfBytesToRead,
  LPDWORD lpNumberOfBytesRead,
  LPOVERLAPPED lpOverlapped
);

The number of bytes to read should not be greater than the size of the buffer, but AFAIK specifying a number greater than the size of the file is no problem. The function will read to the end of the file and stop. If you need to know the size of the file, use GetFileSize or GetFileSizeEx.
eschew obfuscation

Nilrem

Already done that, forgot to update sorry, my code is as follows but it is still blank:
LOCAL lpstring[128]:DWORD; Used for input()
LOCAL buffer[128]:BYTE; Used for putting strings together
LOCAL buffer2[128]:BYTE
LOCAL buffer3[512]:BYTE
invoke CreateFile, ADDR CurrentFile, GENERIC_READ, FILE_SHARE_READ,
        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL;
       
        mov hFile, eax; Move the handle to hFile for later use with ReadFile
        invoke GetFileSize, hFile, NULL; Get the size of the file
        mov FileSize, eax; Set the FileSize
        invoke GetLastError; Get the return value of CreateFile
        xor ebx,ebx; Set ebx to 0
        mov ebx,eax; Move the value of GetLastError to ebx
        .if ebx != 0; If the file does not exist
        strcat ADDR buffer, ADDR CurrentFile, ADDR OpenFileError; Put the strings together
        invoke StdOut, addr buffer; Print the new combined string to the screen
        mov lpstring, input(); Wait for user input
        ret
    .endif
   
    invoke ReadFile, ADDR hFile, ADDR buffer2, ADDR FileSize, ADDR Numb, NULL; Read the file
    invoke StdOut, ADDR buffer2
    invoke CloseHandle, ADDR hFile; Close the file
    mov lpstring, input (); Wait for user input
        ret

Relvinian

Quote from: Nilrem on January 26, 2005, 08:37:32 AM
Already done that, forgot to update sorry, my code is as follows but it is still blank:
LOCAL lpstring[128]:DWORD; Used for input()
LOCAL buffer[128]:BYTE; Used for putting strings together
LOCAL buffer2[128]:BYTE
LOCAL buffer3[512]:BYTE
invoke CreateFile, ADDR CurrentFile, GENERIC_READ, FILE_SHARE_READ,
        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL;
       
        mov hFile, eax; Move the handle to hFile for later use with ReadFile
        invoke GetFileSize, hFile, NULL; Get the size of the file
        mov FileSize, eax; Set the FileSize
        invoke GetLastError; Get the return value of CreateFile
        xor ebx,ebx; Set ebx to 0
        mov ebx,eax; Move the value of GetLastError to ebx
        .if ebx != 0; If the file does not exist
        strcat ADDR buffer, ADDR CurrentFile, ADDR OpenFileError; Put the strings together
        invoke StdOut, addr buffer; Print the new combined string to the screen
        mov lpstring, input(); Wait for user input
        ret
    .endif
   
    invoke ReadFile, ADDR hFile, ADDR buffer2, ADDR FileSize, ADDR Numb, NULL; Read the file
    invoke StdOut, ADDR buffer2
    invoke CloseHandle, ADDR hFile; Close the file
    mov lpstring, input (); Wait for user input
        ret



invoke ReadFile, ADDR hFile, ADDR buffer2, ADDR FileSIze, ADDR Numb, NULL;   Read the file


This line of code is incorrect for ReadFile API function.  According to the API:

BOOL ReadFile(
  HANDLE hFile,
  LPVOID lpBuffer,
  DWORD nNumberOfBytesToRead,
  LPDWORD lpNumberOfBytesRead,
  LPOVERLAPPED lpOverlapped


You should be passing the number of bytes to read -- NOT the address of the number of bytes. Same goes for your handle

A correct invoke statements is as follows for your code:

invoke ReadFile, [hFile], ADDR buffer2, [FileSize], ADDR Numb, NULL;   Read the file


Hope this helps

Relvinian

Ic3D4ne

Your printing program could be caused by you not using "Console Assemble".

Happened to me, just a wild guess.

-Ic3D4ne

Nilrem

Is it possible for that being the problem if StdOut works elsewhere. Could you possible, if you have the free time, compile the code and create the textfile and see if it works, could be a compiler issue on my end. Thankyou.

Relvinian

Nilrem,

Do you have a current link or code that you would like me to compile and help you out with?  If so, let me know and I'll be glad to help you out.

Relvinian

Polizei

Quote from: All_Of_YOU


invoke ReadFile, ADDR hFile, ADDR buffer2, addr FileSize, ADDR Numb, NULL; Read the file


Oh, guys ... Don't you see that the PROBLEM (problem ???) is in the hFile !!!
You should not pass the ADDRESS of the handle, you must pass the HANDLE !!!
According to the :

BOOL ReadFile(
  HANDLE hFile,
  LPVOID lpBuffer,
  DWORD nNumberOfBytesToRead,
  LPDWORD lpNumberOfBytesRead,
  LPOVERLAPPED lpOverlapped
);


The correct code is :

invoke ReadFile, hFile, addr buffer2, FileSize, addr Numb, NULL


Regards ...

Nilrem

#13
Ok thanks a lot guys, I will fix it when I get the chance, my next question is the deliminator? I remember reading about it a while ago, reading a text file and excluding certain data. I'll make a new thread for that, in the meantime any resources already out there covering it?
Cheers.