From 128c2dd624a78ef1e218871a4e4729625be56c04 Mon Sep 17 00:00:00 2001 From: Alex Borisov Date: Sat, 21 Jan 2012 17:43:02 +0200 Subject: Restart Proxifier to apply new configuration --- client/Proxifier.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) (limited to 'client/Proxifier.cpp') 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 #ifdef WIN32 + #include + #include #include #endif #include @@ -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; -- cgit v1.2.3