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

Posted by ile on Stack Overflow See other posts from Stack Overflow or by ile
Published on 2011-01-05T09:49:26Z Indexed on 2011/01/05 9:53 UTC
Read the original article Hit count: 227

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

© Stack Overflow or respective owner

Related posts about c++

Related posts about windows-7