16

I am programming in c++ MFC,

I want to get "C:\windows" "c:\program files" folder path.

Sometimes user may setup windows in other folder such as c:\windows0.

Is there any API to get absolute path of the windows and program files path?

Many thanks!

sxingfeng
  • 971
  • 4
  • 15
  • 32

5 Answers5

28

Using Win32 API>

For the Windows folder:

TCHAR windir[MAX_PATH];
GetWindowsDirectory(windir, MAX_PATH);

For program files:

TCHAR pf[MAX_PATH];
SHGetSpecialFolderPath(
    0,
    pf, 
    CSIDL_PROGRAM_FILES, 
    FALSE ); 

Where MAX_PATH comes from the Windows headers and will guarantee the buffer is long enough for the longest (non-UNC) path.

Also, note that SHGetSpecialFolderPath can be used to retrieve other "special" folder including the Windows folder just by replacing the third parameter to any from this list.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
Anzurio
  • 16,780
  • 3
  • 39
  • 49
14
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
4

On Vista+, SHGetKnownFolderPath is the replacement for SHGetFolderPath and SHGetSpecialFolderPath, although you can continue to use the older functions if you need backward compatibility to older versions of Windows.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
1

Call getenv("%ProgramFiles%") and getenv("%WinDir%")

Sameer
  • 725
  • 5
  • 8
1

Most of these come from SHGetFolderPath, but GetSystemDirectory() returns the absolute location of C:\Windows\System32. Don't use GetWindowsDirectory(). It doesn't do what you want anymore.

Joshua
  • 40,822
  • 8
  • 72
  • 132