Since you've mentioned a Windows user, I am assuming that you
want a solution for Windows. Acquiring the username can be done
using Win32 function SHGetKnownFolderPath
. However, note that
if you want your application to support Windows XP, then you will
have to use an older (deprecated) function SHGetFolderPath
. You
can use this sample:
wchar_t *pszDesktopFolderPath;
if(S_OK == SHGetKnownFolderPath(FOLDERID_Desktop, KF_FLAG_DEFAULT, NULL, pszDesktopFolderPath))
{
// pszDesktopFolderPath now points to the path to desktop folder
...
// After you are done with it, delete the buffer for the path like this
CoTaskMemFree(pszDesktopFolderPath);
}
And here is an example using SHGetFolderPath
:
LPTSTR pszDesktopFolderPath[MAX_PATH];
if(S_OK == SHGetFolderPathA(NULL, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, pszDesktopFolderPath))
{
// pszDesktopFolderPath now points to the path to desktop folder, no need to manually release memory
}
Note that the third parameter represents a token of the user for which
trying to acquire desktop location. In a case when you app is started by
elevating privileges from standard user account to an administrator account,
the path returned by the functions mentioned above is the path to desktop
of the administrator user. If you want to get the path to standard user, you
will have to acquire token of that user and pass it as the third parameter.