33 using namespace KPIMUtils;
43 #include <QtCore/QList>
44 #include <QtCore/QtDebug>
49 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 )
67 OpenProcessToken( hProcess, TOKEN_READ, &hToken );
70 PTOKEN_USER userStruct;
73 GetTokenInformation( hToken, TokenUser, NULL, 0, &size );
74 if ( ERROR_INSUFFICIENT_BUFFER == GetLastError() ) {
75 userStruct =
reinterpret_cast<PTOKEN_USER
>(
new BYTE[size] );
76 GetTokenInformation( hToken, TokenUser, userStruct, size, &size );
78 sid = copySid( userStruct->User.Sid );
79 CloseHandle( hToken );
88 static HANDLE getProcessHandle(
int processID )
90 return OpenProcess( SYNCHRONIZE |
91 PROCESS_QUERY_INFORMATION |
97 void KPIMUtils::getProcessesIdForName(
const QString &processName,
QList<int> &pids )
102 h = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
103 if ( h == INVALID_HANDLE_VALUE ) {
107 pe32.dwSize =
sizeof( PROCESSENTRY32 );
108 if ( !Process32First( h, &pe32 ) ) {
116 PSID user_sid = getProcessOwner( GetCurrentProcess() );
119 HANDLE hProcess = getProcessHandle( pe32.th32ProcessID );
124 PSID sid = getProcessOwner( hProcess );
125 PSID userSid = getProcessOwner( GetCurrentProcess() );
126 if ( !sid || userSid && !EqualSid( userSid, sid ) ) {
131 pids.
append( (
int)pe32.th32ProcessID );
132 kDebug() <<
"found PID: " << (int)pe32.th32ProcessID;
134 }
while ( Process32Next( h, &pe32 ) );
138 bool KPIMUtils::otherProcessesExist(
const QString &processName )
141 getProcessesIdForName( processName, pids );
142 int myPid = getpid();
143 foreach (
int pid, pids ) {
144 if ( myPid != pid ) {
152 bool KPIMUtils::killProcesses(
const QString &processName )
155 getProcessesIdForName( processName, pids );
156 if ( pids.
empty() ) {
160 qWarning() <<
"Killing process \"" << processName <<
" (pid=" << pids[0] <<
")..";
161 int overallResult = 0;
162 foreach (
int pid, pids ) {
164 result = kill( pid, SIGTERM );
168 result = kill( pid, SIGKILL );
170 overallResult = result;
173 return overallResult == 0;
176 struct EnumWindowsStruct
178 EnumWindowsStruct() : windowId( 0 ) {}
183 BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
185 if ( GetWindowLong( hwnd, GWL_STYLE ) & WS_VISIBLE ) {
189 GetWindowThreadProcessId( hwnd, &pidwin );
190 if ( pidwin == ( (EnumWindowsStruct *)lParam )->pid ) {
191 ( (EnumWindowsStruct *)lParam )->windowId = hwnd;
198 void KPIMUtils::activateWindowForProcess(
const QString &executableName )
201 KPIMUtils::getProcessesIdForName( executableName, pids );
202 int myPid = getpid();
204 foreach (
int pid, pids ) {
205 if ( myPid != pid ) {
206 kDebug() <<
"activateWindowForProcess(): PID to activate:" << pid;
211 if ( foundPid == 0 ) {
214 EnumWindowsStruct winStruct;
215 winStruct.pid = foundPid;
216 EnumWindows( EnumWindowsProc, (LPARAM)&winStruct );
217 if ( winStruct.windowId == 0 ) {
220 SetForegroundWindow( winStruct.windowId );
QString fromWCharArray(const wchar_t *string, int size)
This file is part of the kpimutils library.
void append(const T &value)