// Gets the path to the system special folder that is identified by the specified
// parameter value.
//
// Dependencies: CString
//
// Web: http://unclassified.software/source/getspecialfolderpath

// SystemDir defines
#define SD_ROOT          0x0100
#define SD_WIN           0x0101
#define SD_SYS           0x0102

// params used for SHGetSpecialFolderPath
// only available with shell32.dll v4.71+
// copied from pladform sdk shlobj.h (SD_* was originally CSIDL_*)
// remarked values return no filesystem path
#define SD_DESKTOP                  0x0000   // <desktop>
#define SD_PROGRAMS                 0x0002   // Start Menu\Programs
#define SD_PERSONAL                 0x0005   // My Documents
#define SD_FAVORITES                0x0006   // <user name>\Favorites
#define SD_STARTUP                  0x0007   // Start Menu\Programs\Startup
#define SD_RECENT                   0x0008   // <user name>\Recent
#define SD_SENDTO                   0x0009   // <user name>\SendTo
#define SD_STARTMENU                0x000b   // <user name>\Start Menu
#define SD_DESKTOPDIRECTORY         0x0010   // <user name>\Desktop
#define SD_NETHOOD                  0x0013   // <user name>\nethood
#define SD_FONTS                    0x0014   // windows\fonts
#define SD_TEMPLATES                0x0015
#define SD_COMMON_STARTMENU         0x0016   // All Users\Start Menu
#define SD_COMMON_PROGRAMS          0X0017   // All Users\Programs
#define SD_COMMON_STARTUP           0x0018   // All Users\Startup
#define SD_COMMON_DESKTOPDIRECTORY  0x0019   // All Users\Desktop
#define SD_APPDATA                  0x001a   // <user name>\Application Data
#define SD_PRINTHOOD                0x001b   // <user name>\PrintHood
#define SD_LOCAL_APPDATA            0x001c   // <user name>\Local Settings\Application Data (non roaming)
#define SD_COMMON_FAVORITES         0x001f
#define SD_INTERNET_CACHE           0x0020
#define SD_COOKIES                  0x0021
#define SD_HISTORY                  0x0022
#define SD_COMMON_APPDATA           0x0023   // All Users\Application Data
#define SD_WINDOWS                  0x0024   // GetWindowsDirectory()
#define SD_SYSTEM                   0x0025   // GetSystemDirectory()
#define SD_PROGRAM_FILES            0x0026   // C:\Program Files
#define SD_MYPICTURES               0x0027   // C:\Program Files\My Pictures
#define SD_PROFILE                  0x0028   // USERPROFILE
#define SD_SYSTEMX86                0x0029   // x86 system directory on RISC
#define SD_PROGRAM_FILESX86         0x002a   // x86 C:\Program Files on RISC
#define SD_PROGRAM_FILES_COMMON     0x002b   // C:\Program Files\Common
#define SD_PROGRAM_FILES_COMMONX86  0x002c   // x86 Program Files\Common on RISC
#define SD_COMMON_TEMPLATES         0x002d   // All Users\Templates
#define SD_COMMON_DOCUMENTS         0x002e   // All Users\Documents
#define SD_COMMON_ADMINTOOLS        0x002f   // All Users\Start Menu\Programs\Administrative Tools
#define SD_CONNECTIONS              0x0031   // Network and Dial-up Connections
//#define SD_INTERNET                 0x0001   // Internet Explorer (icon on desktop)
//#define SD_CONTROLS                 0x0003   // My Computer\Control Panel
//#define SD_PRINTERS                 0x0004   // My Computer\Printers
//#define SD_BITBUCKET                0x000a   // <desktop>\Recycle Bin
//#define SD_DRIVES                   0x0011   // My Computer
//#define SD_NETWORK                  0x0012   // Network Neighborhood
//#define SD_ALTSTARTUP               0x001d   // non localized startup
//#define SD_COMMON_ALTSTARTUP        0x001e   // non localized common startup
//#define SD_ADMINTOOLS               0x0030   // <user name>\Start Menu\Programs\Administrative Tools

#define SD_FLAG_CREATE              0x8000   // combine with SD_ value to force folder creation in SHGetFolderPath()
#define SD_FLAG_DONT_VERIFY         0x4000   // combine with SD_ value to return an unverified folder path
#define SD_FLAG_MASK                0xFF00   // mask for all possible flag values

// runtime function
typedef BOOL (WINAPI *SHGETSPECIALFOLDERPATH)(HWND hwndOwner, LPTSTR lpszPath, int nFolder, BOOL fCreate);
SHGETSPECIALFOLDERPATH pSHGetSpecialFolderPath = 0;

// initialisation needed!
HMODULE hShellDLL = LoadLibrary("shell32.dll");
if (hShellDLL)
{
	pSHGetSpecialFolderPath =
		(SHGETSPECIALFOLDERPATH) GetProcAddress(hShellDLL, "SHGetSpecialFolderPathA");
}

// free module when finished!
FreeLibrary(hShellDLL);

CString GetSpecialFolderPath(int iType)
{
	CString str;
	int i = MAX_PATH + 1;
	switch (iType)
	{
		case SD_ROOT:
			str = GetSpecialFolderPath(SD_WIN);
			str = str.Left(3);
			break;
		case SD_WIN:
			GetWindowsDirectory(str.GetBuffer(i), i);
			str.ReleaseBuffer();
			break;
		case SD_SYS:
			GetSystemDirectory(str.GetBuffer(i), i);
			str.ReleaseBuffer();
			break;
		default:
			if (pSHGetSpecialFolderPath)
			{
				pSHGetSpecialFolderPath(NULL, str.GetBuffer(MAX_PATH + 1), iType, FALSE);
				str.ReleaseBuffer();
			}
			else
			{
				str = "";
			}
			break;
	}
	return str;
}
