Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've written a pro bono MFC (C++) program for a music teachers organization that copies mp3 files from the hard drive to a Digital Audio Player (DAP). The program uses a thread that contains the three functions SetFileAttributes, DeleteFile, CopyFile. I used Visual C++ 6.0 on Windows XP 32-bit to compile.

The program works perfectly fine in Win XP (32-bit) but returns Access Denied for the above three functions under Windows 7 and 8. Can someone tell me why? All help is much appreciated.
Posted
Updated 3-Jun-14 5:27am
v3
Comments
Nelek 3-Jun-14 11:28am    
Windows 7 and 8 are 32bit as well? Are you running it as administrator?
Adam Zgagacz 3-Jun-14 12:30pm    
What is location of the files on the hard drive? Perhaps if you publish piece of code it would help? Waht method gives access denied?
Stefan_Lang 4-Jun-14 2:53am    
In addition to the already available solution, to understand them you should be aware that the way data and program files are organized changed dramatically from Windows XP to Vista and beyond. While XP allowed you to create and write files pretty much everywhere on your disk, Vista and newer versions won't normally allow you to create and write files in locations that are reserved for program installations, system files, or other user accounts.

You have no write rights to the place where you want to write. The right solution is to copy the files to another location. The MSDN has some nice documentation how to solve it.

The "Quick and Dirty" way is to run your app as admin
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Jun-14 13:45pm    
5ed. I put some different detail in my answer.
—SA
JekyllAndHyde 8-Jun-14 14:22pm    
Thanks for your response Sergei! I don't understand why I would not have write permission. There is only one user on either Windows 7 or 8 computer and I assume they default to Administrator. I am copying files from My Documents to the root folder of a USB Digital Audio Player! I also don't understand what you mean by "copy the files to another location" and "not legitimate" directories. Is there a way for me to upload a two page PDF file that shows a brief description of the application and relevant parts of my code?
Sergey Alexandrovich Kryukov 8-Jun-14 20:45pm    
Maybe you don't understand such security feature of Windows 7 and 8 (introduced in Vista) as User Access Control (UAC). Giving access to the administration group is not enough. A use, even the administrator, has also to execute some application under the elevated privileges.

Read my answer more thoroughly, and also read this:
http://en.wikipedia.org/wiki/User_Account_Control.

"Legitimate directories" is something you need to understand really well to develop for Windows. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762204%28v=vs.85%29.aspx.

Does it make your problem clear now? Please follow my advice in Solution 2. Will you accept it formally?


—SA
First of all, try to execute your application under elevated privileges and see if there is any difference:
http://www.sevenforums.com/tutorials/11841-run-administrator.html[^].

If the problem is the permission, you can do some file system administration to give the user appropriate permissions to the files in question. Or you can embed the manifest requesting the elevation of privileges from the very beginning. This might be the bad solution not addressing the root of the problem.

It's possible that you are using wrong directories with your application, not legitimate ones. It does not mean that you did something legitimate for XP and not legitimate for Windows 7 and 8. It means that your application was wrong in first place, but XP was too forgiving. If someone tightened the permission in XP, your solution would not work. It did not really guaranteed to work on all systems. It often happens when you use some hard-coded path names in your code. There are no situations when this could be useful. You should either put responsibility for file location on the user (take from user input or some configuration files) or use proper directories associated with the user accounts or reserved for "all users". In all cases, you calculate the locations (paths) during runtime, never hard-code them.

[EDIT]

Please read about "special folders":
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762204%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx[^].

You should not assume the user can use any arbitrary directories, but the paths returned by the function shown above will point you to the legitimate directories you can always use. Some are set to be read-write to the currently logged on user, some are read-only, but they are supposed to be always accessible.

—SA
 
Share this answer
 
v2

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