C++ Learning Community Forum
August 01, 2010, 03:12:59 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Problem with ReadFile and WriteFile  (Read 1335 times)
biggoron
C++ Freak
***
Posts: 351


View Profile
« on: June 15, 2008, 12:30:43 AM »

Code
GeSHi (asm):
       ; Create both input and output handles
       invoke  CreateFile, inputFileName, GENERIC_READ, FILE_SHARE_READ, NULL,\
                           OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
       cmp     eax, INVALID_HANDLE_VALUE
       je      .fail
       mov     [hInput], eax
 
       invoke  CreateFile, outputFileName, GENERIC_WRITE, 0, NULL,\
                           CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
       cmp     eax, INVALID_HANDLE_VALUE
       je      .fail
       mov     [hOutput], eax
 
       ; Do stuff
       invoke  ReadFile, hInput, line, 256, numBytesRead, NULL
       invoke  WriteFile, hOutput, line, numBytesRead, numBytesWritten, NULL
 
       invoke  CloseHandle, hInput
       invoke  CloseHandle, hOutput
Created by GeSHI 1.0.7.18

The opening of both files appears to work fine, but when reading nothing is read and ReadFile returns FALSE. The only thing I'm unsure about in that code is numBytesRead and numBytesWritten. Should I be declaring them like:
Code
GeSHi (asm):
numBytesRead dd ?
numBytesWritten dd ?
Created by GeSHI 1.0.7.18
or:
Code
GeSHi (asm):
numBytesRead: dd ?
numBytesWritten: dd ?
Created by GeSHI 1.0.7.18
So that numBytesRead/Written point to the dword?
Logged

adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #1 on: June 16, 2008, 12:55:17 AM »

You forgot to dereference the handles in the Read and WriteFile calls, and also the third param to WriteFile.
Logged

biggoron
C++ Freak
***
Posts: 351


View Profile
« Reply #2 on: June 16, 2008, 01:17:50 AM »

=O that did it. Why did I have to dereference the handles... hInput and hOutput were the handles themselves, not pointers to them. *shrug*
Oh well, that did it! Thanks again, adeyblue!
« Last Edit: June 16, 2008, 01:21:41 AM by biggoron » Logged

adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #3 on: June 16, 2008, 04:49:17 PM »

The names you give global variables are pointers to where the actual dd or db etc are stored. So if you just printed hInput or hOutput you'd see the address of the space declared (for example 0x004004C8). Obviously if you just pass that to Read/WriteFile you'd mostly get error since 0x004004C8 is unlikely to be a valid handle value. It works for strings in things like printf because they use the address of the first character, so no dereference is necessary.

Everything you declare globally is just the address of an offset within the image to where the actual space is, if that makes sense. I'm sure someone will starch my pants if I'm wrong, I'm basically a beginner too.
Logged

biggoron
C++ Freak
***
Posts: 351


View Profile
« Reply #4 on: June 16, 2008, 06:35:39 PM »

Oh, I thought that was only if I made a label like "variable: db 0". I got it now Smiley
Logged

FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #5 on: July 04, 2008, 10:56:13 AM »

You forgot to refrence hInput and hOutout as pointers in your readfile and writefile ruteens.

Code:
       invoke  ReadFile, [hInput], line, 256, numBytesRead, NULL
        invoke  WriteFile, [hOutput], line, numBytesRead, numBytesWritten, NULL

however if hInput and hOutput are handles themselves then you shouldn't need to place them in brackets when when you move eax into them.
Logged


Imagine the impossible, then make it happen.
biggoron
C++ Freak
***
Posts: 351


View Profile
« Reply #6 on: July 04, 2008, 09:26:50 PM »

Guy, check the dates. In the time between the post before yours, the problem was solved, the project was finished, I started about five other ASM projects, then lost interest in ASM Tongue
« Last Edit: July 04, 2008, 09:28:24 PM by biggoron » Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!