From 16bfa4ec2f4623ba843f7a575169e3df8b5eea8d Mon Sep 17 00:00:00 2001 From: Niklas Therning Date: Fri, 2 Sep 2016 21:52:57 +0200 Subject: Prefer SHGetKnownFolderPath() in g_get_home_dir() over HOMEDRIVE/HOMEPATH environment variables Relying on the HOMEDRIVE/HOMEPATH environment variables is fragile as those variables don't always have to be set. One such scenario is when running on a Jenkins slave. When not set g_get_home_dir() in eglib would return NULL which led to a crash in ves_icall_System_Environment_InternalGetHome(). This patch changes g_get_home_dir() to first try to call SHGetKnownFolderPath() to get the current user's profile directory (i.e. home dir). If that fails it falls back to the USERPROFILE environment variable. If that isn't available the function will fallback to the original behavior and try with HOMEDRIVE/HOMEPATH. --- eglib/src/gmisc-win32.c | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'eglib') diff --git a/eglib/src/gmisc-win32.c b/eglib/src/gmisc-win32.c index 9486aff07e3..5baec653c33 100644 --- a/eglib/src/gmisc-win32.c +++ b/eglib/src/gmisc-win32.c @@ -30,6 +30,9 @@ #include #include +#ifdef _MSC_VER +#include +#endif #include #include @@ -116,20 +119,34 @@ g_path_is_absolute (const char *filename) const gchar * g_get_home_dir (void) { - /* FIXME */ - const gchar *drive = g_getenv ("HOMEDRIVE"); - const gchar *path = g_getenv ("HOMEPATH"); gchar *home_dir = NULL; - - if (drive && path) { - home_dir = g_malloc(strlen(drive) + strlen(path) +1); - if (home_dir) { - sprintf(home_dir, "%s%s", drive, path); - } + +#ifdef _MSC_VER + PWSTR profile_path = NULL; + HRESULT hr = SHGetKnownFolderPath (&FOLDERID_Profile, KF_FLAG_DEFAULT, NULL, &profile_path); + if (SUCCEEDED(hr)) { + home_dir = u16to8 (profile_path); + CoTaskMemFree (profile_path); + } +#endif + + if (!home_dir) { + home_dir = (gchar *) g_getenv ("USERPROFILE"); } - g_free (drive); - g_free (path); + if (!home_dir) { + const gchar *drive = g_getenv ("HOMEDRIVE"); + const gchar *path = g_getenv ("HOMEPATH"); + + if (drive && path) { + home_dir = g_malloc (strlen (drive) + strlen (path) + 1); + if (home_dir) { + sprintf (home_dir, "%s%s", drive, path); + } + } + g_free (drive); + g_free (path); + } return home_dir; } -- cgit v1.2.3