首先介绍一下SHGetSpecialFolderPath函数:
该函数用于获取指定的系统路径。需要包含#include <shlobj.h>
BOOL SHGetSpecialFolderPath(
HWND hwndOwner,
_Out_ LPTSTR lpszPath,
_In_ int csidl,
_In_ BOOL fCreate
);
通过分析Qt代码中对于SHGetSpecialFolderPath函数的调用主要在2个类里面。一个为QDesktopServices类,另外一个为QSettings类。
QString QDesktopServices::storageLocation(StandardLocation type)
{
QString result;
#ifndef Q_OS_WINCE
QSystemLibrary library(QLatin1String("shell32"));
#else
QSystemLibrary library(QLatin1String("coredll"));
#endif // Q_OS_WINCE
typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPWSTR, int, BOOL);
static GetSpecialFolderPath SHGetSpecialFolderPath =
(GetSpecialFolderPath)library.resolve("SHGetSpecialFolderPathW");
if (!SHGetSpecialFolderPath)
return QString();
wchar_t path[MAX_PATH];
switch (type) {
case DataLocation:
#if defined Q_WS_WINCE
if (SHGetSpecialFolderPath(0, path, CSIDL_APPDATA, FALSE))
#else
if (SHGetSpecialFolderPath(0, path, CSIDL_LOCAL_APPDATA, FALSE))
#endif
result = QString::fromWCharArray(path);
if (!QCoreApplication::organizationName().isEmpty())
result = result + QLatin1String("\\") + QCoreApplication::organizationName();
if (!QCoreApplication::applicationName().isEmpty())
result = result + QLatin1String("\\") + QCoreApplication::applicationName();
break;
case DesktopLocation:
if (SHGetSpecialFolderPath(0, path, CSIDL_DESKTOPDIRECTORY, FALSE))
result = QString::fromWCharArray(path);
break;
case DocumentsLocation:
if (SHGetSpecialFolderPath(0, path, CSIDL_PERSONAL, FALSE))
result = QString::fromWCharArray(path);
break;
case FontsLocation:
if (SHGetSpecialFolderPath(0, path, CSIDL_FONTS, FALSE))
result = QString::fromWCharArray(path);
break;
case ApplicationsLocation:
if (SHGetSpecialFolderPath(0, path, CSIDL_PROGRAMS, FALSE))
result = QString::fromWCharArray(path);
break;
case MusicLocation:
if (SHGetSpecialFolderPath(0, path, CSIDL_MYMUSIC, FALSE))
result = QString::fromWCharArray(path);
break;
case MoviesLocation:
if (SHGetSpecialFolderPath(0, path, CSIDL_MYVIDEO, FALSE))
result = QString::fromWCharArray(path);
break;
case PicturesLocation:
if (SHGetSpecialFolderPath(0, path, CSIDL_MYPICTURES, FALSE))
result = QString::fromWCharArray(path);
break;
case CacheLocation:
// Although Microsoft has a Cache key it is a pointer to IE's cache, not a cache
// location for everyone. Most applications seem to be using a
// cache directory located in their AppData directory
return storageLocation(DataLocation) + QLatin1String("\\cache");
case QDesktopServices::HomeLocation:
return QDir::homePath(); break;
case QDesktopServices::TempLocation:
return QDir::tempPath(); break;
default:
break;
}
return result;
}
其实该函数提供的地址远远不止这些东西,只是Qt选择部分系统路径。
如果需要扩充该类的功能的话,可以手工添加进去。