In my c++ application I’m rying to read iso file asynchronous by createfile – with overlapped flag and after it – readfile.
however when I try this code on a simple file (txt file for example) it works. but when I run this code on iso file – it fails.
I saw in MSDN that compressed file can only read by readfile sync calls. does iso files is in this category?
if yes – do you have other suggestion how to read iso files async?
this is my code:
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hFile;
DWORD NumberOfBytesRead = 0, dw;
BYTE *buf = (BYTE*)malloc(BUF_SIZE*sizeof(BYTE));
OVERLAPPED overlapped;
overlapped.Offset = overlapped.OffsetHigh = 0;
memset(buf, 0, 1024);
overlapped.hEvent = CreateEvent(NULL, true, false, NULL);
if(NULL == overlapped.hEvent)
printf("error");
hFile = CreateFile("xxx.iso",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING ,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
printf("invalid hfile\n");
int i;
i= ReadFile(hFile,
buf,
BUF_SIZE,
&NumberOfBytesRead,
&overlapped);
if( GetLastError() == ERROR_IO_PENDING)
{
dw = WaitForSingleObject(overlapped.hEvent, INFINITE);
if(dw == WAIT_OBJECT_0)
if (GetOverlappedResult(hFile,&overlapped,&NumberOfBytesRead, TRUE) != 0)
{
if (NumberOfBytesRead != 0)
{
printf("!!!\n");
}
}
}
thanks
You haven’t posted what value you’re using for the
BUF_SIZEconstant, but make sure it’s an integer multiple of the volume sector size. This is a common pitfall when using unbuffered file streams. The documentation forFILE_FLAG_NO_BUFFERINGin theCreateFile()documentation says:The page on file buffering notes:
On my system, this value is 4K and reading anything smaller than 4K at a time produces errors. In many of Microsoft’s code samples, 1K is the default buffer size, so adapting examples often leads to errors with unbuffered I/O.
Edit: also make sure to zero out all members of the
OVERLAPPEDstructure. You don’t set theInternalandInternalHighmembers to 0. Always clear theOVERLAPPEDstructure in the following manner:Then, you can set the file offset and event handle.
Edit: also consider the following note about the
lpNumberOfBytesReadparameter toReadFile():