+bool wxGetUserName(wxChar *buf, int maxSize)
+{
+#if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__)
+ extern HANDLE g_hPenWin; // PenWindows Running?
+ if (g_hPenWin)
+ {
+ // PenWindows Does have a user concept!
+ // Get the current owner of the recognizer
+ GetPrivateProfileString("Current", "User", default_name, wxBuffer, maxSize - 1, "PENWIN.INI");
+ strncpy(buf, wxBuffer, maxSize - 1);
+ }
+ else
+#endif
+ {
+#ifdef USE_NET_API
+ CHAR szUserName[256];
+ if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) )
+ return FALSE;
+
+ // TODO how to get the domain name?
+ CHAR *szDomain = "";
+
+ // the code is based on the MSDN example (also see KB article Q119670)
+ WCHAR wszUserName[256]; // Unicode user name
+ WCHAR wszDomain[256];
+ LPBYTE ComputerName;
+
+ USER_INFO_2 *ui2; // User structure
+
+ // Convert ANSI user name and domain to Unicode
+ MultiByteToWideChar( CP_ACP, 0, szUserName, strlen(szUserName)+1,
+ wszUserName, WXSIZEOF(wszUserName) );
+ MultiByteToWideChar( CP_ACP, 0, szDomain, strlen(szDomain)+1,
+ wszDomain, WXSIZEOF(wszDomain) );
+
+ // Get the computer name of a DC for the domain.
+ if ( NetGetDCName( NULL, wszDomain, &ComputerName ) != NERR_Success )
+ {
+ wxLogError(wxT("Can not find domain controller"));
+
+ goto error;
+ }
+
+ // Look up the user on the DC
+ NET_API_STATUS status = NetUserGetInfo( (LPWSTR)ComputerName,
+ (LPWSTR)&wszUserName,
+ 2, // level - we want USER_INFO_2
+ (LPBYTE *) &ui2 );
+ switch ( status )
+ {
+ case NERR_Success:
+ // ok
+ break;
+
+ case NERR_InvalidComputer:
+ wxLogError(wxT("Invalid domain controller name."));
+
+ goto error;
+
+ case NERR_UserNotFound:
+ wxLogError(wxT("Invalid user name '%s'."), szUserName);
+
+ goto error;
+
+ default:
+ wxLogSysError(wxT("Can't get information about user"));
+
+ goto error;
+ }
+
+ // Convert the Unicode full name to ANSI
+ WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1,
+ buf, maxSize, NULL, NULL );
+
+ return TRUE;
+
+error:
+ wxLogError(wxT("Couldn't look up full user name."));
+
+ return FALSE;
+#else // !USE_NET_API
+ // Could use NIS, MS-Mail or other site specific programs
+ // Use wxWindows configuration data
+ bool ok = GetProfileString(WX_SECTION, eUSERNAME, wxT(""), buf, maxSize - 1) != 0;
+ if ( !ok )
+ {
+ ok = wxGetUserId(buf, maxSize);
+ }
+
+ if ( !ok )
+ {
+ wxStrncpy(buf, wxT("Unknown User"), maxSize);
+ }
+#endif // Win32/16
+ }
+
+ return TRUE;
+}
+
+const wxChar* wxGetHomeDir(wxString *pstr)
+{
+ wxString& strDir = *pstr;
+
+ #if defined(__UNIX__) && !defined(__TWIN32__)
+ const wxChar *szHome = wxGetenv("HOME");
+ if ( szHome == NULL ) {
+ // we're homeless...
+ wxLogWarning(_("can't find user's HOME, using current directory."));
+ strDir = wxT(".");
+ }
+ else
+ strDir = szHome;
+
+ // add a trailing slash if needed
+ if ( strDir.Last() != wxT('/') )
+ strDir << wxT('/');
+ #else // Windows
+ #ifdef __WIN32__
+ const wxChar *szHome = wxGetenv(wxT("HOMEDRIVE"));
+ if ( szHome != NULL )
+ strDir << szHome;
+ szHome = wxGetenv(wxT("HOMEPATH"));
+ if ( szHome != NULL ) {
+ strDir << szHome;
+
+ // the idea is that under NT these variables have default values
+ // of "%systemdrive%:" and "\\". As we don't want to create our
+ // config files in the root directory of the system drive, we will
+ // create it in our program's dir. However, if the user took care
+ // to set HOMEPATH to something other than "\\", we suppose that he
+ // knows what he is doing and use the supplied value.
+ if ( wxStrcmp(szHome, wxT("\\")) != 0 )
+ return strDir.c_str();
+ }
+
+ #else // Win16
+ // Win16 has no idea about home, so use the working directory instead
+ #endif // WIN16/32
+
+ // 260 was taken from windef.h
+ #ifndef MAX_PATH
+ #define MAX_PATH 260
+ #endif
+
+ wxString strPath;
+ ::GetModuleFileName(::GetModuleHandle(NULL),
+ strPath.GetWriteBuf(MAX_PATH), MAX_PATH);
+ strPath.UngetWriteBuf();
+
+ // extract the dir name
+ wxSplitPath(strPath, &strDir, NULL, NULL);
+
+ #endif // UNIX/Win
+
+ return strDir.c_str();
+}
+
+wxChar *wxGetUserHome(const wxString& user)