• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 API Reference
  • KDE Home
  • Contact Us
 

kpimutils

  • kpimutils
processes.cpp
Go to the documentation of this file.
1 
30 #include "processes.h"
31 using namespace KPIMUtils;
32 
33 #ifdef Q_WS_WIN
34 
35 #include <windows.h>
36 #include <tlhelp32.h>
37 #include <psapi.h>
38 #include <signal.h>
39 #include <unistd.h>
40 
41 #ifdef Q_OS_WINCE
42 #include <Tlhelp32.h>
43 #endif
44 
45 #include <QtCore/QList>
46 #include <QtCore/QtDebug>
47 
48 #include <KDebug>
49 
50 // Copy from kdelibs/kinit/kinit_win.cpp
51 PSID copySid(PSID from)
52 {
53  if (!from)
54  return 0;
55  int sidLength = GetLengthSid(from);
56  PSID to = (PSID) malloc(sidLength);
57  CopySid(sidLength, to, from);
58  return to;
59 }
60 
61 // Copy from kdelibs/kinit/kinit_win.cpp
62 static PSID getProcessOwner(HANDLE hProcess)
63 {
64 #ifndef _WIN32_WCE
65  HANDLE hToken = NULL;
66  PSID sid;
67 
68  OpenProcessToken(hProcess, TOKEN_READ, &hToken);
69  if(hToken)
70  {
71  DWORD size;
72  PTOKEN_USER userStruct;
73 
74  // check how much space is needed
75  GetTokenInformation(hToken, TokenUser, NULL, 0, &size);
76  if( ERROR_INSUFFICIENT_BUFFER == GetLastError() )
77  {
78  userStruct = reinterpret_cast<PTOKEN_USER>( new BYTE[size] );
79  GetTokenInformation(hToken, TokenUser, userStruct, size, &size);
80 
81  sid = copySid(userStruct->User.Sid);
82  CloseHandle(hToken);
83  delete [] userStruct;
84  return sid;
85  }
86  }
87 #endif
88  return 0;
89 }
90 
91 // Copy from kdelibs/kinit/kinit_win.cpp
92 static HANDLE getProcessHandle(int processID)
93 {
94  return OpenProcess( SYNCHRONIZE|PROCESS_QUERY_INFORMATION |
95  PROCESS_VM_READ | PROCESS_TERMINATE,
96  false, processID );
97 }
98 
99 void KPIMUtils::getProcessesIdForName( const QString &processName, QList<int> &pids )
100 {
101  HANDLE h;
102  PROCESSENTRY32 pe32;
103 
104  h = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
105  if ( h == INVALID_HANDLE_VALUE ) {
106  return;
107  }
108 
109  pe32.dwSize = sizeof(PROCESSENTRY32); // Necessary according to MSDN
110  if ( !Process32First( h, &pe32 ) ) {
111  return;
112  }
113 
114  pids.clear();
115 
116  do {
117  if ( QString::fromWCharArray( pe32.szExeFile ) == processName ) {
118  PSID user_sid = getProcessOwner( GetCurrentProcess() );
119  if ( user_sid ) {
120  // Also check that we are the Owner of that process
121  HANDLE hProcess = getProcessHandle( pe32.th32ProcessID );
122  if (!hProcess) {
123  continue;
124  }
125 
126  PSID sid = getProcessOwner( hProcess );
127  PSID userSid = getProcessOwner( GetCurrentProcess() );
128  if (!sid || userSid && !EqualSid( userSid, sid )) {
129  free ( sid );
130  continue;
131  }
132  }
133  pids.append( (int)pe32.th32ProcessID );
134  kDebug() << "found PID: " << (int)pe32.th32ProcessID;
135  }
136  } while( Process32Next( h, &pe32 ) );
137 #ifndef _WIN32_WCE
138  CloseHandle(h);
139 #else
140  CloseToolhelp32Snapshot(h);
141 #endif
142 }
143 
144 bool KPIMUtils::otherProcessesExist( const QString &processName )
145 {
146  QList<int> pids;
147  getProcessesIdForName( processName, pids );
148  int myPid = getpid();
149  foreach ( int pid, pids ) {
150  if ( myPid != pid ) {
151 // kDebug() << "Process ID is " << pid;
152  return true;
153  }
154  }
155  return false;
156 }
157 
158 bool KPIMUtils::killProcesses( const QString &processName )
159 {
160  QList<int> pids;
161  getProcessesIdForName( processName, pids );
162  if ( pids.empty() ) {
163  return true;
164  }
165 
166  qWarning() << "Killing process \"" << processName << " (pid=" << pids[0] << ")..";
167  int overallResult = 0;
168  foreach ( int pid, pids ) {
169  int result;
170 #ifndef _WIN32_WCE
171  result = kill( pid, SIGTERM );
172  if ( result == 0 ) {
173  continue;
174  }
175 #endif
176  result = kill( pid, SIGKILL );
177  if ( result != 0 ) {
178  overallResult = result;
179  }
180  }
181  return overallResult == 0;
182 }
183 
184 struct EnumWindowsStruct
185 {
186  EnumWindowsStruct() : windowId( 0 ) {}
187  int pid;
188  HWND windowId;
189 };
190 
191 BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
192 {
193  if ( GetWindowLong( hwnd, GWL_STYLE ) & WS_VISIBLE ) {
194 
195  DWORD pidwin;
196 
197  GetWindowThreadProcessId( hwnd, &pidwin );
198  if ( pidwin == ( (EnumWindowsStruct *)lParam )->pid ) {
199  ( (EnumWindowsStruct *)lParam )->windowId = hwnd;
200  return FALSE; //krazy:exclude=captruefalse
201  }
202  }
203  return TRUE; //krazy:exclude=captruefalse
204 }
205 
206 void KPIMUtils::activateWindowForProcess( const QString &executableName )
207 {
208  QList<int> pids;
209  KPIMUtils::getProcessesIdForName( executableName, pids );
210  int myPid = getpid();
211  int foundPid = 0;
212  foreach ( int pid, pids ) {
213  if ( myPid != pid ) {
214  kDebug() << "activateWindowForProcess(): PID to activate:" << pid;
215  foundPid = pid;
216  break;
217  }
218  }
219  if ( foundPid == 0 ) {
220  return;
221  }
222  EnumWindowsStruct winStruct;
223  winStruct.pid = foundPid;
224  EnumWindows( EnumWindowsProc, (LPARAM)&winStruct );
225  if ( winStruct.windowId == 0 ) {
226  return;
227  }
228  SetForegroundWindow( winStruct.windowId );
229 }
230 
231 #endif // Q_WS_WIN
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 4 2012 14:35:29 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kpimutils

Skip menu "kpimutils"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules

kdepimlibs-4.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal