Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ReadFileEx

15 views
Skip to first unread message

Jim Fell

unread,
Feb 3, 2003, 11:22:45 PM2/3/03
to
I'm having a little trouble getting ReadFileEx and
WriteFileEx to work, and I was wondering if someone could
tell me what I'm doing wrong. When I run the program I
get a 'Memory could not be read.' error, and when I debug,
it it crashes on the ReadFileEx method. However, it does
not appear to be creating the handle in the CreateFile
method (sfile value does not change), but GetLastError
says that it completed succesfully.
Any suggestions? Thanks in advance!
-Jim

char *sfilename, *dfilename;
LPBYTE buffer[3];
HANDLE sfile , dfile;
LPOVERLAPPED olOne = {NULL};
LPOVERLAPPED olTwo = {NULL};
LPOVERLAPPED_COMPLETION_ROUTINE ocr = {NULL};

if(sfilename != NULL)
{
CreateFile
(sfilename,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVER
LAPPED,sfile);
DWORD err = GetLastError();
if(!ReadFileEx(sfile,buffer,2,olOne,ocr))
return false;
}
if(dfilename != NULL)
{
CreateFile
(dfilename,GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_FLAG_OVERL
APPED,dfile);
if(!WriteFileEx(dfile,buffer,2,olTwo,ocr))
return false;
}

David Lowndes

unread,
Feb 4, 2003, 2:57:15 AM2/4/03
to
>I'm having a little trouble getting ReadFileEx and
>WriteFileEx to work, and I was wondering if someone could
>tell me what I'm doing wrong.

> CreateFile
>(sfilename,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVER
>LAPPED,sfile);

Try this instead:

sfile = CreateFile
(sfilename,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVER
LAPPED,NULL);

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq

Scott McPhillips

unread,
Feb 4, 2003, 8:03:31 AM2/4/03
to

All of your pointer allocations (char*, LP...) are incorrect. You are
allocating pointers containing garbage instead of allocating an object
and passing its address.

char sfilename[_MAX_PATH];
BYTE buffer[3];
OVERLAPPED olOne = {0};

The CreateFile result is returned by the function, but you are passing
sFile as a parameter.

sfile = CreateFile(....);

You are not passing the name of the file anywhere. Also if(sfilename !=
NULL) is testing the value of an uninitialized pointer, i.e. garbage.

--
Scott McPhillips [VC++ MVP]

Jim Fell

unread,
Feb 4, 2003, 11:29:11 AM2/4/03
to
Thanks for the help so far! I took your suggestions, but
I'm still getting the 'Memory could not be "read".' error.
Any other ideas? Thanks!
-Jim

char sfilename[60]; // source file path
char dfilename[60]; // destination file path
BYTE buffer[3];
HANDLE sfile , dfile; // source & destination file handles

LPOVERLAPPED olOne = {0};
LPOVERLAPPED olTwo = {0};
LPOVERLAPPED_COMPLETION_ROUTINE ocr = {0};

if(sfilename != NULL) // was path passed to method?
{


sfile = CreateFile
(sfilename,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVER
LAPPED,NULL);

DWORD err = GetLastError();
if(!ReadFileEx(sfile,&buffer,2,olOne,ocr))
return false;
}
if(dfilename != NULL) // was path passed to method?
{
dfile = CreateFile
(dfilename,GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_FLAG_OVERL
APPED,NULL);
if(!WriteFileEx(dfile,&buffer,2,olTwo,ocr))
return false;
}

Scott McPhillips

unread,
Feb 4, 2003, 9:16:13 PM2/4/03
to

Jim, you have pretty much the same kind of errors as before. You don't
seem to have a basic understanding of pointers, which are crucial in
most C/C++ programming.

In the case of the sfilename, it will never be NULL because it refers to
the address of the char array. I'm hesitant to suggest a fix because
you haven't shown anything about what is "passed to method". You need
to know what it being passed.

In the case of the OVERLAPPED structure you are allocating an
LPOVERLAPPED, which is only a pointer to an OVERLAPPED, which is the
same error you had before. You need to allocate an OVERLAPPED. Then
you pass &olOne (note the & address-of operator). The address of an
OVERLAPPED is an LPOVERLAPPED. In plain English, Windows needs you to
pass the address of something, but you are not creating the something.

0 new messages