@@ -1618,13 +1618,20 @@ void WPMUtils::closeProcessesThatUseDirectory(const QString &dir,
16181618 bool changed = false ;
16191619 for (int i = 0 ; i < ps.size (); i++) {
16201620 HANDLE p = ps.at (i);
1621- if (GetProcessId (p) != me && !pathEquals (explorer, getProcessFile (p))) {
1622- QList<HWND> ws = findProcessTopWindows (GetProcessId (p));
1623- if (ws.size () > 0 ) {
1624- closeProcessWindows (p, ws);
1625- changed = true ;
1626- }
1627- }
1621+
1622+ if (GetProcessId (p) == me)
1623+ continue ;
1624+
1625+ QString processFile = getProcessFile (p);
1626+ if (processFile.isEmpty () || pathEquals (explorer, processFile))
1627+ continue ;
1628+
1629+ QList<HWND> ws = findProcessTopWindows (GetProcessId (p));
1630+ if (ws.size () == 0 )
1631+ continue ;
1632+
1633+ closeProcessWindows (p, ws);
1634+ changed = true ;
16281635 }
16291636
16301637 if (changed) {
@@ -1641,17 +1648,26 @@ void WPMUtils::closeProcessesThatUseDirectory(const QString &dir,
16411648 HANDLE hProc = ps.at (i);
16421649 DWORD processId = GetProcessId (hProc);
16431650
1644- if (processId != 0 && processId != me &&
1645- WPMUtils::isProcessRunning (hProc) &&
1646- !pathEquals (explorer, getProcessFile (hProc))) {
1647- // 0 = GR_GDIOBJECTS, 1 = GR_USEROBJECTS
1648- if (GetGuiResources (hProc, 0 ) == 0 &&
1649- GetGuiResources (hProc, 1 ) == 0 ) {
1650- qCInfo (npackd).noquote () << QObject::tr (" Sending Ctrl+C to \" %1\" " ).
1651- arg (getProcessFile (hProc));
1652- if (GenerateConsoleCtrlEvent (CTRL_C_EVENT, processId)) {
1653- changed = true ;
1654- }
1651+ if (processId == 0 )
1652+ continue ;
1653+
1654+ if (processId == me)
1655+ continue ;
1656+
1657+ if (!WPMUtils::isProcessRunning (hProc))
1658+ continue ;
1659+
1660+ QString processFile = getProcessFile (hProc);
1661+ if (processFile.isEmpty () || pathEquals (explorer, processFile))
1662+ continue ;
1663+
1664+ // 0 = GR_GDIOBJECTS, 1 = GR_USEROBJECTS
1665+ if (GetGuiResources (hProc, 0 ) == 0 &&
1666+ GetGuiResources (hProc, 1 ) == 0 ) {
1667+ qCInfo (npackd).noquote () << QObject::tr (" Sending Ctrl+C to \" %1\" " ).
1668+ arg (getProcessFile (hProc));
1669+ if (GenerateConsoleCtrlEvent (CTRL_C_EVENT, processId)) {
1670+ changed = true ;
16551671 }
16561672 }
16571673 }
@@ -1668,19 +1684,27 @@ void WPMUtils::closeProcessesThatUseDirectory(const QString &dir,
16681684 HANDLE hProc = ps.at (i);
16691685 DWORD processId = GetProcessId (hProc);
16701686
1671- if (processId != 0 && processId != me &&
1672- WPMUtils::isProcessRunning (hProc) &&
1673- !pathEquals (explorer, getProcessFile (hProc))) {
1687+ if (processId == 0 )
1688+ continue ;
16741689
1675- qCInfo (npackd). noquote () << QObject::tr ( " Killing the process \" %1 \" " ).
1676- arg ( getProcessFile (hProc)) ;
1690+ if (processId == me)
1691+ continue ;
16771692
1678- // TerminateProcess is asynchronous
1679- if (TerminateProcess (hProc, 1000 ))
1680- WaitForSingleObject (hProc, 30000 );
1693+ if (!WPMUtils::isProcessRunning (hProc))
1694+ continue ;
16811695
1682- changed = true ;
1683- }
1696+ QString processFile = getProcessFile (hProc);
1697+ if (processFile.isEmpty () || pathEquals (explorer, processFile))
1698+ continue ;
1699+
1700+ qCInfo (npackd).noquote () << QObject::tr (" Killing the process \" %1\" " ).
1701+ arg (getProcessFile (hProc));
1702+
1703+ // TerminateProcess is asynchronous
1704+ if (TerminateProcess (hProc, 1000 ))
1705+ WaitForSingleObject (hProc, 30000 );
1706+
1707+ changed = true ;
16841708 }
16851709
16861710 if (changed) {
@@ -1813,7 +1837,9 @@ void WPMUtils::closeProcessWindows(HANDLE process,
18131837 if ((GetWindowLong (w, GWL_STYLE) & WS_DISABLED) == 0 ) {
18141838 // qCDebug(npackd) << "WM_CLOSE to " <<
18151839 // GetProcessId(process) << getClassName(w);
1816- qCInfo (npackd).noquote () << QObject::tr (" Sending a WM_CLOSE event to \" %1\" " ).
1840+ qCInfo (npackd).noquote () << QObject::tr (" Sending a WM_CLOSE event to the window of "
1841+ " the class \" %1\" from the process \" %2\" " ).
1842+ arg (getClassName (w)).
18171843 arg (getProcessFile (process));
18181844 PostMessage (w, WM_CLOSE, 0 , 0 );
18191845 }
@@ -1855,9 +1881,7 @@ QString WPMUtils::getProcessFile(HANDLE hProcess)
18551881 res.resize (MAX_PATH + 1 );
18561882 DWORD r = GetModuleFileNameEx (hProcess, nullptr , (LPWSTR) res.data (),
18571883 res.length ());
1858- if (r != 0 ) {
1859- res.resize (r);
1860- }
1884+ res.resize (r);
18611885
18621886 return res;
18631887}
0 commit comments