GlosSIUtil: Add KIllProcess

pull/192/head
Peter Repukat 2 years ago
parent d22f44d37d
commit 51c2268dc0

@ -14,6 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
#pragma once #pragma once
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <tlhelp32.h>
namespace glossi_util { namespace glossi_util {
@ -33,5 +37,40 @@ inline DWORD PidByName(const std::wstring& name)
return 0; return 0;
} }
inline std::wstring GetProcName(DWORD pid)
{
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(processInfo);
const HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (processesSnapshot == INVALID_HANDLE_VALUE) {
spdlog::trace("util::GetProcName: can't get a process snapshot");
return L"";
}
for (BOOL bok = Process32First(processesSnapshot, &processInfo);
bok;
bok = Process32Next(processesSnapshot, &processInfo)) {
if (pid == processInfo.th32ProcessID) {
CloseHandle(processesSnapshot);
return processInfo.szExeFile;
}
}
CloseHandle(processesSnapshot);
return L"";
}
inline bool KillProcess(DWORD pid)
{
auto res = true;
if (const auto proc = OpenProcess(PROCESS_TERMINATE, FALSE, pid)) {
spdlog::debug("Terminating process: {}", pid);
res = TerminateProcess(proc, 0);
if (!res) {
spdlog::error("Failed to terminate process: {}", pid);
}
CloseHandle(proc);
}
return res;
}
} // namespace glossi_util } // namespace glossi_util

@ -31,6 +31,7 @@ limitations under the License.
#include "../version.hpp" #include "../version.hpp"
#include "../GlosSITarget/Settings.h" #include "../GlosSITarget/Settings.h"
#include "../GlosSITarget/HidHide.h" #include "../GlosSITarget/HidHide.h"
#include "../GlosSITarget/util.h"
bool IsProcessRunning(DWORD pid) bool IsProcessRunning(DWORD pid)
{ {
@ -142,16 +143,7 @@ DWORD WINAPI watchdog(HMODULE hModule)
} }
if (IsProcessRunning(pid)) if (IsProcessRunning(pid))
{ {
if (const auto proc = OpenProcess(PROCESS_TERMINATE, FALSE, pid)) glossi_util::KillProcess(pid);
{
spdlog::debug("Terminating process: {}", pid);
const auto terminate_res = TerminateProcess(proc, 0);
if (!terminate_res)
{
spdlog::error("Failed to terminate process: {}", pid);
}
CloseHandle(proc);
}
} }
else else
{ {

Loading…
Cancel
Save