Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current user's home directory in Windows

Tags:

c++

windows

How can I get the path to the current User's home directory?

Ex: In Windows, if the current user is "guest" I need "C:\Users\guest"

My application will run on most of the Windows versions (XP, Vista, Win 7).

like image 446
Ullan Avatar asked Mar 03 '12 01:03

Ullan


1 Answers

Use the function SHGetFolderPath. This function is preferred over querying environment variables since the latter can be modified to point to a wrong location. The documentation contains an example, which I repeat here (slightly adjusted):

#include <Shlobj.h>  // need to include definitions of constants

// .....

WCHAR path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path))) {
  ...
}
like image 61
Philipp Avatar answered Sep 25 '22 04:09

Philipp