Solved

API for find user's home directory?

Posted on 2001-06-29
8
1,088 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Assisted Solution

by:jkr
jkr earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Value of environment variable USERPROFILE.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now