0

I am using Windows 7 and I have to run one program in that windows but that program working in Windows XP. This is a Visual C++ program and I am using Visual Studio 2008 for this. When I am running my application, it does not throw any errors, but it does not create a directory in "c:\program files\". So can anyone help me to create directory and exe file?

This is the code I am using:

char szAppPath[MAX_PATH];
char szFileName[MAX_PATH];
DWORD dwResult;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;

dwResult = ExpandEnvironmentStrings( NULL, szAppPath, MAX_PATH);  // "%ProgramFiles%"


// do same for NSim directory
strcat(szAppPath,"\\NSim");
hFind = FindFirstFile(szAppPath, &FindFileData);

if (hFind == INVALID_HANDLE_VALUE) 
{
    //Directory Does't Exists create New
    if(!CreateDirectory(szAppPath,NULL))  //Throw Error
    {
        MessageBox("Unable to Create N-SIM directory","NSim Installer");
        return ;
    }
} 
else  
{
    //check if is directory or not
    if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
    {
        MessageBox("Can't Create N-SIM directory\n Another file with same name exists","NSim Installer");
        return ;
    }

    FindClose(hFind);
}

//***************************************N-SIM Application****************************
strcpy(szFileName, szAppPath);
HRSRC hRes;

if( bRegister == FALSE)
{
    strcat(szFileName,"\\NSim.exe"); //make same name of the Client & Server in program file
    hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_LANSIMSERVER),RT_RCDATA);

    if(flagUpgrade ==0)
    {
        CString trial = installationDate();   //----- Detemine Expiry Date -----

        setRegistry(trial);
    }
}
Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
sunil.nishad87
  • 105
  • 1
  • 2
  • 7

5 Answers5

8

It's a file permissions issue, plain and simple. Programs can't just go rooting around system directories in Windows 7. That's why it works "properly" in Windows XP, but not in newer versions.

I can't tell for sure, but it looks like you're trying to write an installer. If so, why are you reinventing the wheel? There are tons of great setup utilities available—Visual Studio provides a setup project that you can customize to your needs, or look into Inno Setup, my personal favorite. A Google search will turn up plenty of other options that have already solved this problem for you, and innumerable others.

If this isn't an installer, and you're just trying to store application and/or user data in the Program Files folder, I highly recommend that you look elsewhere. You weren't supposed to shove data into the app folder under earlier versions of Windows, and Windows 7 just cuts you off at the knees if you do this. Your best bet is to follow the recommendations that existed from the beginning: Investigate the user and common Application Data folders carefully. Use the SHGetKnownFolderPath function to retrieve the full path to a known folder using its KNOWNFOLDERID. A couple of suggestions:

  • FOLDERID_ProgramData (a shared program data directory for all users)
  • FOLDERID_LocalAppData (a per-user program data directory, non-roaming)
  • FOLDERID_RoamingAppData (a per-user program data directory, roaming)

