33 using namespace KPIMUtils;
47 #include <QtCore/QList>
48 #include <QtCore/QtDebug>
53 PSID copySid( PSID from )
59 int sidLength = GetLengthSid( from );
60 PSID to = (PSID) malloc( sidLength );
61 CopySid( sidLength, to, from );
66 static PSID getProcessOwner( HANDLE hProcess )
72 OpenProcessToken( hProcess, TOKEN_READ, &hToken );
75 PTOKEN_USER userStruct;
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 );
83 sid = copySid( userStruct->User.Sid );
84 CloseHandle( hToken );
94 static HANDLE getProcessHandle(
int processID )
96 return OpenProcess( SYNCHRONIZE |
97 PROCESS_QUERY_INFORMATION |
103 void KPIMUtils::getProcessesIdForName(
const QString &processName, QList<int> &pids )
108 h = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
109 if ( h == INVALID_HANDLE_VALUE ) {
113 pe32.dwSize =
sizeof( PROCESSENTRY32 );
114 if ( !Process32First( h, &pe32 ) ) {
121 if ( QString::fromWCharArray( pe32.szExeFile ) == processName ) {
122 PSID user_sid = getProcessOwner( GetCurrentProcess() );
125 HANDLE hProcess = getProcessHandle( pe32.th32ProcessID );
130 PSID sid = getProcessOwner( hProcess );
131 PSID userSid = getProcessOwner( GetCurrentProcess() );
132 if ( !sid || userSid && !EqualSid( userSid, sid ) ) {
137 pids.append( (
int)pe32.th32ProcessID );
138 kDebug() <<
"found PID: " << (int)pe32.th32ProcessID;
140 }
while ( Process32Next( h, &pe32 ) );
144 CloseToolhelp32Snapshot( h );
148 bool KPIMUtils::otherProcessesExist(
const QString &processName )
151 getProcessesIdForName( processName, pids );
152 int myPid = getpid();
153 foreach (
int pid, pids ) {
154 if ( myPid != pid ) {
162 bool KPIMUtils::killProcesses(
const QString &processName )
165 getProcessesIdForName( processName, pids );
166 if ( pids.empty() ) {
170 qWarning() <<
"Killing process \"" << processName <<
" (pid=" << pids[0] <<
")..";
171 int overallResult = 0;
172 foreach (
int pid, pids ) {
175 result = kill( pid, SIGTERM );
180 result = kill( pid, SIGKILL );
182 overallResult = result;
185 return overallResult == 0;
188 struct EnumWindowsStruct
190 EnumWindowsStruct() : windowId( 0 ) {}
195 BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
197 if ( GetWindowLong( hwnd, GWL_STYLE ) & WS_VISIBLE ) {
201 GetWindowThreadProcessId( hwnd, &pidwin );
202 if ( pidwin == ( (EnumWindowsStruct *)lParam )->pid ) {
203 ( (EnumWindowsStruct *)lParam )->windowId = hwnd;
210 void KPIMUtils::activateWindowForProcess(
const QString &executableName )
213 KPIMUtils::getProcessesIdForName( executableName, pids );
214 int myPid = getpid();
216 foreach (
int pid, pids ) {
217 if ( myPid != pid ) {
218 kDebug() <<
"activateWindowForProcess(): PID to activate:" << pid;
223 if ( foundPid == 0 ) {
226 EnumWindowsStruct winStruct;
227 winStruct.pid = foundPid;
228 EnumWindows( EnumWindowsProc, (LPARAM)&winStruct );
229 if ( winStruct.windowId == 0 ) {
232 SetForegroundWindow( winStruct.windowId );
This file is part of the kpimutils library.