开发者

Cannot access an NTFS volume via CreateFile

I am attempting to add USB key passthrough functionality to a piece of software that is similar in functionality to Remote Desktop. I have the following code:

public void Open(string path)
{
    System.OperatingSystem ver = System.Environment.OSVersion;
    uint mode;
    uint attr;
    uint share;
    int junk = 0;
    bool success;
    string partitionFileName = @"\\.\" + path;
    uint drvtype;

    // Only determine drvtype if not already defined
    // Default drytype is DRIVE_UNKNOWN
    if (drvtype == Kernel32.DriveType.Unknown)
    {
        drvtype = Kernel32.GetDriveType(path); //0=unknown, 1 = no root drive, 2= removable, 3=fixed, 4=remote, 5=cdrom, 6=ramdisk
    }

    // Set the access modes depending on what we are opening
    if (drvtype == Kernel32.DriveType.CDROM)
    {
        // Depending on the OS version, the flags for opening the cdrom for ioctls are different
        if (ver.Version.Major == 4)    /开发者_如何学编程/4 == Windows NT
        {
            mode = Kernel32.GENERIC_READ;
            share = Kernel32.FILE_SHARE_READ;
            attr = Kernel32.FILE_ATTRIBUTE_NORMAL;
        }
        else
        {
            mode = Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE;
            share = Kernel32.FILE_SHARE_READ | Kernel32.FILE_SHARE_WRITE;
            attr = Kernel32.FILE_ATTRIBUTE_NORMAL;
        }
    }
    else
    {
        mode = Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE;
        share = Kernel32.FILE_SHARE_READ | Kernel32.FILE_SHARE_WRITE;   // exclusive access to device
        attr = Kernel32.FILE_ATTRIBUTE_NORMAL;
    }

    // Try to open the partition as read/write
    hFile = Kernel32.CreateFile(partitionFileName,
                      mode,
                      share,
                      IntPtr.Zero,
                      Kernel32.OPEN_EXISTING,
                      attr,
                      IntPtr.Zero);

    // Lock the volume
    Kernel32.DeviceIoControl(hFile, Kernel32.EIOControlCode.FsctlLockVolume, IntPtr.Zero, 0, IntPtr.Zero, 0, ref junk, IntPtr.Zero);

    // Dismount from the host OS to get around Vista's security
    Kernel32.DeviceIoControl(hFile, Kernel32.EIOControlCode.FsctlDismountVolume, IntPtr.Zero, 0, IntPtr.Zero, 0, ref junk, IntPtr.Zero);

    // Allow extended access to get at boot sectors
    Kernel32.DeviceIoControl(hFile, Kernel32.EIOControlCode.FsctlAllowExtendedDasdIo, IntPtr.Zero, 0, IntPtr.Zero, 0, ref junk, IntPtr.Zero);

    // If it's a USB disk, see if it is write-protected
    if (drvtype == Kernel32.DriveType.Removable)
    {
        // If it is write-protected, tell the user
        success = Kernel32.DeviceIoControl(hFile, Kernel32.EIOControlCode.DiskIsWritable, IntPtr.Zero, 0, IntPtr.Zero, 0, ref junk, IntPtr.Zero);
        if (success)
        {
            bWriteProtect = false;
        }
        else
        {
            if (Kernel32.GetLastError() == 19) //ERROR_WRITE_PROTECT
            {
                bWriteProtect = true;
                showStatus("Unable to open as read/write. File opened as read-only.");
            }
            else
            {
                bWriteProtect = false;
            }
        }
    }
}

I then read from the disk with ReadFile(). It works great for a FAT-formatted USB key, I can read/write and even boot from it. For some reason however, it does not work with NTFS-formatted keys. Calling ReadFile() throws error 21, ERROR_NOT_READY, which some digging has found is because the volume is unmounted. I cannot leave the volume mounted, because I am accessing (and possibly writing to) the boot sectors on the drive, and from what I understand, Vista/7 will not allow you to do that without first unmounting the drive.

Are there some other DeviceIoControl calls I am supposed to be making for NTFS that I'm missing?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