annotate ui/processhelp_win.cpp @ 754:27043d74dc90

(Issue25) Align header contents in their own column. We now also stretch column 3 so that the contents are aligned with the descriptive labels without a space in between. Sadly this causes the quit button to be resized to it's minimum instead of sharing the space with the installation button as the installation button is so large that it squeezes the push button.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 07 Jul 2014 12:38:33 +0200
parents 175370634226
children 20ca94680003
rev   line source
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1 /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2 * Software engineering by Intevation GmbH
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
3 *
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
4 * This file is Free Software under the GNU GPL (v>=2)
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY!
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
6 * See LICENSE.txt for details. */
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
7
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
8 #ifdef WIN32
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
9 #include "processhelp.h"
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
10 #include "strhelp.h"
670
175370634226 Move getProcessOwner to util and use it to skip the current user in locate other hives
Andre Heinecke <andre.heinecke@intevation.de>
parents: 608
diff changeset
11 #include "util.h"
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
12
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
13 #include <windows.h>
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
14 #include <tlhelp32.h>
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
15 #include <psapi.h>
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
16 #include <unistd.h>
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
17
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
18 #include <QDebug>
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
19
606
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
20 struct EnumWindowsStruct {
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
21 EnumWindowsStruct() : windowId(0) {}
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
22 DWORD pid;
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
23 HWND windowId;
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
24 };
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
25
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
26 BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
27 {
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
28 if (GetWindowLong(hwnd, GWL_STYLE) & WS_VISIBLE) {
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
29
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
30 DWORD pidwin;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
31
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
32 GetWindowThreadProcessId(hwnd, &pidwin);
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
33 if (pidwin == ((EnumWindowsStruct *)lParam)->pid) {
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
34 ((EnumWindowsStruct *)lParam)->windowId = hwnd;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
35 return FALSE;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
36 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
37 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
38 return TRUE;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
39 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
40
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
41 static HANDLE getProcessHandle(int processID)
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
42 {
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
43 return OpenProcess(SYNCHRONIZE |
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
44 PROCESS_QUERY_INFORMATION |
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
45 PROCESS_VM_READ |
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
46 PROCESS_TERMINATE,
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
47 false, processID);
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
48 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
49
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
50 const QList<int> ProcessHelp::getProcessesIdForName(const QString &processName)
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
51 {
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
52 HANDLE h;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
53 PROCESSENTRY32 pe32;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
54 QList <int> pids;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
55
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
56 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
57 if (h == INVALID_HANDLE_VALUE) {
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
58 return pids;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
59 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
60
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
61 pe32.dwSize = sizeof(PROCESSENTRY32); // Necessary according to MSDN
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
62 if (!Process32First(h, &pe32)) {
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
63 return pids;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
64 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
65
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
66 pids.clear();
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
67
608
81a44b93229e Be more permissive with process names. Case insenstive and with suffix or not.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 606
diff changeset
68 const QString processNameLower = processName.toLower().replace(".exe", "");
81a44b93229e Be more permissive with process names. Case insenstive and with suffix or not.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 606
diff changeset
69 const QString processNameExe = processNameLower + ".exe";
81a44b93229e Be more permissive with process names. Case insenstive and with suffix or not.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 606
diff changeset
70
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
71 do {
608
81a44b93229e Be more permissive with process names. Case insenstive and with suffix or not.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 606
diff changeset
72 const QString exeFile = QString::fromWCharArray(pe32.szExeFile).toLower();
81a44b93229e Be more permissive with process names. Case insenstive and with suffix or not.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 606
diff changeset
73 if (exeFile == processNameLower || exeFile == processNameExe) {
670
175370634226 Move getProcessOwner to util and use it to skip the current user in locate other hives
Andre Heinecke <andre.heinecke@intevation.de>
parents: 608
diff changeset
74 PSID user_sid = get_process_owner(GetCurrentProcess());
606
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
75 if (user_sid) {
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
76 // Also check that we are the owner of that process
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
77 HANDLE hProcess = getProcessHandle(pe32.th32ProcessID);
606
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
78 if (!hProcess) {
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
79 continue;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
80 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
81
670
175370634226 Move getProcessOwner to util and use it to skip the current user in locate other hives
Andre Heinecke <andre.heinecke@intevation.de>
parents: 608
diff changeset
82 PSID sid = get_process_owner(hProcess);
175370634226 Move getProcessOwner to util and use it to skip the current user in locate other hives
Andre Heinecke <andre.heinecke@intevation.de>
parents: 608
diff changeset
83 PSID userSid = get_process_owner(GetCurrentProcess());
606
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
84 if (!sid || (userSid && !EqualSid(userSid, sid))) {
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
85 free(sid);
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
86 continue;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
87 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
88 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
89 pids.append((int)pe32.th32ProcessID);
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
90 qDebug() << "found PID: " << (int)pe32.th32ProcessID;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
91 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
92 } while (Process32Next(h, &pe32));
606
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
93 CloseHandle(h);
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
94 return pids;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
95 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
96
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
97 bool ProcessHelp::otherProcessesExist(const QString &processName)
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
98 {
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
99 const QList<int> pids = getProcessesIdForName(processName);
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
100 int myPid = getpid();
606
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
101 foreach(int pid, pids) {
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
102 if (myPid != pid) {
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
103 qDebug() << "Found another process with id: " << pid;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
104 return true;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
105 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
106 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
107 return false;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
108 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
109
606
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
110 void ProcessHelp::activateWindowForProcess(const QString &executableName)
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
111 {
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
112 const QList<int> pids = getProcessesIdForName(executableName);
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
113 int myPid = getpid();
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
114 int foundPid = 0;
606
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
115 foreach(int pid, pids) {
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
116 if (myPid != pid) {
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
117 qDebug() << "activateWindowForProcess(): PID to activate:" << pid;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
118 foundPid = pid;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
119 break;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
120 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
121 }
606
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
122 if (foundPid == 0) {
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
123 return;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
124 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
125 EnumWindowsStruct winStruct;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
126 winStruct.pid = foundPid;
606
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
127 EnumWindows(EnumWindowsProc, (LPARAM)&winStruct);
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
128 if (winStruct.windowId == 0) {
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
129 return;
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
130 }
606
91dd38a71783 Style: run astyle on processhelp
Andre Heinecke <andre.heinecke@intevation.de>
parents: 603
diff changeset
131 SetForegroundWindow(winStruct.windowId);
603
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
132 }
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
133 #endif // WIN32
cfef809b890d Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
134

http://wald.intevation.org/projects/trustbridge/