Solved

API for find user's home directory?

Posted on 2001-06-29
8
1,099 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Query (QBE) Using VBA Function for Criteria 13 71
Windows 10 Scheduled Tasks 11 111
Is COM supported from Apache 1 77
Developing a front end to SPLUNK 1 100
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 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