Mercurial > trustbridge
view ui/processhelp_win.cpp @ 721:abc76948d8e5
(Auto) Update translations
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Wed, 02 Jul 2014 17:04:02 +0200 |
parents | 175370634226 |
children | 20ca94680003 |
line wrap: on
line source
/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik * Software engineering by Intevation GmbH * * This file is Free Software under the GNU GPL (v>=2) * and comes with ABSOLUTELY NO WARRANTY! * See LICENSE.txt for details. */ #ifdef WIN32 #include "processhelp.h" #include "strhelp.h" #include "util.h" #include <windows.h> #include <tlhelp32.h> #include <psapi.h> #include <unistd.h> #include <QDebug> struct EnumWindowsStruct { EnumWindowsStruct() : windowId(0) {} DWORD pid; HWND windowId; }; BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { if (GetWindowLong(hwnd, GWL_STYLE) & WS_VISIBLE) { DWORD pidwin; GetWindowThreadProcessId(hwnd, &pidwin); if (pidwin == ((EnumWindowsStruct *)lParam)->pid) { ((EnumWindowsStruct *)lParam)->windowId = hwnd; return FALSE; } } return TRUE; } static HANDLE getProcessHandle(int processID) { return OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE, false, processID); } const QList<int> ProcessHelp::getProcessesIdForName(const QString &processName) { HANDLE h; PROCESSENTRY32 pe32; QList <int> pids; h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (h == INVALID_HANDLE_VALUE) { return pids; } pe32.dwSize = sizeof(PROCESSENTRY32); // Necessary according to MSDN if (!Process32First(h, &pe32)) { return pids; } pids.clear(); const QString processNameLower = processName.toLower().replace(".exe", ""); const QString processNameExe = processNameLower + ".exe"; do { const QString exeFile = QString::fromWCharArray(pe32.szExeFile).toLower(); if (exeFile == processNameLower || exeFile == processNameExe) { PSID user_sid = get_process_owner(GetCurrentProcess()); if (user_sid) { // Also check that we are the owner of that process HANDLE hProcess = getProcessHandle(pe32.th32ProcessID); if (!hProcess) { continue; } PSID sid = get_process_owner(hProcess); PSID userSid = get_process_owner(GetCurrentProcess()); if (!sid || (userSid && !EqualSid(userSid, sid))) { free(sid); continue; } } pids.append((int)pe32.th32ProcessID); qDebug() << "found PID: " << (int)pe32.th32ProcessID; } } while (Process32Next(h, &pe32)); CloseHandle(h); return pids; } bool ProcessHelp::otherProcessesExist(const QString &processName) { const QList<int> pids = getProcessesIdForName(processName); int myPid = getpid(); foreach(int pid, pids) { if (myPid != pid) { qDebug() << "Found another process with id: " << pid; return true; } } return false; } void ProcessHelp::activateWindowForProcess(const QString &executableName) { const QList<int> pids = getProcessesIdForName(executableName); int myPid = getpid(); int foundPid = 0; foreach(int pid, pids) { if (myPid != pid) { qDebug() << "activateWindowForProcess(): PID to activate:" << pid; foundPid = pid; break; } } if (foundPid == 0) { return; } EnumWindowsStruct winStruct; winStruct.pid = foundPid; EnumWindows(EnumWindowsProc, (LPARAM)&winStruct); if (winStruct.windowId == 0) { return; } SetForegroundWindow(winStruct.windowId); } #endif // WIN32