Skip to content

Commit

Permalink
Create functionality for streamlined taskkill
Browse files Browse the repository at this point in the history
  • Loading branch information
eschan145 committed Jan 17, 2025
1 parent 729d9bb commit 5f6c5ff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
57 changes: 44 additions & 13 deletions src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,54 @@ DK_API const char* get_folder_path() {
return FOLDER_PATH;
}

bool taskkill(DWORD identifier) {
HANDLE process = OpenProcess(PROCESS_TERMINATE, FALSE, identifier);

if (process) {
TerminateProcess(process, -1);
CloseHandle(process);
return true;
}
return false;
}

void CALLBACK sweep(
HWND hwnd,
UINT uMsg,
UINT_PTR idEvent,
DWORD dwTime) {
for (const auto& entry :
std::filesystem::directory_iterator(FOLDER_PATH)) {
if (entry.is_directory()) {
for (const auto& sub_entry :
std::filesystem::directory_iterator(entry.path())) {
if ((sub_entry.is_regular_file()) &&
(sub_entry.path().extension() == ".exe")) {
bool result = dieknow::close_application_by_exe(
sub_entry.path().filename().string().c_str()
);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (snapshot == INVALID_HANDLE_VALUE) {
error("Failed to produce a snapshot with CreateToolHelp32! Error " +
"code: " + GetLastError() + "\n");
return;
}

PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);

if (Process32First(snapshot, &pe32)) {
do {
for (const auto& entry :
std::filesystem::directory_iterator(FOLDER_PATH)) {
if (entry.is_directory()) {
for (const auto& sub_entry :
std::filesystem::directory_iterator(entry.path())) {
if ((sub_entry.is_regular_file()) &&
(sub_entry.path().extension() == ".exe")) {
std::string name = sub_entry.path().filename().string();
if (_stricmp(pe32.szExeFile, name.c_str()) == 0) {
dieknow::taskkill(pe32.th32ProcessID);
}
}
}
}
}
break;
}
} while (Process32Next(snapshot, &pe32);
} else {
error("Failed to enumerate processes! Error code: " +
GetLastError() + "\n");
}
}

Expand Down Expand Up @@ -376,7 +405,7 @@ DK_API void start_monitoring(const char* folder_path) {
// old_interval = interval;
// }

SetTimer(NULL, 12, interval * 1000, sweep);
SetTimer(NULL, SWEEP_TIMER_ID, interval * 1000, sweep);

// Reduces CPU usage by prioritizing other applications.
// Other options:
Expand All @@ -402,6 +431,8 @@ DK_API void stop_monitoring() {
Stop monitoring executables.
*/

KillTimer(SWEEP_TIMER_ID);

// Although just a variable is set to false, because the DieKnow process is
// in a separate thread it will finish immediately.

Expand Down
3 changes: 3 additions & 0 deletions src/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Compile with g++ -shared -o api.dll api.cpp -Ofast -fPIC -shared
#define DK_API __declspec(dllexport)

#define TIMEOUT 5000
#define SWEEP_TIMER_ID 2048

#include <windows.h>
#include <wininet.h>
Expand Down Expand Up @@ -71,6 +72,8 @@ enum class InternetFlags {

namespace dieknow {

bool taskkill(DWORD identifier);

void CALLBACK sweep(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);

extern "C" {
Expand Down

0 comments on commit 5f6c5ff

Please sign in to comment.