leocad/qt/system.cpp

91 lines
2.2 KiB
C++
Raw Permalink Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
2020-12-20 21:06:14 +01:00
#ifdef Q_OS_WIN
2013-08-09 06:57:18 +02:00
2024-04-28 02:19:34 +02:00
#include <windows.h>
#include <TlHelp32.h>
2013-08-09 06:57:18 +02:00
char* strcasestr(const char *s, const char *find)
{
char c, sc;
if ((c = *find++) != 0)
{
c = tolower((unsigned char)c);
2021-11-15 03:34:24 +01:00
const int len = (int)strlen(find);
2013-08-09 06:57:18 +02:00
do
{
do
{
if ((sc = *s++) == 0)
return (nullptr);
2013-08-09 06:57:18 +02:00
} while ((char)tolower((unsigned char)sc) != c);
2017-02-07 18:35:11 +01:00
} while (qstrnicmp(s, find, len) != 0);
2013-08-09 06:57:18 +02:00
s--;
}
return ((char *)s);
}
2024-04-28 02:19:34 +02:00
int lcTerminateChildProcess(QWidget* Parent, const qint64 Pid, const qint64 Ppid)
{
DWORD pID = DWORD(Pid);
DWORD ppID = DWORD(Ppid);
HANDLE hSnapshot = INVALID_HANDLE_VALUE, hProcess = INVALID_HANDLE_VALUE;
PROCESSENTRY32 pe32;
if ((hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, ppID)) == INVALID_HANDLE_VALUE)
{
QMessageBox::warning(Parent, QObject::tr("Error"), QString("%1 failed: %1").arg("CreateToolhelp32Snapshot").arg(GetLastError()));
return -1;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32) == FALSE)
{
QMessageBox::warning(Parent, QObject::tr("Error"), QString("%1 failed: %2").arg("Process32First").arg(GetLastError()));
CloseHandle(hSnapshot);
return -2;
}
do
{
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QRegularExpression ExeExpression("^(?:cmd\\.exe|conhost\\.exe|blender\\.exe)$", QRegularExpression::CaseInsensitiveOption);
#else
QRegExp ExeExpression("^(?:cmd\\.exe|conhost\\.exe|blender\\.exe)$", Qt::CaseInsensitive);
#endif
if (QString::fromWCharArray(pe32.szExeFile).contains(ExeExpression))
{
if ((pe32.th32ProcessID == pID && pe32.th32ParentProcessID == ppID) || pe32.th32ParentProcessID == pID)
{
if ((hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID)) == INVALID_HANDLE_VALUE)
{
QMessageBox::warning(Parent, QObject::tr("Error"), QObject::tr("%1 failed: %2").arg("OpenProcess").arg(GetLastError()));
return -3;
}
else
{
TerminateProcess(hProcess, 9);
CloseHandle(hProcess);
}
}
}
}
while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return 0;
}
2013-08-09 06:57:18 +02:00
#else
char* strupr(char *string)
{
for (char *c = string; *c; c++)
*c = toupper(*c);
return string;
}
#endif