Skip to content

Commit

Permalink
Transition to window hwnd finder instead of snapshot32
Browse files Browse the repository at this point in the history
  • Loading branch information
eschan145 committed Jan 18, 2025
1 parent b4752f7 commit bc43fdc
Showing 1 changed file with 30 additions and 39 deletions.
69 changes: 30 additions & 39 deletions src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,51 +289,42 @@ void sweep() {
int count = 0;
int total_count = 0;

HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
HWND hwnd = FindWindow(
nullptr,
DYK_CLASS_NAME
);

if (snapshot == INVALID_HANDLE_VALUE) {
error("Failed to produce a snapshot with CreateToolHelp32! Error "
"code: " + last_error() + "\n");
return;
}
if (!hwnd) {
std::vector<std::string> possible_names;

PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
possible_names.push_back("Do you understand?");
possible_names.push_back("We let your teacher know (I get it!)");
possible_names.push_back("We let your teacher know (I'm not sure!)");
possible_names.push_back(
"We let your teacher know (I don't get it, yet!)"
);

if (Process32First(snapshot, &pe32)) {
do {
total_count++;
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);
count++;
}

if (count >= 2)
break;
}
}
}
}
} while (Process32Next(snapshot, &pe32));
} else {
error("Failed to enumerate through processes! Error code: " +
last_error() + "\n");
for (const std::string& name : possible_names) {
hwnd = FindWindow(nullptr, name);
if (hwnd)
break;
}
}

CloseHandle(snapshot);
if (!hwnd) {
error("Failed to find DyKnow window! It may not be running.\n");
return;
}

DWORD identifier;
GetWindowThreadProcessId(hwnd, &identifier);

dieknow::taskkill(identifier);

std::cout << "Sweep completed. Total executables scanned: " << total_count
<< ", processes terminated: " << count << "\n";
// else {
// error("Failed to enumerate through processes! Error code: " +
// last_error() + "\n");
// }
}

DK_API void start_monitoring(const char* folder_path) {
Expand Down

0 comments on commit bc43fdc

Please sign in to comment.