00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "win32.h"
00018
00019 #include <QDir>
00020 #include <QLibrary>
00021 #include <QtDebug>
00022
00023 #include <tlhelp32.h>
00024 #include <shlobj.h>
00025
00026 #if defined(UNICODE)
00027
00028
00029 #undef PROCESSENTRY32
00030 #undef LPPROCESSENTRY32
00031 #undef Process32First
00032 #undef Process32Next
00033 #endif
00034
00035
00036
00037 typedef HANDLE (WINAPI *CreateToolhelp32Snapshot_fn)(DWORD, DWORD);
00038 typedef BOOL (WINAPI *Process32First_fn)(HANDLE, LPPROCESSENTRY32);
00039 typedef BOOL (WINAPI *Process32Next_fn)(HANDLE, LPPROCESSENTRY32);
00040
00041
00042
00043
00044 QString
00045 win32_get_folder_location(int folder, QString defaultPath)
00046 {
00047 TCHAR path[MAX_PATH+1];
00048 LPITEMIDLIST idl;
00049 IMalloc *m;
00050 HRESULT result;
00051
00052
00053
00054
00055
00056
00057
00058
00059 if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, folder, &idl))) {
00060
00061 result = SHGetPathFromIDList(idl, path);
00062 SHGetMalloc(&m);
00063 if (m) {
00064 m->Release();
00065 }
00066 if (SUCCEEDED(result)) {
00067 #if defined(UNICODE)
00068 return QString::fromUtf16(static_cast<const ushort *>(path));
00069 #else
00070 return QString::fromLocal8Bit(static_cast<const char *>(path));
00071 #endif
00072 }
00073 }
00074 return defaultPath;
00075 }
00076
00077
00078 QString
00079 win32_program_files_folder()
00080 {
00081 return win32_get_folder_location(
00082 CSIDL_PROGRAM_FILES, QDir::rootPath() + "\\Program Files");
00083 }
00084
00085
00086 QString
00087 win32_app_data_folder()
00088 {
00089 return win32_get_folder_location(
00090 CSIDL_APPDATA, QDir::homePath() + "\\Application Data");
00091 }
00092
00093
00094
00095 QString
00096 win32_registry_get_key_value(QString keyLocation, QString keyName)
00097 {
00098 HKEY key;
00099 char data[255] = {0};
00100 DWORD size = sizeof(data);
00101
00102
00103 if (RegOpenKeyExA(HKEY_CURRENT_USER,
00104 qPrintable(keyLocation),
00105 0L, KEY_READ, &key) == ERROR_SUCCESS) {
00106
00107
00108 RegQueryValueExA(key, qPrintable(keyName),
00109 NULL, NULL, (LPBYTE)data, &size);
00110 }
00111
00112
00113 RegCloseKey(key);
00114
00115 return QString(data);
00116 }
00117
00118
00119 void
00120 win32_registry_set_key_value(QString keyLocation, QString keyName, QString keyValue)
00121 {
00122 HKEY key;
00123
00124
00125 if (RegOpenKeyExA(HKEY_CURRENT_USER,
00126 qPrintable(keyLocation),
00127 0, KEY_WRITE, &key) != ERROR_SUCCESS) {
00128
00129
00130 RegCreateKeyExA(HKEY_CURRENT_USER,
00131 qPrintable(keyLocation),
00132 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
00133 &key, NULL);
00134 }
00135
00136
00137 RegSetValueExA(key, qPrintable(keyName), 0, REG_SZ,
00138 (BYTE *)qPrintable(keyValue),
00139 (DWORD)keyValue.length() + 1);
00140
00141
00142 RegCloseKey(key);
00143 }
00144
00145
00146 void
00147 win32_registry_remove_key(QString keyLocation, QString keyName)
00148 {
00149 HKEY key;
00150
00151
00152 if (RegOpenKeyExA(HKEY_CURRENT_USER,
00153 qPrintable(keyLocation),
00154 0, KEY_SET_VALUE, &key) == ERROR_SUCCESS) {
00155
00156
00157 RegDeleteValueA(key, qPrintable(keyName));
00158 }
00159
00160
00161 RegCloseKey(key);
00162 }
00163
00164
00165
00166
00167 BOOL CALLBACK
00168 quitWindowCallback(HWND hwnd, LPARAM targetPID)
00169 {
00170 DWORD hwndPID = 0;
00171
00172
00173
00174 GetWindowThreadProcessId(hwnd, &hwndPID);
00175 if (hwndPID == (DWORD)targetPID)
00176 PostMessage(hwnd, WM_QUIT, 0, (LPARAM)NULL);
00177 return TRUE;
00178 }
00179
00180
00181
00182
00183
00184 void
00185 win32_end_process_by_pid(DWORD pid)
00186 {
00187
00188 EnumWindows(&quitWindowCallback, (LPARAM)pid);
00189
00190
00191
00192
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 void
00204 win32_end_process_by_filename(QString filename)
00205 {
00206
00207 QHash<qint64, QString> procList = win32_process_list();
00208
00209
00210
00211 if (procList.isEmpty()) {
00212 return;
00213 }
00214
00215
00216 QHashIterator<qint64, QString> i(procList);
00217 while (i.hasNext()) {
00218 i.next();
00219 if (i.value().toLower() == filename) {
00220
00221 win32_end_process_by_pid((DWORD)i.key());
00222 }
00223 }
00224 }
00225
00226
00227
00228 QHash<qint64, QString>
00229 win32_process_list()
00230 {
00231 QHash<qint64, QString> procList;
00232 CreateToolhelp32Snapshot_fn pCreateToolhelp32Snapshot;
00233 Process32First_fn pProcess32First;
00234 Process32Next_fn pProcess32Next;
00235 HANDLE hSnapshot;
00236 PROCESSENTRY32 proc;
00237 QString exeFile;
00238 qint64 pid;
00239
00240
00241 pCreateToolhelp32Snapshot =
00242 (CreateToolhelp32Snapshot_fn)QLibrary::resolve("kernel32", "CreateToolhelp32Snapshot");
00243 pProcess32First = (Process32First_fn)QLibrary::resolve("kernel32", "Process32First");
00244 pProcess32Next = (Process32Next_fn)QLibrary::resolve("kernel32", "Process32Next");
00245
00246 if (!pCreateToolhelp32Snapshot || !pProcess32First || !pProcess32Next) {
00247 qWarning("Unable to load tool help functions. Running process information "
00248 "will be unavailable.");
00249 return QHash<qint64, QString>();
00250 }
00251
00252
00253 hSnapshot = pCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
00254 if (hSnapshot != INVALID_HANDLE_VALUE) {
00255 proc.dwSize = sizeof(PROCESSENTRY32);
00256
00257
00258 if (pProcess32First(hSnapshot, &proc)) {
00259 do {
00260
00261 pid = (qint64)proc.th32ProcessID;
00262 exeFile = QString::fromAscii((const char *)proc.szExeFile);
00263
00264
00265 procList.insert(pid, exeFile);
00266 } while (pProcess32Next(hSnapshot, &proc));
00267 }
00268 CloseHandle(hSnapshot);
00269 }
00270 return procList;
00271 }
00272