diff options
author | Alex Borisov <b0ric.alex@gmail.com> | 2012-01-21 17:43:02 +0200 |
---|---|---|
committer | Alex Borisov <b0ric.alex@gmail.com> | 2012-01-21 17:43:02 +0200 |
commit | 128c2dd624a78ef1e218871a4e4729625be56c04 (patch) | |
tree | 1cdd5e6fb619d15909116fe345ed15a841b5e4ec /client/Proxifier.cpp | |
parent | 6f8e7cae703145cd6eb103c08681045ced28f5e5 (diff) |
Restart Proxifier to apply new configuration
Diffstat (limited to 'client/Proxifier.cpp')
-rw-r--r-- | client/Proxifier.cpp | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/client/Proxifier.cpp b/client/Proxifier.cpp index 28964af..46e4339 100644 --- a/client/Proxifier.cpp +++ b/client/Proxifier.cpp @@ -1,6 +1,8 @@ #include <algorithm> #ifdef WIN32 + #include <windows.h> + #include <tlhelp32.h> #include <shlobj.h> #endif #include <QtCore> @@ -74,7 +76,7 @@ Proxifier::Proxifier(): valid(false) QString file = str; file += pathSlash + folderName + pathSlash + fileName; QFileInfo filePathInfo(file); - Logger::Info("Checking path: %s\n",file.toStdString().c_str()); + Logger::Info("Checking path: %s\n", file.toStdString().c_str()); if (filePathInfo.exists()) { filePath = filePathInfo.absoluteFilePath(); @@ -97,6 +99,62 @@ Proxifier::Proxifier(): valid(false) #endif } +bool Proxifier::Restart() +{ + char execName[] = "Proxifier.exe"; + Logger::Info("Restarting Proxifier process\n"); +#ifdef WIN32 + HANDLE hProcessSnap; + HANDLE hProcess; + PROCESSENTRY32 pe32; + + // Takes a snapshot of all the processes + hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (hProcessSnap == INVALID_HANDLE_VALUE) + { + return false; + } + + // interate snapshot and find all process to be killed + pe32.dwSize = sizeof(PROCESSENTRY32); + if (!Process32First(hProcessSnap, &pe32)) + { + CloseHandle(hProcessSnap); + return false; + } + do + { + QString procName = QString::fromWCharArray(pe32.szExeFile); + if (QString::compare(procName, QString::fromLocal8Bit(execName)) == 0) + { + Logger::Trace("Killing Proxifier process\n"); + hProcess = OpenProcess(PROCESS_TERMINATE, 0, pe32.th32ProcessID); + TerminateProcess(hProcess, 0); + CloseHandle(hProcess); + } + } + while (Process32Next(hProcessSnap, &pe32)); + + // start over again + QString folderName = "Proxifier"; + QString pathSlash = "\\"; + TCHAR programFiles[MAX_PATH]; + if (SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, 0, programFiles) != S_FALSE) + { + // check in %ProgramFiles(x86)% + char *str = wstrdup(programFiles); + QString program = str; + program += pathSlash + folderName + pathSlash + execName; + program.prepend("\""); + program.append("\""); + Logger::Trace("Starting process: %s\n", program.toStdString().c_str()); + + return QProcess::startDetached(program); + } +#endif + return false; +} + bool Proxifier::IsValid() { return valid; |