internal static void SkipInitialDebugBreak(uint dwProcessId) { if (!NativeDebuggingMethods.DebugActiveProcess(dwProcessId)) { throw new LastWin32Exception(); } try { bool done = false; uint mainThread = 0; while (!done) { dynamic debugEvent = null; if (!WaitForDebugEvent(ref debugEvent, (int)TimeSpan.FromSeconds(10).TotalMilliseconds)) { throw new LastWin32Exception(); } switch ((NativeDebugEventCode)debugEvent.header.dwDebugEventCode) { case NativeDebugEventCode.CREATE_PROCESS_DEBUG_EVENT: new SafeFileHandle(debugEvent.union.CreateProcess.hFile, true).Dispose(); break; case NativeDebugEventCode.LOAD_DLL_DEBUG_EVENT: new SafeFileHandle(debugEvent.union.LoadDll.hFile, true).Dispose(); break; case NativeDebugEventCode.EXCEPTION_DEBUG_EVENT: case NativeDebugEventCode.EXIT_PROCESS_DEBUG_EVENT: mainThread = debugEvent.header.dwThreadId; done = true; break; } if (!NativeDebuggingMethods.ContinueDebugEvent(debugEvent.header.dwProcessId, debugEvent.header.dwThreadId, NativeDebuggingMethods.ContinueStatus.DBG_CONTINUE)) { throw new LastWin32Exception(); } } SuspendThread(new IntPtr(mainThread)); } finally { if (!NativeDebuggingMethods.DebugActiveProcessStop(dwProcessId)) { throw new LastWin32Exception(); } } }
private static bool WaitForDebugEvent(ref dynamic debugEvent32Or64, int dwMilliseconds) { bool is64BitProcess = (IntPtr.Size == 8); if (is64BitProcess) { var debugEvent64 = new DebugEvent64(); if (!NativeDebuggingMethods.WaitForDebugEvent64(ref debugEvent64, dwMilliseconds)) { return(false); } debugEvent32Or64 = debugEvent64; } else { var debugEvent32 = new DebugEvent32(); if (!NativeDebuggingMethods.WaitForDebugEvent32(ref debugEvent32, dwMilliseconds)) { return(false); } debugEvent32Or64 = debugEvent32; } return(true); }