31 using namespace KPIMUtils;
45 #include <QtCore/QList>
46 #include <QtCore/QtDebug>
51 PSID copySid(PSID from)
55 int sidLength = GetLengthSid(from);
56 PSID to = (PSID) malloc(sidLength);
57 CopySid(sidLength, to, from);
62 static PSID getProcessOwner(HANDLE hProcess)
68 OpenProcessToken(hProcess, TOKEN_READ, &hToken);
72 PTOKEN_USER userStruct;
75 GetTokenInformation(hToken, TokenUser, NULL, 0, &size);
76 if( ERROR_INSUFFICIENT_BUFFER == GetLastError() )
78 userStruct =
reinterpret_cast<PTOKEN_USER
>(
new BYTE[size] );
79 GetTokenInformation(hToken, TokenUser, userStruct, size, &size);
81 sid = copySid(userStruct->User.Sid);
92 static HANDLE getProcessHandle(
int processID)
94 return OpenProcess( SYNCHRONIZE|PROCESS_QUERY_INFORMATION |
95 PROCESS_VM_READ | PROCESS_TERMINATE,
99 void KPIMUtils::getProcessesIdForName(
const QString &processName, QList<int> &pids )
104 h = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
105 if ( h == INVALID_HANDLE_VALUE ) {
109 pe32.dwSize =
sizeof(PROCESSENTRY32);
110 if ( !Process32First( h, &pe32 ) ) {
117 if ( QString::fromWCharArray( pe32.szExeFile ) == processName ) {
118 PSID user_sid = getProcessOwner( GetCurrentProcess() );
121 HANDLE hProcess = getProcessHandle( pe32.th32ProcessID );
126 PSID sid = getProcessOwner( hProcess );
127 PSID userSid = getProcessOwner( GetCurrentProcess() );
128 if (!sid || userSid && !EqualSid( userSid, sid )) {
133 pids.append( (
int)pe32.th32ProcessID );
134 kDebug() <<
"found PID: " << (int)pe32.th32ProcessID;
136 }
while( Process32Next( h, &pe32 ) );
140 CloseToolhelp32Snapshot(h);
144 bool KPIMUtils::otherProcessesExist(
const QString &processName )
147 getProcessesIdForName( processName, pids );
148 int myPid = getpid();
149 foreach (
int pid, pids ) {
150 if ( myPid != pid ) {
158 bool KPIMUtils::killProcesses(
const QString &processName )
161 getProcessesIdForName( processName, pids );
162 if ( pids.empty() ) {
166 qWarning() <<
"Killing process \"" << processName <<
" (pid=" << pids[0] <<
")..";
167 int overallResult = 0;
168 foreach (
int pid, pids ) {
171 result = kill( pid, SIGTERM );
176 result = kill( pid, SIGKILL );
178 overallResult = result;
181 return overallResult == 0;
184 struct EnumWindowsStruct
186 EnumWindowsStruct() : windowId( 0 ) {}
191 BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
193 if ( GetWindowLong( hwnd, GWL_STYLE ) & WS_VISIBLE ) {
197 GetWindowThreadProcessId( hwnd, &pidwin );
198 if ( pidwin == ( (EnumWindowsStruct *)lParam )->pid ) {
199 ( (EnumWindowsStruct *)lParam )->windowId = hwnd;
206 void KPIMUtils::activateWindowForProcess(
const QString &executableName )
209 KPIMUtils::getProcessesIdForName( executableName, pids );
210 int myPid = getpid();
212 foreach (
int pid, pids ) {
213 if ( myPid != pid ) {
214 kDebug() <<
"activateWindowForProcess(): PID to activate:" << pid;
219 if ( foundPid == 0 ) {
222 EnumWindowsStruct winStruct;
223 winStruct.pid = foundPid;
224 EnumWindows( EnumWindowsProc, (LPARAM)&winStruct );
225 if ( winStruct.windowId == 0 ) {
228 SetForegroundWindow( winStruct.windowId );