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

ReadFile fails with error code 87

696 views
Skip to first unread message

amr123

unread,
Oct 9, 2009, 5:26:13 AM10/9/09
to

Hello,

I want to read the boot sector of my pen drive with Fat32 file system
on it. I use win32 API CreateFile() to open the drive, Set file pointer
to begin of drive and call ReadFile() to read 512 bytes.

ReadFile() fails with error code 87. (The parameter is incorrect)


Code:
--------------------

PBYTE byFatBootBlock[512];
hDrive = CreateFile(
_T("\\\\.\\H:"),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
0,
0
);
if (INVALID_HANDLE_VALUE == hDrive)
{
_tprintf(_T("CreateFile() failed.\n"));

return FALSE;
}

dwLowPtr = SetFilePointer(
hDrive,
0,
0,
FILE_BEGIN
);
if (INVALID_SET_FILE_POINTER == dwLowPtr)
{
_tprintf(_T("SetFilePointer() failed.\n"));

CloseHandle(hDrive);
return FALSE;
}

bRet = ReadFile(
hDrive,
byFatBootBlock,
512,
&dwBytesRead,
NULL
);
if (FALSE == bRet || 512 != dwBytesRead)
{
_tprintf(_T("ReadFile() failed.\n"));

CloseHandle(hDrive);
return FALSE;
}
--------------------


What is wrong in the parameters??
I am reading bytes in multiple of sector size, also the buffer is
aligned to 8 byte boundary.

Thanks.


--
amr123
Posted via http://ms-os.com Forum to Usenet gateway

Guido Franzke

unread,
Oct 9, 2009, 10:24:17 AM10/9/09
to
I think you posted in the wrong newsgroup. Ask again in
microsoft.public.vc.language


"amr123" <amr123...@news.home.local> schrieb im Newsbeitrag
news:amr123...@news.home.local...

Peter Foldes

unread,
Oct 9, 2009, 12:26:34 PM10/9/09
to
http://support.microsoft.com/kb/162623

--
Peter

Please Reply to Newsgroup for the benefit of others
Requests for assistance by email can not and will not be acknowledged.

"amr123" <amr123...@news.home.local> wrote in message
news:amr123...@news.home.local...

SPAMCOP User

unread,
Oct 9, 2009, 2:09:25 PM10/9/09
to

Wrong info Peter & wrong OS

--
SPAMCOP User

"Peter Foldes" <ok...@hotmail.com> wrote in message
news:umNb%230PSK...@TK2MSFTNGP02.phx.gbl...

Peter Foldes

unread,
Oct 9, 2009, 11:09:29 PM10/9/09
to
OK, so which is the right one according to you. The info is correct and applies in
the OP's case using XP

--
Peter

Please Reply to Newsgroup for the benefit of others
Requests for assistance by email can not and will not be acknowledged.

"SPAMCOP User" <spamcop_user@no_mail.haha> wrote in message
news:%23naZjuQ...@TK2MSFTNGP02.phx.gbl...

amr123

unread,
Oct 9, 2009, 11:59:43 PM10/9/09
to

Thanks for reply.

I got the solution. The problem was my pen drive had 2048 bytes per
sector and so ReadFile() failed as number of bytes to read was not
sector aligned.

I used DeviceIoControl() with IOCTL_DISK_GET_DRIVE_GEOMETRY flag to get
the value of bytes per sector and then called ReadFile() with this
value.

I hope this will be helpful to someone.

0 new messages