Sign in to follow this  

Getting path to "My Documents" folder in XP (Solved)

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Been googling for a while, and only found SHGetPathFromIDList, which I'm having little luck getting to work. I was wondering is there another command to return the path to "My Documents" in Windows XP, or am I just trying to use this incorrectly? Bear in mind the best example code I found to do this was Visual Basic code, which didn't seem to transfer nicely into C++, and MSDN wasn't much help on the subject: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shgetpathfromidlist.asp Thanks. - edit Figured it out, i'll post the code for anyone else who decides to search:
int Result;
char pPath[512];
LPITEMIDLIST pIDL = NULL;

Result = SHGetSpecialFolderLocation( (HWND)100, 0x5, &pIDL );

if ( Result == 0 )
{
	Result = SHGetPathFromIDList(pIDL, pPath);
}

[Edited by - Richy2k on September 28, 2006 7:50:34 AM]

Share this post


Link to post
Share on other sites
int Result;
char pPath[512];
LPITEMIDLIST pIDL = NULL;

Result = SHGetSpecialFolderLocation( (HWND)100, 0x5, &pIDL );

if ( Result == 0 )
{
Result = SHGetPathFromIDList(pIDL, pPath);
}




Obviously that's the way to return the My Documents location. Why couldn't you see that from the beginning?
</sarcasm>

I win32 gives me headaches. Viva Qt!

Even though Qt has a whole set of problems on it's own.

Nevermind the Qt plug. Aparently it sucks just as bad at finding the My Documents folder. It does find the %HOMEPATH% just fine though.

[Edited by - tstrimp on September 28, 2006 8:57:00 AM]

Share this post


Link to post
Share on other sites
Quote:
you could maybe use "%HOMEPATH%\My Documents"
That'll break as soon as somebody runs on a non-English version of Windows, or tries using Vista with no "My " prefix. (Another thing to not use is the Shell Folders registry key.)

The proper way is with SHGetFolderPath - that page includes an example that finds the user's documents directory, with a single function call and no magic numbers that probably work only through luck.

There's also Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) in .NET, and wxStandardPaths.Get().GetDocumentsDir() in wxWidgets (2.7+).

Share this post


Link to post
Share on other sites
Quote:
Original post by Richy2k
Figured it out, i'll post the code for anyone else who decides to search:

*** Source Snippet Removed ***

No! You do not cast an arbitrary number to an HWND and you do not stick and arbitrary number in for a CSIDL. Further there is another function which does what you want better:


TCHAR pszPath[MAX_PATH];
if (SUCCEEDED(SHGetSpecialFolderPath(NULL, pszPath, CSIDL_PERSONAL, FALSE))) {
// pszPath is now the path that you want
}



Or alternatively (if Win2k is a minimum requirement)

TCHAR pszPath[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, pszPath))) {
// pszPath is now the path that you want
}

Share this post


Link to post
Share on other sites

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this