Mercurial > trustbridge
annotate ui/processhelp_win.cpp @ 605:1642a79cc02d
Every class that inherits QObject needs the Q_Object macro.
Fixes moc warnings
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Wed, 18 Jun 2014 10:44:39 +0200 |
parents | cfef809b890d |
children | 91dd38a71783 |
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" |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
11 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
12 #include <windows.h> |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
13 #include <tlhelp32.h> |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
14 #include <psapi.h> |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
15 #include <unistd.h> |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
16 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
17 #include <QDebug> |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
18 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
19 struct EnumWindowsStruct |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
20 { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
21 EnumWindowsStruct() : windowId( 0 ) {} |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
22 DWORD pid; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
23 HWND windowId; |
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 PSID copySid(PSID from) |
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 ( !from ) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
29 return 0; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
30 } |
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 int sidLength = GetLengthSid( from ); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
33 PSID to = (PSID) xmalloc( sidLength ); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
34 CopySid(sidLength, to, from); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
35 return to; |
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 static PSID getProcessOwner(HANDLE hProcess) |
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 HANDLE hToken = NULL; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
41 PSID sid; |
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 OpenProcessToken(hProcess, TOKEN_READ, &hToken); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
44 if ( hToken ) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
45 DWORD size; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
46 PTOKEN_USER userStruct; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
47 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
48 // check how much space is needed |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
49 GetTokenInformation( hToken, TokenUser, NULL, 0, &size ); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
50 if ( ERROR_INSUFFICIENT_BUFFER == GetLastError() ) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
51 userStruct = reinterpret_cast<PTOKEN_USER>( new BYTE[size] ); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
52 GetTokenInformation( hToken, TokenUser, userStruct, size, &size ); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
53 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
54 sid = copySid( userStruct->User.Sid ); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
55 CloseHandle( hToken ); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
56 delete [] userStruct; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
57 return sid; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
58 } |
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 return 0; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
61 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
62 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
63 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
|
64 { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
65 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
|
66 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
67 DWORD pidwin; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
68 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
69 GetWindowThreadProcessId(hwnd, &pidwin); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
70 if (pidwin == ((EnumWindowsStruct *)lParam)->pid) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
71 ((EnumWindowsStruct *)lParam)->windowId = hwnd; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
72 return FALSE; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
73 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
74 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
75 return TRUE; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
76 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
77 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
78 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
79 static HANDLE getProcessHandle(int processID) |
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 return OpenProcess(SYNCHRONIZE | |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
82 PROCESS_QUERY_INFORMATION | |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
83 PROCESS_VM_READ | |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
84 PROCESS_TERMINATE, |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
85 false, processID); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
86 } |
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 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
|
89 { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
90 HANDLE h; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
91 PROCESSENTRY32 pe32; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
92 QList <int> pids; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
93 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
94 h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
95 if (h == INVALID_HANDLE_VALUE) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
96 return pids; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
97 } |
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 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
|
100 if (!Process32First(h, &pe32)) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
101 return pids; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
102 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
103 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
104 pids.clear(); |
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 do { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
107 if ( QString::fromWCharArray(pe32.szExeFile) == processName ) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
108 PSID user_sid = getProcessOwner(GetCurrentProcess()); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
109 if ( user_sid ) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
110 // 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
|
111 HANDLE hProcess = getProcessHandle(pe32.th32ProcessID); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
112 if ( !hProcess ) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
113 continue; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
114 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
115 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
116 PSID sid = getProcessOwner(hProcess); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
117 PSID userSid = getProcessOwner(GetCurrentProcess()); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
118 if ( !sid || (userSid && !EqualSid(userSid, sid))) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
119 free (sid); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
120 continue; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
121 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
122 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
123 pids.append((int)pe32.th32ProcessID); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
124 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
|
125 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
126 } while (Process32Next(h, &pe32)); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
127 CloseHandle( h ); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
128 return pids; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
129 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
130 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
131 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
|
132 { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
133 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
|
134 int myPid = getpid(); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
135 foreach (int pid, pids) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
136 if (myPid != pid) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
137 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
|
138 return true; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
139 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
140 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
141 return false; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
142 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
143 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
144 void ProcessHelp::activateWindowForProcess(const QString &executableName) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
145 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
|
146 int myPid = getpid(); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
147 int foundPid = 0; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
148 foreach (int pid, pids) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
149 if (myPid != pid) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
150 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
|
151 foundPid = pid; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
152 break; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
153 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
154 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
155 if ( foundPid == 0 ) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
156 return; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
157 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
158 EnumWindowsStruct winStruct; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
159 winStruct.pid = foundPid; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
160 EnumWindows( EnumWindowsProc, (LPARAM)&winStruct ); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
161 if ( winStruct.windowId == 0 ) { |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
162 return; |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
163 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
164 SetForegroundWindow( winStruct.windowId ); |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
165 } |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
166 #endif // WIN32 |
cfef809b890d
Add Process Management functions and ther implementation for windows.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
167 |