views:

53

answers:

3

Dear All, I would like to know if there is any command or tool that can be used to start a process and then pause it immediately.

I need this function so that I can have time to attach a debugger to it. I have tried visual studio's /debugexe feature, but the behavior of the program seemed changed. So I need to find other way to attach it and debug.

Thank you.

+3  A: 

You can use CreateProcess() with CREATE_SUSPENDED flag. This will start the process with the main thread suspended. Then you call ResumeThread() after having attached.

sharptooth
A: 

How about adding a Sleep() call as the very first statement.

R Samuel Klatchko
That will work most of the times. But if the problem lies in something happening before WinMain() being called it will be useless.
sharptooth
A: 

You can also add a call to

System.Diagnostics.Debugger.Break();

in the code. It will suspend the program and then ask you to choose the debugger you want to use. You can then attach to a running Visual Studio instance. If you're already running from a debugger, it will simply break as if it would have hit a breakpoint.

Rytmis