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

kpimutils

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

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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