0

I have an old c++ application that needs to be modified to work with windows 7. Problem is in creating a new folder 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,
Ilija

Community
  • 1
  • 1
ilija veselica
  • 9,414
  • 39
  • 93
  • 147
  • i think you should try this by MSDOS commands like md foldername with path and these commands are run by your application – Badr Jan 05 '11 at 09:59
  • can you please be more specific or give me a code example? I am not really familiar with c++... Thanks! – ilija veselica Jan 05 '11 at 10:02

5 Answers5

3

You 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.

CashCow
  • 30,981
  • 5
  • 61
  • 92
3

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.

Roger Perkins
  • 678
  • 5
  • 7
  • See http://stackoverflow.com/questions/1615444/windows-standard-file-locations/1615619#1615619 for the APIs to use. (They differ between XP and Vista/Win7) – Macke Jan 05 '11 at 10:21
3

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.

Helge Klein
  • 8,829
  • 8
  • 51
  • 71
1

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:

Community
  • 1
  • 1
dalle
  • 18,057
  • 5
  • 57
  • 81
0

It looks like it was enough to set permissions for this folder in installer and now it works normally.

Thanks everyone for your answers!

ilija veselica
  • 9,414
  • 39
  • 93
  • 147