Search code examples
c++windows-7directoryprogram-files

create a folder and files in c:\program files\myApp\data in windows 7


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


Solution

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

    Thanks everyone for your answers!