Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a piece of code that writes sectors to a physical disk using WriteFile. This works fine under Win XP, but not under Windows 7 (Not tried Vista). It returns an error code of ERROR_ACCESS_DENIED. Having read http://msdn.microsoft.com/en-us/library/aa365747%28VS.85%29.aspx[^] I can see that I should be dismounting or locking the volumes associated with the physical device. I added code to enumerate the volumes and called DeviceIoControl with FSCTL_LOCK_VOLUME. Now when I call WriteFile I get the error code ERROR_DEVICE_CHANGED, even if I repeat the WriteFile call.

What should I be doing?
Posted

Vista and Windows 7 don't let you write to physical disks or partitions that have mounted file systems on, even as an administrator.

After locking the drive you have to dismount it and get rid of the DOS device. To dismount the drive use DeviceIoControl with FSCTL_DISMOUNT_VOLUME. To get rid of the DOS device (i.e. the drive letter) call DefineDosDevice with DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE.

Hope that helps,

Cheers,

Ash
 
Share this answer
 
Comments
Sauro Viti 28-Jul-10 5:19am    
Reason for my vote of 5
Very useful information
Anorexic Tribble 28-Jul-10 6:17am    
Tried DefineDosDevice on "E"

Also tried DeleteVolumeMountPoint

But still getting ERROR_DEVICE_CHANGED
Aescleal 28-Jul-10 7:07am    
it sounds like you've still got something with a mounted file system on there. If you've done the three things I outlined (lock the volume, dismount it, remove DOS device) for each volume on the physical disk then I can't think of a lot else to try, sorry!

[I know, don't you hate it when people tell you "It works for me..."]
Did you try running it by right clicking on the EXE and select "Run as administrator"?
 
Share this answer
 
Comments
Anorexic Tribble 28-Jul-10 6:15am    
Yes I am running as admin
Member 7894601 6-Jan-15 23:25pm    
I have a question.Please help me.Are you trying using the disk number or drive letter to createFile
Anorexic Tribble 10-Jan-15 7:38am    
I use both disk numbers and volume letters. What is your question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900