Alternatively, you can try running the application as an Administrator. You might want to look into creating a manifest that indicates the application requires administrator-level permissions to execute.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • hi i have all permission. can u told some other logic? – sunil.nishad87 Jan 03 '11 at 11:06
  • 1
    @sunil: How have you confirmed that? The difference in the way UAC enforces permissions is the most likely explanation for your code working as you expect on Windows XP, but not on later versions of Windows. – Cody Gray - on strike Jan 03 '11 at 11:11
  • yes i am confirmed. this code is running for windowsxp but not in windows7 operating system. can i use anything else instead of this code. – sunil.nishad87 Jan 03 '11 at 11:18
  • @sunil: Did you actually *read* my answer before posting a comment? I provided at least 3 different suggestions on what you could do instead. – Cody Gray - on strike Jan 03 '11 at 11:21
  • yes first i tried inno setup but i don't know how to used that software for creating setup file. – sunil.nishad87 Jan 03 '11 at 11:52
  • 1
    @sunil: Read the [documentation](http://www.innosetup.com/ishelp/) and the help file included with Inno Setup. There are also some sample installer scripts included. The website has a [knowledge base](http://www.innosetup.com/iskb.php) containing supplemental information. A quick Google search reveals [this quick-start guide](http://www.fredshack.com/docs/inno.html). It really isn't very difficult to use. Don't be helpless. – Cody Gray - on strike Jan 03 '11 at 11:58
2

windows7? Ok, the problem is not with your program. Its with the file system permissions in Windows 7. User programs cannot create files there.

Sarwar Erfan
  • 18,034
  • 5
  • 46
  • 57
  • See this: http://stackoverflow.com/questions/1795784/copy-to-program-files-under-windows-vista-7 – Sarwar Erfan Jan 03 '11 at 09:37
  • 1
    And, try avoiding to use your own program as installer for your other program. Even if you have custom UI and custom this and that, many installer maker have the option to plugin your own dll and call functions from your dll during installation process. – Sarwar Erfan Jan 03 '11 at 09:38
  • can u told me the process for that. – sunil.nishad87 Jan 03 '11 at 10:27
  • What additional thing do you want to have in your installer (other than just copying files)? – Sarwar Erfan Jan 03 '11 at 10:36
  • my exe file is also not displayed at desktop and startup menu. – sunil.nishad87 Jan 03 '11 at 10:44
  • If you are willing to have a normal installer with file copying, shortcut in desktop/start menu, initial registry key creation, uninstall option in start menu/control panel (and many more) you should try InnoSetup. It is free. http://www.jrsoftware.org/isinfo.php – Sarwar Erfan Jan 03 '11 at 10:48
  • Read documentation on the site. Or just try using it right away, it has a wizard too. Why dont you start with File > New? – Sarwar Erfan Jan 03 '11 at 11:08
  • This video might also help (I did not create it) http://www.youtube.com/watch?v=LxA_oK_DeQE – Sarwar Erfan Jan 03 '11 at 11:10
  • thanks. for the help but i can't use that software for creating exe file and desktop menu – sunil.nishad87 Jan 03 '11 at 11:48
  • Then you are doing something wrong. Read the user manual. This InnoSetup is able to do all those stuffs. Put some efforts reading. – Sarwar Erfan Jan 03 '11 at 11:50
2

[edit] I edited the code in the question for readability and removed the commented out code (to see the wood for the trees). It is now obvious that nothing initialises szAppPath before calling strcat(), and calling ExpandEnvironmentStrings with NULL as the first argument is undefined (and certainly useless). Calling strcat() on an unitialised string is not likely to have the desired result. This may be an artefact of not posting the real code, or even of other peoples edits (including mine).


CreateDirectory sets the system error code on error; if you want to know what went wrong, check it! Any answer you get here will be an educated guess.

if(!CreateDirectory(szAppPath,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); 
    return ;
}

If you just want to get the error code and look it up manually, then a complete directory of codes is available on MSDN, here, I would guess that ERROR_ACCESS_DENIED (5) is most probable. A more elaborate example of error code display is given here.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    @sunil.nishad87: So fix it! Except that it quite clearly does!: http://msdn.microsoft.com/en-us/library/ms645505%28VS.85%29.aspx – Clifford Jan 03 '11 at 12:30
1

I think the problem is lack of privileges. You can debug your project to see whether the CreateDirectory function sets an error as ERROR_ACCESS_DENIED, if it does, you should make your program run with an administrator privilege. Add manifest in your project to do so.

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
denfon
  • 11
  • 2
0

It is intended to protect your computer against attack. Well maybe. Or Microsoft deciding to tell you what you are and not allowed to do on your own computer.

In any case you can change your UAC settings if you really have to write there in that way although that obviously exposes you to risk.

Otherwise play nice and do things the Microsoft way, using a proper installer.

CashCow
  • 30,981
  • 5
  • 61
  • 92