I have an old c++ application that needs to be modified to work with windows 7. Problem is in creating a new f开发者_如何学Colder and saving a file in that folder. This folder should be created in
c:\program files\myApp\data\newFolder.
This is function I use to create new folder and get errors:
if(!CreateDirectory(pathSamples,NULL)) //Throw Error
{
DWORD errorcode = GetLastError();
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL );
MessageBox(NULL, (LPCTSTR)lpMsgBuf, TEXT("Error"), MB_OK);
}
In XP this works, but in Windows 7 it doesn't. If I run application as administrator than the folder is created, otherwise "Access is denied" error is thrown.
My question is following:
Is there an option to make changes to the code so that the folder can be created in "program files" nad that files can be saved in this folder?PS I saw this thread already but it doesn't answer my question.
Thanks,
IlijaYou have answered your own question. You need to be an administrator to write under Program Files in Windows 7.
Application data goes in a different area under Users//AppData etc...
You can always use the registry to select the location to write, so you can use the old area on XP and the new area on Vista and Windows 7.
With limited user access under Vista and later you don't want to be trying to put files in "Program Files" or any other non-standard place. You should really be using SHGetFolderPath to obtain the correct location from the system.
As others already wrote, %ProgramFiles% is not the right place to store user data. The correct solution obviously is to redesign the application so that it uses a different storage location.
As an alternative there exists a quick and dirty (!) fix: If the application does not have a manifest, User Account Control Data Redirection kicks in, transparently redirecting write requests to system areas to a safe place in the user profile. The redirection target is %LocalAppData%\VirtualStore\Program Files. Details about this kind of built-in virtualization can be found here.
So you could be done by simply removing the manifest from your application.
As @CashCow writes:
You need to be an administrator to write under Program Files in Windows 7.
Best way to do this is to elevate your process (using ShellExecute "runas"
or similar), and then create the folder.
Some ShellExecute
examples:
- http://en.wikipedia.org/wiki/User_Account_Control#cite_ref-kennykerr_4-2
- http://blogs.msdn.com/b/vistacompatteam/archive/2006/09/25/771232.aspx
- http://weblogs.asp.net/kennykerr/archive/2006/09/29/Windows-Vista-for-Developers-1320-Part-4-1320-User-Account-Control.aspx
It looks like it was enough to set permissions for this folder in installer and now it works normally.
Thanks everyone for your answers!
精彩评论