From e163a8046724c1c79119ed0642ff3b0b4ecd2e00 Mon Sep 17 00:00:00 2001 From: lordmulder Date: Wed, 8 Dec 2010 20:53:30 +0100 Subject: [PATCH] Implement a method that will detect the TEMP folder independent from the %TMP% environment variable on Windows XP (and probably also 2000) too. --- src/Global.cpp | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Global.cpp b/src/Global.cpp index 01d20c9c..01d19ac3 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -608,10 +608,13 @@ QString lamexp_rand_str(void) */ const QString &lamexp_temp_folder(void) { + typedef HRESULT (WINAPI *SHGetKnownFolderPathFun)(__in const GUID &rfid, __in DWORD dwFlags, __in HANDLE hToken, __out PWSTR *ppszPath); + typedef HRESULT (WINAPI *SHGetFolderPathFun)(__in HWND hwndOwner, __in int nFolder, __in HANDLE hToken, __in DWORD dwFlags, __out LPWSTR pszPath); + + const char *TEMP_STR = "Temp"; + const int CSIDL_LOCAL_APPDATA = 0x001c; const GUID LocalAppDataID={0xF1B32785,0x6FBA,0x4FCF,{0x9D,0x55,0x7B,0x8E,0x7F,0x15,0x70,0x91}}; const GUID LocalAppDataLowID={0xA520A1A4,0x1780,0x4FF6,{0xBD,0x18,0x16,0x73,0x43,0xC5,0xAF,0x16}}; - - typedef HANDLE (WINAPI *SHGetKnownFolderPathFun)(__in const GUID &rfid, __in DWORD dwFlags, __in HANDLE hToken, __out PWSTR *ppszPath); if(g_lamexp_temp_folder.isEmpty()) { @@ -619,21 +622,21 @@ const QString &lamexp_temp_folder(void) QLibrary Kernel32Lib("shell32.dll"); SHGetKnownFolderPathFun SHGetKnownFolderPathPtr = (SHGetKnownFolderPathFun) Kernel32Lib.resolve("SHGetKnownFolderPath"); + SHGetFolderPathFun SHGetFolderPathPtr = (SHGetFolderPathFun) Kernel32Lib.resolve("SHGetFolderPathW"); if(SHGetKnownFolderPathPtr) { WCHAR *localAppDataPath = NULL; if(SHGetKnownFolderPathPtr(LocalAppDataID, 0x00008000, NULL, &localAppDataPath) == S_OK) { - MessageBoxW(NULL, localAppDataPath, L"LocalAppData", MB_TOPMOST); QDir localAppData = QDir(QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast(localAppDataPath)))); if(localAppData.exists()) { - if(!localAppData.entryList(QDir::AllDirs).contains("Temp")) + if(!localAppData.entryList(QDir::AllDirs).contains(TEMP_STR)) { - localAppData.mkdir("Temp"); + localAppData.mkdir(TEMP_STR); } - if(localAppData.cd("Temp")) + if(localAppData.cd(TEMP_STR)) { temp.setPath(localAppData.canonicalPath()); } @@ -641,6 +644,26 @@ const QString &lamexp_temp_folder(void) CoTaskMemFree(localAppDataPath); } } + else if(SHGetFolderPathPtr) + { + WCHAR *localAppDataPath = new WCHAR[4096]; + if(SHGetFolderPathPtr(NULL, CSIDL_LOCAL_APPDATA, NULL, NULL, localAppDataPath) == S_OK) + { + QDir localAppData = QDir(QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast(localAppDataPath)))); + if(localAppData.exists()) + { + if(!localAppData.entryList(QDir::AllDirs).contains(TEMP_STR)) + { + localAppData.mkdir(TEMP_STR); + } + if(localAppData.cd(TEMP_STR)) + { + temp.setPath(localAppData.canonicalPath()); + } + } + } + delete [] localAppDataPath; + } if(!temp.exists()) {