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"
31 #include "wx/dynlib.h"
32 #include "wx/filename.h"
34 #include "wx/stdpaths.h"
36 #include "wx/msw/private.h"
37 #include "wx/msw/wrapshl.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 typedef HRESULT (WINAPI
*SHGetFolderPath_t
)(HWND
, int, HANDLE
, DWORD
, LPTSTR
);
44 typedef HRESULT (WINAPI
*SHGetSpecialFolderPath_t
)(HWND
, LPTSTR
, int, BOOL
);
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // used in our wxLogTrace messages
51 static const wxChar
*TRACE_MASK
= _T("stdpaths");
53 #ifndef CSIDL_LOCAL_APPDATA
54 #define CSIDL_LOCAL_APPDATA 0x001c
57 #ifndef CSIDL_COMMON_APPDATA
58 #define CSIDL_COMMON_APPDATA 0x0023
61 #ifndef CSIDL_PROGRAM_FILES
62 #define CSIDL_PROGRAM_FILES 0x0026
65 #ifndef SHGFP_TYPE_CURRENT
66 #define SHGFP_TYPE_CURRENT 0
69 #ifndef SHGFP_TYPE_DEFAULT
70 #define SHGFP_TYPE_DEFAULT 1
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
81 pSHGetFolderPath
= NULL
;
82 pSHGetSpecialFolderPath
= NULL
;
86 SHGetFolderPath_t pSHGetFolderPath
;
87 SHGetSpecialFolderPath_t pSHGetSpecialFolderPath
;
92 // in spite of using a static variable, this is MT-safe as in the worst case it
93 // results in initializing the function pointer several times -- but this is
95 static ShellFunctions gs_shellFuncs
;
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 static void ResolveShellFunctions()
103 // don't give errors if the functions are unavailable, we're ready to deal
107 // start with the newest functions, fall back to the oldest ones
109 // first check for SHGetFolderPath (shell32.dll 5.0)
110 wxDynamicLibrary
dllShell32(_T("shell32"));
111 if ( !dllShell32
.IsLoaded() )
113 wxLogTrace(TRACE_MASK
, _T("Failed to load shell32.dll"));
117 static const wchar_t UNICODE_SUFFIX
= L
'W';
119 static const char UNICODE_SUFFIX
= 'A';
120 #endif // Unicode/!Unicode
122 wxString
funcname(_T("SHGetFolderPath"));
123 gs_shellFuncs
.pSHGetFolderPath
=
124 (SHGetFolderPath_t
)dllShell32
.GetSymbol(funcname
+ UNICODE_SUFFIX
);
126 // then for SHGetSpecialFolderPath (shell32.dll 4.71)
127 if ( !gs_shellFuncs
.pSHGetFolderPath
)
129 funcname
= _T("SHGetSpecialFolderPath");
130 gs_shellFuncs
.pSHGetSpecialFolderPath
= (SHGetSpecialFolderPath_t
)
131 dllShell32
.GetSymbol(funcname
+ UNICODE_SUFFIX
);
134 // finally we fall back on SHGetSpecialFolderLocation (shell32.dll 4.0),
135 // but we don't need to test for it -- it is available even under Win95
137 // shell32.dll is going to be unloaded, but it still remains in memory
138 // because we also link to it statically, so it's ok
140 gs_shellFuncs
.initialized
= true;
143 // ============================================================================
144 // wxStandardPaths implementation
145 // ============================================================================
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
152 wxString
wxStandardPaths::DoGetDirectory(int csidl
)
154 if ( !gs_shellFuncs
.initialized
)
155 ResolveShellFunctions();
160 // test whether the function is available during compile-time (it must be
161 // defined as either "SHGetFolderPathA" or "SHGetFolderPathW")
162 #ifdef SHGetFolderPath
163 // and now test whether we have it during run-time
164 if ( gs_shellFuncs
.pSHGetFolderPath
)
166 hr
= gs_shellFuncs
.pSHGetFolderPath
168 NULL
, // parent window, not used
170 NULL
, // access token (current user)
171 SHGFP_TYPE_CURRENT
, // current path, not just default value
172 wxStringBuffer(dir
, MAX_PATH
)
175 // somewhat incredibly, the error code in the Unicode version is
176 // different from the one in ASCII version for this function
183 // directory doesn't exist, maybe we can get its default value?
184 hr
= gs_shellFuncs
.pSHGetFolderPath
190 wxStringBuffer(dir
, MAX_PATH
)
194 #endif // SHGetFolderPath
196 #ifdef SHGetSpecialFolderPath
197 if ( FAILED(hr
) && gs_shellFuncs
.pSHGetSpecialFolderPath
)
199 hr
= gs_shellFuncs
.pSHGetSpecialFolderPath
201 NULL
, // parent window
202 wxStringBuffer(dir
, MAX_PATH
),
204 FALSE
// don't create if doesn't exist
207 #endif // SHGetSpecialFolderPath
209 // SHGetSpecialFolderLocation should be available with all compilers and
210 // under all Win32 systems, so don't test for it (and as it doesn't exist
211 // in "A" and "W" versions anyhow, testing would be more involved, too)
215 hr
= SHGetSpecialFolderLocation(NULL
, csidl
, &pidl
);
219 // creating this temp object has (nice) side effect of freeing pidl
220 dir
= wxItemIdList(pidl
).GetPath();
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 wxString
wxStandardPaths::GetConfigDir() const
233 return AppendAppName(DoGetDirectory(CSIDL_COMMON_APPDATA
));
236 wxString
wxStandardPaths::GetUserConfigDir() const
238 return DoGetDirectory(CSIDL_APPDATA
);
241 wxString
wxStandardPaths::GetDataDir() const
243 return AppendAppName(DoGetDirectory(CSIDL_PROGRAM_FILES
));
246 wxString
wxStandardPaths::GetUserDataDir() const
248 return AppendAppName(GetUserConfigDir());
251 wxString
wxStandardPaths::GetUserLocalDataDir() const
253 return AppendAppName(DoGetDirectory(CSIDL_LOCAL_APPDATA
));
256 wxString
wxStandardPaths::GetPluginsDir() const
258 return wxFileName(wxGetFullModuleName()).GetPath();
262 // ============================================================================
263 // wxStandardPathsWin16 implementation
264 // ============================================================================
266 wxString
wxStandardPathsWin16::GetConfigDir() const
268 // this is for compatibility with earlier wxFileConfig versions
269 // which used the Windows directory for the global files
272 if ( !::GetWindowsDirectory(wxStringBuffer(dir
, MAX_PATH
), MAX_PATH
) )
274 wxLogLastError(_T("GetWindowsDirectory"));
278 // eVC4 - use CSIDL_WINDOWS
279 // eVC3 - probably not possible through API
285 wxString
wxStandardPathsWin16::GetUserConfigDir() const
287 // again, for wxFileConfig which uses $HOME for its user config file
288 return wxGetHomeDir();