Peretz
Peretz

Reputation: 1126

Is there a simple Program Files/Program Files (x86) directive for C++ in windows?

I am currently hard-coding the path to my application as follows:

const char* OriginCopyFile = "C:\\Program Files (x86)\\i-cut\\i-cut\\Origin_copy.txt";

This application is going to be running in both 32 and 64 systems. How can I detect the path without the file name in order to reuse it with several files and make it portable between architecture.

Upvotes: 6

Views: 3785

Answers (3)

krolth
krolth

Reputation: 1032

You can use environment variables for this:

#include <stdio.h>
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
char* programFiles = getenv("ProgramFiles(x86)");   
if (programFiles==NULL)
{
    programFiles = getenv("ProgramFiles");
}

printf(programFiles);

return 0;
} 

Upvotes: -2

kichik
kichik

Reputation: 34744

You can use GetModuleFileName to get the path to your executable, wherever it was installed or even moved later. You can then PathRemoveFileSpec to remove the executable name (or strchr() and friends if you want to support earlier versions than Windows 2000).

Upvotes: 7

Jerry Coffin
Jerry Coffin

Reputation: 490713

SHGetSpecialFolderPath(CSIDL_PROGRAM_FILES) will at least give the path to the program files directory. You'll have to deal with adding the rest of the path and file name.

Upvotes: 5

Related Questions