Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Get System Folder Path(C:\Windows C:\Program Files) in Windows using C++?

Tags:

c++

windows

api

mfc

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!

like image 960
sxingfeng Avatar asked Mar 22 '10 01:03

sxingfeng


2 Answers

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.

like image 115
Anzurio Avatar answered Oct 23 '22 01:10

Anzurio


  • GetWindowsDirectory: http://msdn.microsoft.com/en-us/library/ms724454(VS.85).aspx
  • SHGetSpecialFolderPath: http://msdn.microsoft.com/en-us/library/bb762204(VS.85).aspx
like image 14
Daniel A. White Avatar answered Oct 23 '22 02:10

Daniel A. White