comparison ui/processhelp_win.cpp @ 606:91dd38a71783

Style: run astyle on processhelp
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 18 Jun 2014 11:05:10 +0200
parents cfef809b890d
children 81a44b93229e
comparison
equal deleted inserted replaced
605:1642a79cc02d 606:91dd38a71783
14 #include <psapi.h> 14 #include <psapi.h>
15 #include <unistd.h> 15 #include <unistd.h>
16 16
17 #include <QDebug> 17 #include <QDebug>
18 18
19 struct EnumWindowsStruct 19 struct EnumWindowsStruct {
20 { 20 EnumWindowsStruct() : windowId(0) {}
21 EnumWindowsStruct() : windowId( 0 ) {} 21 DWORD pid;
22 DWORD pid; 22 HWND windowId;
23 HWND windowId;
24 }; 23 };
25 24
26 PSID copySid(PSID from) 25 PSID copySid(PSID from)
27 { 26 {
28 if ( !from ) { 27 if (!from) {
29 return 0; 28 return 0;
30 } 29 }
31 30
32 int sidLength = GetLengthSid( from ); 31 int sidLength = GetLengthSid(from);
33 PSID to = (PSID) xmalloc( sidLength ); 32 PSID to = (PSID) xmalloc(sidLength);
34 CopySid(sidLength, to, from); 33 CopySid(sidLength, to, from);
35 return to; 34 return to;
36 } 35 }
37 36
38 static PSID getProcessOwner(HANDLE hProcess) 37 static PSID getProcessOwner(HANDLE hProcess)
39 { 38 {
40 HANDLE hToken = NULL; 39 HANDLE hToken = NULL;
41 PSID sid; 40 PSID sid;
42 41
43 OpenProcessToken(hProcess, TOKEN_READ, &hToken); 42 OpenProcessToken(hProcess, TOKEN_READ, &hToken);
44 if ( hToken ) { 43 if (hToken) {
45 DWORD size; 44 DWORD size;
46 PTOKEN_USER userStruct; 45 PTOKEN_USER userStruct;
47 46
48 // check how much space is needed 47 // check how much space is needed
49 GetTokenInformation( hToken, TokenUser, NULL, 0, &size ); 48 GetTokenInformation(hToken, TokenUser, NULL, 0, &size);
50 if ( ERROR_INSUFFICIENT_BUFFER == GetLastError() ) { 49 if (ERROR_INSUFFICIENT_BUFFER == GetLastError()) {
51 userStruct = reinterpret_cast<PTOKEN_USER>( new BYTE[size] ); 50 userStruct = reinterpret_cast<PTOKEN_USER>(new BYTE[size]);
52 GetTokenInformation( hToken, TokenUser, userStruct, size, &size ); 51 GetTokenInformation(hToken, TokenUser, userStruct, size, &size);
53 52
54 sid = copySid( userStruct->User.Sid ); 53 sid = copySid(userStruct->User.Sid);
55 CloseHandle( hToken ); 54 CloseHandle(hToken);
56 delete [] userStruct; 55 delete [] userStruct;
57 return sid; 56 return sid;
58 } 57 }
59 } 58 }
60 return 0; 59 return 0;
102 } 101 }
103 102
104 pids.clear(); 103 pids.clear();
105 104
106 do { 105 do {
107 if ( QString::fromWCharArray(pe32.szExeFile) == processName ) { 106 if (QString::fromWCharArray(pe32.szExeFile) == processName) {
108 PSID user_sid = getProcessOwner(GetCurrentProcess()); 107 PSID user_sid = getProcessOwner(GetCurrentProcess());
109 if ( user_sid ) { 108 if (user_sid) {
110 // Also check that we are the owner of that process 109 // Also check that we are the owner of that process
111 HANDLE hProcess = getProcessHandle(pe32.th32ProcessID); 110 HANDLE hProcess = getProcessHandle(pe32.th32ProcessID);
112 if ( !hProcess ) { 111 if (!hProcess) {
113 continue; 112 continue;
114 } 113 }
115 114
116 PSID sid = getProcessOwner(hProcess); 115 PSID sid = getProcessOwner(hProcess);
117 PSID userSid = getProcessOwner(GetCurrentProcess()); 116 PSID userSid = getProcessOwner(GetCurrentProcess());
118 if ( !sid || (userSid && !EqualSid(userSid, sid))) { 117 if (!sid || (userSid && !EqualSid(userSid, sid))) {
119 free (sid); 118 free(sid);
120 continue; 119 continue;
121 } 120 }
122 } 121 }
123 pids.append((int)pe32.th32ProcessID); 122 pids.append((int)pe32.th32ProcessID);
124 qDebug() << "found PID: " << (int)pe32.th32ProcessID; 123 qDebug() << "found PID: " << (int)pe32.th32ProcessID;
125 } 124 }
126 } while (Process32Next(h, &pe32)); 125 } while (Process32Next(h, &pe32));
127 CloseHandle( h ); 126 CloseHandle(h);
128 return pids; 127 return pids;
129 } 128 }
130 129
131 bool ProcessHelp::otherProcessesExist(const QString &processName) 130 bool ProcessHelp::otherProcessesExist(const QString &processName)
132 { 131 {
133 const QList<int> pids = getProcessesIdForName(processName); 132 const QList<int> pids = getProcessesIdForName(processName);
134 int myPid = getpid(); 133 int myPid = getpid();
135 foreach (int pid, pids) { 134 foreach(int pid, pids) {
136 if (myPid != pid) { 135 if (myPid != pid) {
137 qDebug() << "Found another process with id: " << pid; 136 qDebug() << "Found another process with id: " << pid;
138 return true; 137 return true;
139 } 138 }
140 } 139 }
141 return false; 140 return false;
142 } 141 }
143 142
144 void ProcessHelp::activateWindowForProcess(const QString &executableName) { 143 void ProcessHelp::activateWindowForProcess(const QString &executableName)
144 {
145 const QList<int> pids = getProcessesIdForName(executableName); 145 const QList<int> pids = getProcessesIdForName(executableName);
146 int myPid = getpid(); 146 int myPid = getpid();
147 int foundPid = 0; 147 int foundPid = 0;
148 foreach (int pid, pids) { 148 foreach(int pid, pids) {
149 if (myPid != pid) { 149 if (myPid != pid) {
150 qDebug() << "activateWindowForProcess(): PID to activate:" << pid; 150 qDebug() << "activateWindowForProcess(): PID to activate:" << pid;
151 foundPid = pid; 151 foundPid = pid;
152 break; 152 break;
153 } 153 }
154 } 154 }
155 if ( foundPid == 0 ) { 155 if (foundPid == 0) {
156 return; 156 return;
157 } 157 }
158 EnumWindowsStruct winStruct; 158 EnumWindowsStruct winStruct;
159 winStruct.pid = foundPid; 159 winStruct.pid = foundPid;
160 EnumWindows( EnumWindowsProc, (LPARAM)&winStruct ); 160 EnumWindows(EnumWindowsProc, (LPARAM)&winStruct);
161 if ( winStruct.windowId == 0 ) { 161 if (winStruct.windowId == 0) {
162 return; 162 return;
163 } 163 }
164 SetForegroundWindow( winStruct.windowId ); 164 SetForegroundWindow(winStruct.windowId);
165 } 165 }
166 #endif // WIN32 166 #endif // WIN32
167 167

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