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

Started by
3 comments, last by Colin Jeanne 17 years, 6 months ago
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]
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
Advertisement
I don't know of any function to return it, but you could maybe use "%HOMEPATH%\My Documents"

Hope this helps,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
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]
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+).
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}

This topic is closed to new replies.

Advertisement