Windows File I/O Reading
        Posted  
        
            by 
                eyeanand
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by eyeanand
        
        
        
        Published on 2013-10-27T03:51:39Z
        Indexed on 
            2013/10/27
            3:53 UTC
        
        
        Read the original article
        Hit count: 146
        
Currently working on open/read images in VC++.
Some examples i came across on the internet use Windows.h I/O routines like ReadFile...but there seems to be inconsistency in there declaration.
Here's what i have got.
//So i have this function to load file
BYTE* LoadFile ( int* width, int* height, long* size, LPCWSTR bmpfile )
{
  BITMAPFILEHEADER bmpheader;
  BITMAPINFOHEADER bmpinfo;
  DWORD bytesread = 0;
      HANDLE file = CreateFile ( bmpfile , GENERIC_READ, FILE_SHARE_READ,NULL, 
                              OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL );
  if ( NULL == file )
    return NULL;
  if ( ReadFile ( file, &bmpheader, sizeof ( BITMAPFILEHEADER ),&bytesread, 
                      NULL ) == false )
  {
    CloseHandle ( file );
    return NULL;
  } 
 .
 .
 .
 return appropriate value;
}
Now the ReadFile API function is declared as follows in WinBase.h
WINBASEAPI BOOL WINAPI ReadFile( In HANDLE hFile, Out LPVOID lpBuffer, In DWORD nNumberOfBytesToRead, _Out_opt_ LPDWORD lpNumberOfBytesRead, _Inout_opt_ LPOVERLAPPED lpOverlapped );
And in MSDN examples... They call this function like this.
ReadFile(hFile, chBuffer, BUFSIZE, &dwBytesRead, NULL)
Which expects that "bytesRead" is sort of out parameter. so it gives me number of bytes read.
But in my code ..it is giving error message. 'ReadFile' : cannot convert parameter 4 from 'LPDWORD *' to 'LPDWORD'
so i just initialized bytesRead to 0 and passed by value.( which is wrong..but just to check if it works ).
then it gives this exception
Unhandled exception at 0x774406ae in ImProc.exe: 0xC0000005: Access violation 
writing location 0x00000000.
Kindly suggest .
Kindly tell if any code i missed out....including while forming the question itself.
Thanks.
© Stack Overflow or respective owner