Re: Why SetFilePointerEx can't accept a negative number as the distance offset?

Tech-Archive recommends: Fix windows errors by optimizing your registry



You can't. For all practical purposes, the *concept* does not even exist. Since it makes
no sense to allow this (you don't even know what the last sector is, and given dynamic
remapping, RAID drives, virtual drives, etc.) there is no way to even *identify* what the
last sector is! You can only do this if you have privileged access to the physical drive,
and you don't have that.

I have no idea why this could possibly make sense, even if it were possible, but no sane
operating system would ever permit this sort of thing to be done. It would be a truly
massive security hole. So it isn't going to happen.
joe
On Fri, 07 Sep 2007 21:27:28 -0700, xmllmx <xmllmx@xxxxxxxxx> wrote:

Thank you, Joseph.

My purpose is simple: I want to read the last sector of the drive.

All of the first three posts are mine. Maybe you just saw my first
post.

My second post:
==================
"Even if I change the value -512 to 0, the call to SetFilePointerEx
still fails.

How weird it is!"
==================


My third post:
==============
"PS.

The error code returned by SetFilePointerEx is 0x87
(ERROR_INVALID_PARAMETER)."
==================

CreateFile is successful. And I can sucessfully call SetFilePointerEx
with positive value such as 512, 1024, etc. As long as the offset is
positive and sector-aligned, the call will always succeed!

For example, the following code is OK.

========== Code =============
HANDLE hDrive = CreateFile(L"\\\\.\\C:", GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING,
NULL, NULL);
LARGE_INTEGER i64;
i64.QuadPart = 512; // must be a whole number multiple of the volume's
sector size
SetFilePointerEx(hDrive, i64, 0, FILE_BEGIN); // OK!
CloseHandle(hDrive);
========== Code =============

Moreover, in some case, I can use a negative value successfully. See
below:

i64.QuadPart = 512; // must be a whole number multiple of the volume's
sector size
SetFilePointerEx(hDrive, i64, 0, FILE_BEGIN); // OK!

/*
Now the current file pointer points to 512, so I can move the file
pointer backward.
*/
i64.QuadPart = -512;
SetFilePointerEx(hDrive, i64, 0, FILE_CURRENT); // OK!

From the experiments above, we can see that SetFilePointerEx() has its
meaning. I think a drive can be treated as an ordinary file.
GetFileType() will return FILE_TYPE_DISK, which means the drive is
seeking device rather than nonseeking device, so we can call
SetFilePointerEx on the drive.

After I rephrase my problem, my original problem is still unsolved:

i64.QuadPart = -512;
SetFilePointerEx(hDrive, i64, 0, FILE_END); // This time the call will
still return 0x87

I'm still awaiting help for this. Thanks.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Why SetFilePointerEx cant accept a negative number as the distance offset?
    ... It could be that files opened to the whole partition don't have "size" ... Even though we don't know in the physical hardware which sector is "last" ... remapping, RAID drives, virtual drives, etc.) there is no way to even ... "Even if I change the value -512 to 0, the call to SetFilePointerEx ...
    (microsoft.public.vc.mfc)
  • Re: Why SetFilePointerEx cant accept a negative number as the distance offset?
    ... The original poster has permission to open the entire partition and read/write the entire partition, so an attempt to read the last sector of the partition is not a security hole. ... remapping, RAID drives, virtual drives, etc.) there is no way to even *identify* what the ... "Even if I change the value -512 to 0, the call to SetFilePointerEx ...
    (microsoft.public.vc.mfc)
  • Re: MD/RAID time out writing superblock
    ... thread from people with somewhat different hardware and drives to ours. ... I/O error, dev sde, sector 1465147264 ... ata1: ... I/O error, dev sda, sector 1465147272 ...
    (Linux-Kernel)
  • vinum limits disk size to 255MB
    ... I'm trying to set up vinum on a freshly installed FreeBSD 5.3-BETA7 ... drives to create a vinum volume. ... BIOS sector numbering starts with sector 1 ... The data for partition 1 is: ...
    (freebsd-questions)
  • Re: laptop WITHOUT bottom fan
    ... You overlook the fact that when an IDE drive detects a damaged sector ... failure once the stock is exhausted. ... We would like to use hard disk drives as data recorders for aircraft ... never found tape drives to be very reliable storage medium. ...
    (comp.sys.laptops)