Is there any Win32 API to get users home directory
-
HI Any can help me, to get the users home directory in windows. Is there any win32 API avilable for it? Home directory means ::C:\Documents and Settings\ Please help with possible ways to get this thro program. Thanks, Nandu
-
HI Any can help me, to get the users home directory in windows. Is there any win32 API avilable for it? Home directory means ::C:\Documents and Settings\ Please help with possible ways to get this thro program. Thanks, Nandu
-
HI Any can help me, to get the users home directory in windows. Is there any win32 API avilable for it? Home directory means ::C:\Documents and Settings\ Please help with possible ways to get this thro program. Thanks, Nandu
You could also use GetEnvironmentVariable:
TCHAR tcDirName[_MAX_PATH];
GetEnvironmentVariable(_T("userprofile"), tcDirName, _MAX_PATH);This environment variable is set when the user logs on. Note that it is not always on the same drive as the system (C:). Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
You could also use GetEnvironmentVariable:
TCHAR tcDirName[_MAX_PATH];
GetEnvironmentVariable(_T("userprofile"), tcDirName, _MAX_PATH);This environment variable is set when the user logs on. Note that it is not always on the same drive as the system (C:). Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
Thanks for your reply, is there any specific win32 API who will do this job. As i need to use this in win32 API programming, not in MFC or higher framwork. -Nandu
-
Thanks for your reply, is there any specific win32 API who will do this job. As i need to use this in win32 API programming, not in MFC or higher framwork. -Nandu
Nandu_77b wrote:
is there any specific win32 API who will do this job
Yes, use
GetUserProfileDirectory
- retrieves the path to the root directory of the specified user's profile. For e.g. C:\Documents and Settings\Nibu A function which uses ths API to return current process user home dir...#include "Userenv.h"
#pragma comment(lib, "userenv.lib")
CString GetUserHomeDir()
{
TCHAR szHomeDirBuf[MAX_PATH] = { 0 };HANDLE hToken = 0;
VERIFY( OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ));DWORD BufSize = MAX_PATH;
VERIFY( GetUserProfileDirectory( hToken, szHomeDirBuf, &BufSize ));CloseHandle( hToken );
return CString( szHomeDirBuf );
}Then there are other functions too which might interest you, like...
GetAllUsersProfileDirectory
GetDefaultUserProfileDirectory
GetProfilesDirectory
Look up these in MSDN for more information.Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
modified on Wednesday, June 4, 2008 11:55 PM
-
Nandu_77b wrote:
is there any specific win32 API who will do this job
Yes, use
GetUserProfileDirectory
- retrieves the path to the root directory of the specified user's profile. For e.g. C:\Documents and Settings\Nibu A function which uses ths API to return current process user home dir...#include "Userenv.h"
#pragma comment(lib, "userenv.lib")
CString GetUserHomeDir()
{
TCHAR szHomeDirBuf[MAX_PATH] = { 0 };HANDLE hToken = 0;
VERIFY( OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ));DWORD BufSize = MAX_PATH;
VERIFY( GetUserProfileDirectory( hToken, szHomeDirBuf, &BufSize ));CloseHandle( hToken );
return CString( szHomeDirBuf );
}Then there are other functions too which might interest you, like...
GetAllUsersProfileDirectory
GetDefaultUserProfileDirectory
GetProfilesDirectory
Look up these in MSDN for more information.Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
modified on Wednesday, June 4, 2008 11:55 PM
Is the given code correct? I am wondering about the return instruction, in which a CString object is created: return CString( szHomeDirBuf ); In my opinion this object will be already destroyed while the function returns. When accessing from caller, the object is already invalid. CString res = GetUserHomeDir(); Am I wrong?