6

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.

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
Peretz
  • 1,096
  • 2
  • 18
  • 31

3 Answers3

7

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

kichik
  • 33,220
  • 7
  • 94
  • 114
5

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • You still don't know where the software was actually installed to. All you know is the program files folder, but for all we know it might be installed in C:\i-cut\ – pezcode Dec 01 '11 at 21:47
  • @pezcode: At least according to the title (and the remainder seems to fit with this) he's basically just looking for `Program Files` vs. `Program files (x86)`. – Jerry Coffin Dec 01 '11 at 22:00
-2

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;
} 
krolth
  • 1,032
  • 9
  • 12
  • This is wrong. Always use ProgramFiles, never use ProgramFiles(x86) - WOW64 filesystem redirection will take care of the translation. – Ana Betts Dec 01 '11 at 22:32