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