I am having trouble getting ReadFile to work for me.
I am runnng an AMD with XP SP1. I compile with a .cpp
extention normally using Borland Bcc32 but I have
also compiled using cl.exe.

I declare LPVOID lpBuffer and have tried assigning
memory using malloc. I also tried GlobalAlloc. Using
GlobalAlloc, I have tried GMEM_FIXED, GPTR and GHND.

I have also tried GlobalLock with lpBuffer in the
() to reassign lpBuffer from a handle (hmem) to a
pointer as described in WIN32.HLP. All failures
crash at the same point.

My CreateFile works fine and returns a good handle.
CreateFile sucessfully loads the file location into
memory. GetLastError tells me that CreateFile worked
and my memory allocation has been successful. I also
tried a call to GetFileSize which worked fine before
calling ReadFile.

Tracking what happens in SoftIce, all is well until
the ReadFile function calls a tick count. Before the
tick count, lpBuffer "seems" to be pointing to a good
location. On return from the tick count, EBP+0x14
contains zero. This is later used as a memory
address which naturally fails.

I'm at a loss to see what I'm doing wrong. Any
ideas?

Hello toolmanx,

Two things

My CreateFile works fine and returns a good handle.
CreateFile sucessfully loads the file location into
memory. GetLastError tells me that CreateFile worked
and my memory allocation has been successful. I also
tried a call to GetFileSize which worked fine before
calling ReadFile.

1) Never call GetLastError if the API is sucessful. Call it only when API fails and it is documented that GetLastError will give you right error value.

2) Give a small code sinppet which can demonstrate how are u using ReadFile.

ReadFile is a very widely used API in Windows. Most likely it should not "CRASH".

Of course posting actual code rather than just describing it would really help.

This is one of my many attemps. I have brought down most of my
declarations so you can see them.

HANDLE hfile;           
HGLOBAL hmem;
LPDWORD dwRead = (LPDWORD)0;
LPVOID lpBuffer;
LPDWORD lpFileSizeHigh; 
DWORD Mylength;

hfile = CreateFile(lpstrFile,GENERIC_READ,
        FILE_SHARE_READ,(LPSECURITY_ATTRIBUTES)NULL, 
        OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);

Mylength = GetFileSize(hfile,lpFileSizeHigh);

lpBuffer = GlobalAlloc(GPTR,Mylength);

ReadFile(hfile,lpBuffer,Mylength+1,dwRead,NULL);

Try this

int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hfile;           
    HGLOBAL hmem;
    DWORD dwRead = 0;
    LPVOID lpBuffer;
	
    DWORD lpFileSizeHigh; 
    DWORD Mylength;

	hfile = CreateFile(TEXT("D://Sample.txt"),GENERIC_READ,
            FILE_SHARE_READ,(LPSECURITY_ATTRIBUTES)NULL, 
            OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);

    Mylength = GetFileSize(hfile,&lpFileSizeHigh);

    lpBuffer = GlobalAlloc(GPTR,Mylength);

    ReadFile(hfile,(BYTE *)lpBuffer,512,&dwRead,NULL);            
    
	return 0;
}

In your case LPDWORD dwRead = (LPDWORD)0; is the culprit. ReadFile(hfile,lpBuffer,Mylength+1,dwRead,NULL); tries to write in a variable which has no size. you need to allocate some size to dwRead before you pass it to ReadFile.

Notice the small change I did in your code.

Yes, you seem to be declaring lots of variables of the right type (as far as the called functions are concerned), but you've missed the point that all those pointers (the LP... variables) all need to point to somewhere.

dubeyprateek has already pointed those out.

What you also need to do is stop lying to the routines about how much memory they're allowed to use. ReadFile(hfile,lpBuffer,Mylength+1,&dwRead,NULL); You allocated Mylength, so say Mylength, not Mylength+1.
Functions are only safe if you're honest about the size they're allowed to work with.

That did it. Thanks.

I put MyLength+1 to make sure I don't run over
my allowed memory. Doesn't that allow room for end of file?

> Doesn't that allow room for end of file?
Using a character to denote the end of the file was an obsolete idea when it was first invented, and that was in the 1970's.

If the file is 10 bytes long, you allocate 10 bytes, you read 10 bytes, and therefore you have the WHOLE file in memory. That's all there is to it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.