OSDN Git Service

26bb5d6d64c67aa3d7ffb610542f906630735b40
[mutilities/MUtilities.git] / src / OSSupport_Win32.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 #pragma once
23
24 //Internal
25 #include <MUtils/Global.h>
26 #include <MUtils/OSSupport.h>
27 #include "CriticalSection_Win32.h"
28
29 //Win32 API
30 #define WIN32_LEAN_AND_MEAN 1
31 #include <Windows.h>
32 #include <Objbase.h>
33
34 //Qt
35 #include <QMap>
36 #include <QReadWriteLock>
37 #include <QLibrary>
38 #include <QDir>
39
40 //Main thread ID
41 static const DWORD g_main_thread_id = GetCurrentThreadId();
42
43 ///////////////////////////////////////////////////////////////////////////////
44 // KNWON FOLDERS
45 ///////////////////////////////////////////////////////////////////////////////
46
47 typedef HRESULT (WINAPI *SHGetKnownFolderPath_t)(const GUID &rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
48 typedef HRESULT (WINAPI *SHGetFolderPath_t)(HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath);
49
50 static QMap<size_t, QString>* g_known_folders_map;
51 static SHGetKnownFolderPath_t g_known_folders_fpGetKnownFolderPath;
52 static SHGetFolderPath_t      g_known_folders_fpGetFolderPath;
53 static QReadWriteLock         g_known_folders_lock;
54
55 const QString &MUtils::OS::known_folder(known_folder_t folder_id)
56 {
57         static const int CSIDL_FLAG_CREATE = 0x8000;
58         typedef enum { KF_FLAG_CREATE = 0x00008000 } kf_flags_t;
59         
60         struct
61         {
62                 const int csidl;
63                 const GUID guid;
64         }
65         static s_folders[] =
66         {
67                 { 0x001c, {0xF1B32785,0x6FBA,0x4FCF,{0x9D,0x55,0x7B,0x8E,0x7F,0x15,0x70,0x91}} },  //CSIDL_LOCAL_APPDATA
68                 { 0x0026, {0x905e63b6,0xc1bf,0x494e,{0xb2,0x9c,0x65,0xb7,0x32,0xd3,0xd2,0x1a}} },  //CSIDL_PROGRAM_FILES
69                 { 0x0024, {0xF38BF404,0x1D43,0x42F2,{0x93,0x05,0x67,0xDE,0x0B,0x28,0xFC,0x23}} },  //CSIDL_WINDOWS_FOLDER
70                 { 0x0025, {0x1AC14E77,0x02E7,0x4E5D,{0xB7,0x44,0x2E,0xB1,0xAE,0x51,0x98,0xB7}} },  //CSIDL_SYSTEM_FOLDER
71         };
72
73         size_t folderId = size_t(-1);
74
75         switch(folder_id)
76         {
77                 case FOLDER_LOCALAPPDATA: folderId = 0; break;
78                 case FOLDER_PROGRAMFILES: folderId = 1; break;
79                 case FOLDER_SYSTROOT_DIR: folderId = 2; break;
80                 case FOLDER_SYSTEMFOLDER: folderId = 3; break;
81         }
82
83         if(folderId == size_t(-1))
84         {
85                 qWarning("Invalid 'known' folder was requested!");
86                 return *reinterpret_cast<QString*>(NULL);
87         }
88
89         QReadLocker readLock(&g_known_folders_lock);
90
91         //Already in cache?
92         if(g_known_folders_map)
93         {
94                 if(g_known_folders_map->contains(folderId))
95                 {
96                         return g_known_folders_map->operator[](folderId);
97                 }
98         }
99
100         //Obtain write lock to initialize
101         readLock.unlock();
102         QWriteLocker writeLock(&g_known_folders_lock);
103
104         //Still not in cache?
105         if(g_known_folders_map)
106         {
107                 if(g_known_folders_map->contains(folderId))
108                 {
109                         return g_known_folders_map->operator[](folderId);
110                 }
111         }
112
113         //Initialize on first call
114         if(!g_known_folders_map)
115         {
116                 QLibrary shell32("shell32.dll");
117                 if(shell32.load())
118                 {
119                         g_known_folders_fpGetFolderPath =      (SHGetFolderPath_t)      shell32.resolve("SHGetFolderPathW");
120                         g_known_folders_fpGetKnownFolderPath = (SHGetKnownFolderPath_t) shell32.resolve("SHGetKnownFolderPath");
121                 }
122                 g_known_folders_map = new QMap<size_t, QString>();
123         }
124
125         QString folderPath;
126
127         //Now try to get the folder path!
128         if(g_known_folders_fpGetKnownFolderPath)
129         {
130                 WCHAR *path = NULL;
131                 if(g_known_folders_fpGetKnownFolderPath(s_folders[folderId].guid, KF_FLAG_CREATE, NULL, &path) == S_OK)
132                 {
133                         //MessageBoxW(0, path, L"SHGetKnownFolderPath", MB_TOPMOST);
134                         QDir folderTemp = QDir(QDir::fromNativeSeparators(MUTILS_WCHAR2QSTR(path)));
135                         if(folderTemp.exists())
136                         {
137                                 folderPath = folderTemp.canonicalPath();
138                         }
139                         CoTaskMemFree(path);
140                 }
141         }
142         else if(g_known_folders_fpGetFolderPath)
143         {
144                 QScopedArrayPointer<WCHAR> path(new WCHAR[4096]);
145                 if(g_known_folders_fpGetFolderPath(NULL, s_folders[folderId].csidl | CSIDL_FLAG_CREATE, NULL, NULL, path.data()) == S_OK)
146                 {
147                         //MessageBoxW(0, path, L"SHGetFolderPathW", MB_TOPMOST);
148                         QDir folderTemp = QDir(QDir::fromNativeSeparators(MUTILS_WCHAR2QSTR(path.data())));
149                         if(folderTemp.exists())
150                         {
151                                 folderPath = folderTemp.canonicalPath();
152                         }
153                 }
154         }
155
156         //Update cache
157         g_known_folders_map->insert(folderId, folderPath);
158         return g_known_folders_map->operator[](folderId);
159 }
160
161 ///////////////////////////////////////////////////////////////////////////////
162 // FATAL EXIT
163 ///////////////////////////////////////////////////////////////////////////////
164
165 static CriticalSection g_fatal_exit_lock;
166 static volatile bool   g_fatal_exit_flag = true;
167
168 static DWORD WINAPI fatal_exit_helper(LPVOID lpParameter)
169 {
170         MessageBoxA(NULL, ((LPCSTR) lpParameter), "LameXP - Guru Meditation", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_TOPMOST | MB_SETFOREGROUND);
171         return 0;
172 }
173
174 void MUtils::OS::fatal_exit(const char* const errorMessage)
175 {
176         g_fatal_exit_lock.enter();
177         
178         if(!g_fatal_exit_flag)
179         {
180                 return; /*prevent recursive invocation*/
181         }
182
183         g_fatal_exit_flag = false;
184
185         if(g_main_thread_id != GetCurrentThreadId())
186         {
187                 if(HANDLE hThreadMain = OpenThread(THREAD_SUSPEND_RESUME, FALSE, g_main_thread_id))
188                 {
189                         SuspendThread(hThreadMain); /*stop main thread*/
190                 }
191         }
192
193         if(HANDLE hThread = CreateThread(NULL, 0, fatal_exit_helper, (LPVOID) errorMessage, 0, NULL))
194         {
195                 WaitForSingleObject(hThread, INFINITE);
196         }
197
198         for(;;)
199         {
200                 TerminateProcess(GetCurrentProcess(), 666);
201         }
202 }
203
204 ///////////////////////////////////////////////////////////////////////////////