diff win/dw.c @ 907:3e393a9375c4

Implemented _dw_user_dir() using GetUserProfileDirectory() on Windows. GetUserProfileDirectory is in userenv.dll which is a file only included on NT-based Windows versions. Therefore Windows 95/98/ME are no longer supported by Dynamic Windows.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 19 Apr 2011 07:55:07 +0000
parents db26726118ba
children 0c39705ddd4a
line wrap: on
line diff
--- a/win/dw.c	Tue Apr 19 05:54:01 2011 +0000
+++ b/win/dw.c	Tue Apr 19 07:55:07 2011 +0000
@@ -13,6 +13,7 @@
 #include <commctrl.h>
 #include <shlwapi.h>
 #include <shlobj.h>
+#include <userenv.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
@@ -10145,19 +10146,27 @@
  */
 char * API dw_user_dir(void)
 {
-   static char _user_dir[1024] = "";
-
-   if(!_user_dir[0])
-   {
-      /* Figure out how to do this the "Windows way" */
-      char *home = getenv("HOME");
-
-      if(home)
-         strcpy(_user_dir, home);
-      else
-         strcpy(_user_dir, "C:\\");
-   }
-   return _user_dir;
+    static char _user_dir[1024] = "";
+
+    if(!_user_dir[0])
+    {
+        HANDLE hToken = 0;
+        
+        /* Use the Windows API to get the user's profile directory */
+        if(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
+        {
+            DWORD BufSize = 1024;
+        
+            GetUserProfileDirectory(hToken, _user_dir, &BufSize);
+            CloseHandle(hToken);
+        }
+        /* If it fails set it to the root directory */
+        if(!_user_dir[0])
+        {
+            strcpy(_user_dir, "C:\\");
+        }
+    }
+    return _user_dir;
 }
 
 /*