leocad/qt/system.cpp
2024-04-27 17:19:34 -07:00

90 lines
2.2 KiB
C++

#include "lc_global.h"
#ifdef Q_OS_WIN
#include <windows.h>
#include <TlHelp32.h>
char* strcasestr(const char *s, const char *find)
{
char c, sc;
if ((c = *find++) != 0)
{
c = tolower((unsigned char)c);
const int len = (int)strlen(find);
do
{
do
{
if ((sc = *s++) == 0)
return (nullptr);
} while ((char)tolower((unsigned char)sc) != c);
} while (qstrnicmp(s, find, len) != 0);
s--;
}
return ((char *)s);
}
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;
}
#else
char* strupr(char *string)
{
for (char *c = string; *c; c++)
*c = toupper(*c);
return string;
}
#endif