В общем если это дочерний процесс, то все неплохо работает Демо Код (Text): #include <windows.h> #include <iostream> #include <atomic> #include <thread> void WaitForDebugEvents(HANDLE hProcess) { DEBUG_EVENT debugEvent; while (true) { // Wait for a debug event if (WaitForDebugEvent(&debugEvent, INFINITE)) { // std::cout << "Catch debug Event. ThreadID: " << debugEvent.dwThreadId << std::endl; // Check event type if (debugEvent.dwDebugEventCode == EXIT_THREAD_DEBUG_EVENT) { std::cout << "Thread terminated with exit code: " << debugEvent.u.ExitThread.dwExitCode << std::endl; } else if (debugEvent.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) { std::cout << "Process terminated with exit code: " << debugEvent.u.ExitProcess.dwExitCode << std::endl; break; // Exit loop } // Process other events as needed // Continue the event to the next handler ContinueDebugEvent(debugEvent.dwProcessId, debugEvent.dwThreadId, DBG_CONTINUE); } else { std::cout << "Fail WaitForDebugEvent" << std::endl; } } } bool EnableDebugPrivilege() { bool bResult = false; HANDLE hToken = NULL; DWORD ec = 0; do { if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) break; TOKEN_PRIVILEGES tp; tp.PrivilegeCount = 1; if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid)) break; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL)) break; bResult = true; } while (0); if (hToken) CloseHandle(hToken); return bResult; } int main() { STARTUPINFO si = { sizeof(si) }; PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi)); // Create a child process if (!CreateProcess( L"c:\\Windows\\notepad.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { std::cerr << "Error creating process: " << GetLastError() << std::endl; return 1; } if (EnableDebugPrivilege()) { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pi.dwProcessId); if (!hProcess) { std::cerr << "Failed to OpenProcess for debug." << GetLastError() << std::endl; } else { if (DebugActiveProcess(pi.dwProcessId)) { std::cout << "Wait debug event .... " << std::endl; WaitForDebugEvents(hProcess); } else { std::cout << "Fail to debug process." << GetLastError() << std::endl; } } CloseHandle(hProcess); } else { std::cerr << "EnableDebugPrivilege false." << std::endl; } std::cout << "Main thread is exiting." << std::endl; return 0; }