melhore o código c++

#ifndef ATTACH_H
#define ATTACH_H

#include <memory>
#include <windows.h>
#include <string>
#include <winternl.h>
#include <iostream>
#include "Secure.h"

class AlreadyDebugged {
public:
    void start(int argc, char* argv[]) {
        load();
        if (argc == 1) {
            initializeMainProcess();
        }
        else {
            const auto parent_process_id = atoi(argv[1]);
            startChildProcess(parent_process_id);
        }
    }

private:
    void load() {
        using NtQuerySystemInformationFunc = DWORD(WINAPI*)(DWORD, PVOID, ULONG, PULONG);
        using NtOpenThreadFunc = DWORD(WINAPI*)(HANDLE*, DWORD, OBJECT_ATTRIBUTES*, CLIENT_ID*);
        using NtQueryObjectFunc = DWORD(WINAPI*)(HANDLE, DWORD, PVOID, DWORD, DWORD*);

        static NtQuerySystemInformationFunc NtQuerySystemInformation = nullptr;
        static NtOpenThreadFunc NtOpenThread = nullptr;
        static NtQueryObjectFunc NtQueryObject = nullptr;

        const HMODULE ntdll_handle = GetModuleHandleA(SecureString("ntdll.dll").c_str());
        if (!ntdll_handle) return;

        NtQuerySystemInformation = reinterpret_cast<NtQuerySystemInformationFunc>(GetProcAddress(ntdll_handle, SecureString("NtQuerySystemInformation").c_str()));
        NtOpenThread = reinterpret_cast<NtOpenThreadFunc>(GetProcAddress(ntdll_handle, SecureString("NtOpenThread").c_str()));
        NtQueryObject = reinterpret_cast<NtQueryObjectFunc>(GetProcAddress(ntdll_handle, SecureString("NtQueryObject").c_str()));
    }

    void startChildProcess(const DWORD& parent_process_id) {
        auto process_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, parent_process_id);
        if (!process_handle || !DebugActiveProcess(parent_process_id)) {
            if (process_handle) {
                CloseHandle(process_handle);
            }
            return;
        }

        HANDLE debug_handle = *reinterpret_cast<HANDLE*>(reinterpret_cast<BYTE*>(__readgsqword(0x30)) + 0x16A8);
        if (!debug_handle) {
            CloseHandle(process_handle);
            return;
        }

        HANDLE cloned_debug_handle;
        if (!DuplicateHandle(GetCurrentProcess(), debug_handle, process_handle, &cloned_debug_handle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
            CloseHandle(process_handle);
            return;
        }

        DEBUG_EVENT debug_event;
        DWORD timeout_count = 0;
        while (timeout_count < 10) {
            if (!WaitForDebugEvent(&debug_event, 100)) {
                ++timeout_count;
                continue;
            }
            timeout_count = 0;
            ContinueDebugEvent(debug_event.dwProcessId, debug_event.dwThreadId, DBG_CONTINUE);
        }

        CloseHandle(cloned_debug_handle);
        CloseHandle(debug_handle);
        CloseHandle(process_handle);
        ExitThread(0);
    }

    void initializeMainProcess() {
        STARTUPINFOA startup_info = { sizeof(startup_info) };
        PROCESS_INFORMATION process_info = {};
        char current_path[MAX_PATH];
        GetModuleFileNameA(nullptr, current_path, sizeof(current_path));
        std::string command_line = "\"" + std::string(current_path) + "\" " + std::to_string(GetCurrentProcessId());

        if (CreateProcessA(nullptr, const_cast<LPSTR>(command_line.c_str()), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &startup_info, &process_info)) {
            CloseHandle(process_info.hProcess);
            CloseHandle(process_info.hThread);
            WaitForSingleObject(process_info.hProcess, INFINITE);
        }
    }
};

inline const auto c_already_debugged = std::make_unique<AlreadyDebugged>();

#endif // ATTACH_H