1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/stdpaths.cpp
3 // Purpose: wxStandardPaths implementation for Win32
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/dynlib.h"
34 #include "wx/filename.h"
36 #include "wx/stdpaths.h"
38 #include "wx/msw/private.h"
39 #include "wx/msw/wrapshl.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 typedef HRESULT (WINAPI
*SHGetFolderPath_t
)(HWND
, int, HANDLE
, DWORD
, LPTSTR
);
46 typedef HRESULT (WINAPI
*SHGetSpecialFolderPath_t
)(HWND
, LPTSTR
, int, BOOL
);
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // used in our wxLogTrace messages
53 static const wxChar
*TRACE_MASK
= _T("stdpaths");
56 #define CSIDL_APPDATA 0x001a
59 #ifndef CSIDL_LOCAL_APPDATA
60 #define CSIDL_LOCAL_APPDATA 0x001c
63 #ifndef CSIDL_COMMON_APPDATA
64 #define CSIDL_COMMON_APPDATA 0x0023
67 #ifndef CSIDL_PROGRAM_FILES
68 #define CSIDL_PROGRAM_FILES 0x0026
71 #ifndef SHGFP_TYPE_CURRENT
72 #define SHGFP_TYPE_CURRENT 0
75 #ifndef SHGFP_TYPE_DEFAULT
76 #define SHGFP_TYPE_DEFAULT 1
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
87 pSHGetFolderPath
= NULL
;
88 pSHGetSpecialFolderPath
= NULL
;
92 SHGetFolderPath_t pSHGetFolderPath
;
93 SHGetSpecialFolderPath_t pSHGetSpecialFolderPath
;
98 // in spite of using a static variable, this is MT-safe as in the worst case it
99 // results in initializing the function pointer several times -- but this is
101 static ShellFunctions gs_shellFuncs
;
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 static void ResolveShellFunctions()
109 // start with the newest functions, fall back to the oldest ones
111 wxString
shellDllName(_T("coredll"));
113 // first check for SHGetFolderPath (shell32.dll 5.0)
114 wxString
shellDllName(_T("shell32"));
117 wxDynamicLibrary
dllShellFunctions( shellDllName
);
118 if ( !dllShellFunctions
.IsLoaded() )
120 wxLogTrace(TRACE_MASK
, _T("Failed to load %s.dll"), shellDllName
.c_str() );
123 // don't give errors if the functions are unavailable, we're ready to deal
129 static const wchar_t UNICODE_SUFFIX
= L
''; // WinCE SH functions don't seem to have 'W'
131 static const wchar_t UNICODE_SUFFIX
= L
'W';
134 static const char UNICODE_SUFFIX
= 'A';
135 #endif // Unicode/!Unicode
137 wxString
funcname(_T("SHGetFolderPath"));
138 gs_shellFuncs
.pSHGetFolderPath
=
139 (SHGetFolderPath_t
)dllShellFunctions
.GetSymbol(funcname
+ UNICODE_SUFFIX
);
141 // then for SHGetSpecialFolderPath (shell32.dll 4.71)
142 if ( !gs_shellFuncs
.pSHGetFolderPath
)
144 funcname
= _T("SHGetSpecialFolderPath");
145 gs_shellFuncs
.pSHGetSpecialFolderPath
= (SHGetSpecialFolderPath_t
)
146 dllShellFunctions
.GetSymbol(funcname
+ UNICODE_SUFFIX
);
149 // finally we fall back on SHGetSpecialFolderLocation (shell32.dll 4.0),
150 // but we don't need to test for it -- it is available even under Win95
152 // shell32.dll is going to be unloaded, but it still remains in memory
153 // because we also link to it statically, so it's ok
155 gs_shellFuncs
.initialized
= true;
158 // ============================================================================
159 // wxStandardPaths implementation
160 // ============================================================================
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
167 wxString
wxStandardPaths::DoGetDirectory(int csidl
)
169 if ( !gs_shellFuncs
.initialized
)
170 ResolveShellFunctions();
175 // test whether the function is available during compile-time (it must be
176 // defined as either "SHGetFolderPathA" or "SHGetFolderPathW")
177 #ifdef SHGetFolderPath
178 // and now test whether we have it during run-time
179 if ( gs_shellFuncs
.pSHGetFolderPath
)
181 hr
= gs_shellFuncs
.pSHGetFolderPath
183 NULL
, // parent window, not used
185 NULL
, // access token (current user)
186 SHGFP_TYPE_CURRENT
, // current path, not just default value
187 wxStringBuffer(dir
, MAX_PATH
)
190 // somewhat incredibly, the error code in the Unicode version is
191 // different from the one in ASCII version for this function
198 // directory doesn't exist, maybe we can get its default value?
199 hr
= gs_shellFuncs
.pSHGetFolderPath
205 wxStringBuffer(dir
, MAX_PATH
)
209 #endif // SHGetFolderPath
211 #ifdef SHGetSpecialFolderPath
212 if ( FAILED(hr
) && gs_shellFuncs
.pSHGetSpecialFolderPath
)
214 hr
= gs_shellFuncs
.pSHGetSpecialFolderPath
216 NULL
, // parent window
217 wxStringBuffer(dir
, MAX_PATH
),
219 FALSE
// don't create if doesn't exist
222 #endif // SHGetSpecialFolderPath
224 // SHGetSpecialFolderLocation should be available with all compilers and
225 // under all Win32 systems, so don't test for it (and as it doesn't exist
226 // in "A" and "W" versions anyhow, testing would be more involved, too)
230 hr
= SHGetSpecialFolderLocation(NULL
, csidl
, &pidl
);
234 // creating this temp object has (nice) side effect of freeing pidl
235 dir
= wxItemIdList(pidl
).GetPath();
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 wxString
wxStandardPaths::GetConfigDir() const
248 return AppendAppName(DoGetDirectory(CSIDL_COMMON_APPDATA
));
251 wxString
wxStandardPaths::GetUserConfigDir() const
253 return DoGetDirectory(CSIDL_APPDATA
);
256 wxString
wxStandardPaths::GetDataDir() const
258 // under Windows each program is usually installed in its own directory and
259 // so its datafiles are in the same directory as its main executable
260 return wxFileName(wxGetFullModuleName()).GetPath();
263 wxString
wxStandardPaths::GetUserDataDir() const
265 return AppendAppName(GetUserConfigDir());
268 wxString
wxStandardPaths::GetUserLocalDataDir() const
270 return AppendAppName(DoGetDirectory(CSIDL_LOCAL_APPDATA
));
273 wxString
wxStandardPaths::GetPluginsDir() const
275 return wxFileName(wxGetFullModuleName()).GetPath();
279 // ============================================================================
280 // wxStandardPathsWin16 implementation
281 // ============================================================================
283 wxString
wxStandardPathsWin16::GetConfigDir() const
285 // this is for compatibility with earlier wxFileConfig versions
286 // which used the Windows directory for the global files
289 if ( !::GetWindowsDirectory(wxStringBuffer(dir
, MAX_PATH
), MAX_PATH
) )
291 wxLogLastError(_T("GetWindowsDirectory"));
294 // TODO: use CSIDL_WINDOWS (eVC4, possibly not eVC3)
295 dir
= wxT("\\Windows");
301 wxString
wxStandardPathsWin16::GetUserConfigDir() const
303 // again, for wxFileConfig which uses $HOME for its user config file
304 return wxGetHomeDir();
307 #endif // wxUSE_STDPATHS