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
Solved

API for find user's home directory?

Posted on 2001-06-29
8
1,094 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where to obtain Oracle Data Provider for .NET 3.5 7 53
Post a good COM tutorial 1 60
windbg taking it's time 32 93
Need a simple RegEx to search for two words 27 42
This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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