Solved

API for find user's home directory?

Posted on 2001-06-29
8
1,096 Views
Last Modified: 2013-12-03
What is the Win32 API for discovering the user's home or profile directory? I want to be able to create application temporary and preference files, on a per user basis, while allowing the application to run from read-only media like a cdrom.

I've researched some calls like GetWindowsDirectory(), GetSystemInfo(), %HOMEPATH% but can't find anything that really does the job. Any pointers to how this can be done is appreciated on the different flavors of Windows (98/NT)?

Regards,
Marcus
0
Comment
Question by:Marcus_Aurelius
8 Comments
 
LVL 15

Accepted Solution

by:
NickRepin earned 25 total points
ID: 6239839
SHGetSpecialFolderPath()
SHGetSpecialFolderLocation()

CSIDL_APPDATA File system directory that serves as a common repository for application-specific data.

CSIDL_PERSONAL  File system directory that serves as a common repository for documents.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6240498
On NT, you can use the following:
char szWinDir[128];
GetWindowsDirectory(szWinDir,sizeof(szWinDir));
char szUserName[128];
GetUserName(szUserName,sizeof(szUserName));
char szUserHomeDir[1024];
sprintf(szUserHomeDir,"%sprofile%s",szWinDir,szUserName);
0
 
LVL 32

Expert Comment

by:jhance
ID: 6240992
Use NetUserGetInfo() (from NETAPI32.LIB) using the USER_INFO_1 structure.  I'm not sure about the SHGet... suggestion above but Axter's comment assumes that the user home dirs are under the Windows folder, which they don't have to be.

Use it like (Note that this function is UNICODE ONLY!!):

NET_API_STATUS nStatus;
LPUSER_INFO_1 usrBuf;
char lpszUserDir[_MAX_PATH];

nStatus = NetUserGetInfo(NULL, L"joeuser", 1, (LPBYTE *)&usrBuf);


if(nStatus == NERR_Success){
  // The home dir is in the usri1_home_dir member
  // Again note that this is a UNICODE string. PRobably
  // need to convert to ASCII with WideCharToMultiByte()
  // or whatever
  WideCharToMultiByte(CP_ACP, 0, usrBuf->usri1_home_dir, wcslen(usrBuf->usri1_home_dir), lpszUserDir, _MAX_PATH, NULL, NULL);

  // Must free the buffer returned byt NetUserGetInfo
  NetApiBufferFree((LPVOID)usrBuf);
}

(I didn't try to compile this so please be tolerant of syntax errors but this should give you the feel for it...)
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 86

Assisted Solution

by:jkr
jkr earned 25 total points
ID: 6241017
NT/W2k and afaik Win9x also store the user's home directory's location in the 3 env. variables

HOMESHARE=\\<server name>\<share name> HOMEPATH=\<path> HOMEDRIVE=<drive letter>:

If the home directory is a local path such as c:\nt then they will look like this:

HOMEDRIVE=c: HOMESHARE= HOMEPATH=\nt
0
 

Author Comment

by:Marcus_Aurelius
ID: 6241357
HOMESHARE etc. are not always defined via the Sys. Admins via the user manager tools. I find them to be too unreliable to use. I'm researching the Shell suggestion which seems to underly the Net functionality.

Keep'm coming thanks!
Marcus
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6247928
Value of environment variable USERPROFILE.
0
 
LVL 86

Expert Comment

by:jkr
ID: 10580935
Let me see if I got that right - three *years* no feedback and you are handing out Cs? Well, be happy that there is no grading for questioners.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question