locked
WriteFile() returned with ERROR_ACCESS_DENIED, What happened in Vista? RRS feed

  • Question

  • Hello, I'm working on porting one program from XP to Vista.
    This program write datas into sectors directly using WriteFile/ReadFile functions.
    like this example in MSDN (http://msdn2.microsoft.com/en-us/library/aa363147.aspx)

     

    Code Snippet

    // Open disk device
    HANDLE hDevice = CreateFile(
     "\\\\.\\PhysicalDrive0",
     GENERIC_READ | GENERIC_WRITE,
     FILE_SHARE_READ | FILE_SHARE_WRITE,
     NULL,
     OPEN_EXISTING,
     0,
     NULL
     );

     

    // Select sector to write into
    SetFilePointer(hDevice, pos.LowPart, &pos.HighPart, FILE_BEGIN);

     

    // Write data
    bResult = WriteFile(hDevice, buffer, 512, &bytesWritten, 0);

     
    On XP it works well. but, on Vista it does NOT.
    On Vista ReadFile works. but, WriteFile returns ERROR_ACCESS_DENINED
    when I try to write into a sector after 2048.(before 2048, WriteFile works)


    I've seen this thread(http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=893093&SiteID=1) about same problem.
    My Wendell said that writing driver can be a solution for this. so, I also making a filter driver.
    But driver's IRP also returned with error: STATUS_ACCESS_DENIED(0xC0000022).  I'm testing with set UAC off and "Run as Administrator"

     

    Can you tell me how can I solve this problem?
    Thanks in advance.

    Thursday, May 10, 2007 2:07 AM

All replies

  • i faced same problem with you.

    i think maybe write a Service Application will solve it~

     

    marking....

    Thursday, May 10, 2007 7:22 AM
  • Have you solove the problem?
    I am facing same issue, and my program is a service.
    I tried raise priviledge of it, but not work.
    Wednesday, June 27, 2007 5:23 PM
  •  

    Anybody solve this problem? I have the same problem too!
    Monday, October 1, 2007 10:22 AM
  • do them in driver

    Wednesday, October 3, 2007 3:51 AM
  • Hi there,

    I have been grappling with the same issue and have found the following under Win7 (XP is OK):

    You need to lock and then dismount the volume as per snippet below (LogicalDriveID in my case is 'L' drive: "\\\\.\\L:") :

    BUT - HUGE CAVEAT to this for removable media!!! (And I don't know why)

    What I have found is that if the media DOESN'T have a boot sector (i.e. The actual media's partition starts at LBA[0]) then this code DOES NOT WORK. It DOES however work for any media that does indeed have a boot sector at LBA[0] and a partition elsewhere (LBA[0x3F] typically). I found this out by copying a boot sector (LBA[0]) from other media that had one and copying it into LBA[0] of the one that did not have a BS. Windows then wanted to format it again, but left the BS intact and created the partition to suit - but with the size of the media I took the BS from. Only then did WriteFile() work. So for some reason, you need to have a BS on the media to be able to dismount the volume "successfully" so WriteFile() works (even though the snippet below succeeds in either case)

    Can anyone out there answer why this is???

        // Dismount partition
        LogicalDevice = CreateFile(LogicalDriveID,GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,NULL, OPEN_EXISTING, 0, NULL);   
        i = DeviceIoControl(LogicalDevice, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0,&ol, NULL);
        if(!i)
        {
            printf("\r\nFailed to lock volume (L). Error: %d",GetLastError());
        }   
        i = DeviceIoControl(LogicalDevice, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0,&ol, NULL);
        if(!i)
        {
            printf("\r\nFailed to dismount volume (L). Error: %d",GetLastError());
        }

    Friday, August 26, 2011 2:05 AM