diff options
author | Niklas Therning <niklas@therning.org> | 2016-09-02 22:52:57 +0300 |
---|---|---|
committer | Niklas Therning <niklas@therning.org> | 2016-09-03 23:10:59 +0300 |
commit | 16bfa4ec2f4623ba843f7a575169e3df8b5eea8d (patch) | |
tree | 51fc0fd39c58c5bdcdadaa284b6ddeb6fb26f4f3 /eglib/src/gmisc-win32.c | |
parent | 5589845bf38b67d7566907ddcc6c9fb6b2c8c069 (diff) |
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.
Diffstat (limited to 'eglib/src/gmisc-win32.c')
-rw-r--r-- | eglib/src/gmisc-win32.c | 39 |
1 files changed, 28 insertions, 11 deletions
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 <glib.h> #include <windows.h> +#ifdef _MSC_VER +#include <shlobj.h> +#endif #include <direct.h> #include <io.h> @@ -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; } |