Remove dynamic loading of SHGetFolderPath and SHGetSpecialFolderPath.

SHGetFolderPath is available since Win2k and no fallback to the outdated SHGetSpecialFolderPath is required.
This commit is contained in:
Tobias Taschner
2015-10-08 10:29:50 +02:00
parent 5cce48186c
commit 0709199548

View File

@@ -42,8 +42,6 @@
// types // types
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
typedef HRESULT (WINAPI *SHGetFolderPath_t)(HWND, int, HANDLE, DWORD, LPTSTR);
typedef HRESULT (WINAPI *SHGetSpecialFolderPath_t)(HWND, LPTSTR, int, BOOL);
typedef HRESULT (WINAPI *SHGetKnownFolderPath_t)(const GUID&, DWORD, HANDLE, PWSTR *); typedef HRESULT (WINAPI *SHGetKnownFolderPath_t)(const GUID&, DWORD, HANDLE, PWSTR *);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -95,13 +93,10 @@ struct ShellFunctions
{ {
ShellFunctions() ShellFunctions()
{ {
pSHGetFolderPath = NULL; pSHGetKnownFolderPath = NULL;
pSHGetSpecialFolderPath = NULL;
initialized = false; initialized = false;
} }
SHGetFolderPath_t pSHGetFolderPath;
SHGetSpecialFolderPath_t pSHGetSpecialFolderPath;
SHGetKnownFolderPath_t pSHGetKnownFolderPath; SHGetKnownFolderPath_t pSHGetKnownFolderPath;
bool initialized; bool initialized;
@@ -134,30 +129,9 @@ void ResolveShellFunctions()
// with this // with this
wxLogNull noLog; wxLogNull noLog;
#if wxUSE_UNICODE
static const wchar_t UNICODE_SUFFIX = L'W';
#else // !Unicode
static const char UNICODE_SUFFIX = 'A';
#endif // Unicode/!Unicode
wxString funcname(wxT("SHGetFolderPath"));
gs_shellFuncs.pSHGetFolderPath =
(SHGetFolderPath_t)dllShellFunctions.GetSymbol(funcname + UNICODE_SUFFIX);
// then for SHGetSpecialFolderPath (shell32.dll 4.71)
if ( !gs_shellFuncs.pSHGetFolderPath )
{
funcname = wxT("SHGetSpecialFolderPath");
gs_shellFuncs.pSHGetSpecialFolderPath = (SHGetSpecialFolderPath_t)
dllShellFunctions.GetSymbol(funcname + UNICODE_SUFFIX);
}
gs_shellFuncs.pSHGetKnownFolderPath = (SHGetKnownFolderPath_t) gs_shellFuncs.pSHGetKnownFolderPath = (SHGetKnownFolderPath_t)
dllShellFunctions.GetSymbol("SHGetKnownFolderPath"); dllShellFunctions.GetSymbol("SHGetKnownFolderPath");
// finally we fall back on SHGetSpecialFolderLocation (shell32.dll 4.0),
// but we don't need to test for it -- it is available even under Win95
// shell32.dll is going to be unloaded, but it still remains in memory // shell32.dll is going to be unloaded, but it still remains in memory
// because we also link to it statically, so it's ok // because we also link to it statically, so it's ok
@@ -178,74 +152,35 @@ void ResolveShellFunctions()
/* static */ /* static */
wxString wxStandardPaths::DoGetDirectory(int csidl) wxString wxStandardPaths::DoGetDirectory(int csidl)
{ {
if ( !gs_shellFuncs.initialized )
ResolveShellFunctions();
wxString dir; wxString dir;
HRESULT hr = E_FAIL; HRESULT hr = E_FAIL;
// test whether the function is available during compile-time (it must be hr = ::SHGetFolderPath
// defined as either "SHGetFolderPathA" or "SHGetFolderPathW") (
#ifdef SHGetFolderPath NULL, // parent window, not used
// and now test whether we have it during run-time csidl,
if ( gs_shellFuncs.pSHGetFolderPath ) NULL, // access token (current user)
{ SHGFP_TYPE_CURRENT, // current path, not just default value
hr = gs_shellFuncs.pSHGetFolderPath wxStringBuffer(dir, MAX_PATH)
( );
NULL, // parent window, not used
csidl,
NULL, // access token (current user)
SHGFP_TYPE_CURRENT, // current path, not just default value
wxStringBuffer(dir, MAX_PATH)
);
// somewhat incredibly, the error code in the Unicode version is // somewhat incredibly, the error code in the Unicode version is
// different from the one in ASCII version for this function // different from the one in ASCII version for this function
#if wxUSE_UNICODE #if wxUSE_UNICODE
if ( hr == E_FAIL ) if ( hr == E_FAIL )
#else #else
if ( hr == S_FALSE ) if ( hr == S_FALSE )
#endif #endif
{
// directory doesn't exist, maybe we can get its default value?
hr = gs_shellFuncs.pSHGetFolderPath
(
NULL,
csidl,
NULL,
SHGFP_TYPE_DEFAULT,
wxStringBuffer(dir, MAX_PATH)
);
}
}
#endif // SHGetFolderPath
#ifdef SHGetSpecialFolderPath
if ( FAILED(hr) && gs_shellFuncs.pSHGetSpecialFolderPath )
{ {
hr = gs_shellFuncs.pSHGetSpecialFolderPath // directory doesn't exist, maybe we can get its default value?
( hr = ::SHGetFolderPath
NULL, // parent window (
wxStringBuffer(dir, MAX_PATH), NULL,
csidl, csidl,
FALSE // don't create if doesn't exist NULL,
); SHGFP_TYPE_DEFAULT,
} wxStringBuffer(dir, MAX_PATH)
#endif // SHGetSpecialFolderPath );
// SHGetSpecialFolderLocation should be available with all compilers and
// under all Win32 systems, so don't test for it (and as it doesn't exist
// in "A" and "W" versions anyhow, testing would be more involved, too)
if ( FAILED(hr) )
{
LPITEMIDLIST pidl;
hr = SHGetSpecialFolderLocation(NULL, csidl, &pidl);
if ( SUCCEEDED(hr) )
{
// creating this temp object has (nice) side effect of freeing pidl
dir = wxItemIdList(pidl).GetPath();
}
} }
return dir; return dir;