Solved

API for find user's home directory?

Posted on 2001-06-29
8
1,089 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now