anand
anand

Reputation: 11319

Debugging application when launched via ShellExecute

I am trying to launch an application via the ShellExecute() API call. This application contains only a main function which does some processing and exits.

Now I have put DebugBreak() in starting of main. When ShellExecute() is called the application is launched successfully but it does not ask for breaking.

How can I debug my application when launched from other application using ShellExecute()?

I am using VC++ .

Upvotes: 0

Views: 1412

Answers (5)

Stephen Kellett
Stephen Kellett

Reputation: 3236

The method I use for things like this is to embed some interactive code, which you can either delete afterwards, comment out or conditionally enable. In a few cases we have this code enabled by querying an environment variable which is set by the tool that launches the main application. This allows me to click a check box, hit launch and have the breakpoint dialog in seconds.

if (MessageBox(NULL, 
               _T("Attach the debugger now, then choose Yes to hit a breakpoint"), 
               _T("Attach Debugger"), 
               MB_YESNO) == IDYES)
    __debugbreak();

This gives you the ability to attach the debugger when the dialog box appears and the option to hit a breakpoint or not. My earlier versions didn't give me the option and after a while I realised some of the time I wanted the breakpoint, and some of the time I didn't.

Upvotes: 0

James Hirschorn
James Hirschorn

Reputation: 7994

There is now a Microsoft Child Process Debugging Power Tool.

Upvotes: 0

wenhm
wenhm

Reputation: 106

you can try this, it's ok in xp system.

app.exe is your application name,

-s1...-s3 is command line arguments.

HINSTANCE hRet = ShellExecute(NULL, L"open", L"vsjitdebugger.exe", L" app.exe -s1 a1 -s2 a2 a3 -s3", szExePath, SW_SHOW);

Upvotes: 0

Leo Davidson
Leo Davidson

Reputation: 6143

If DebugBreak() isn't workign for you, try _CrtDbgBreak(). Note that _CrtDbgBreak only works in a debug build.

_CrtDebugBreak definitely works for me to make a launched process break on startup, although I'm pretty sure DebugBreak does also.

Note that both functions will make it look like the process has crashed, since they raise an exception. That is normal and gives you the opportunity to attach a debugger via the crash dialog. (The crash dialog also lets you terminate the process; don't use that, obviously.)

Also note that if you have a catch-all SEH exception handler around your main then the exception raise by DebugBreak & friends will be swallowed up and the app will simply exit without showing the crash dialog or letting you attach to it.

Upvotes: 1

Ana Betts
Ana Betts

Reputation: 74652

You can't do this with VC++; with WinDbg this is just .childdbg 1 to debug all child processes. With VC++, you can use Image File Execution Options in a pinch - check out http://codereflect.com/2009/09/20/how-to-debug-child-process-using-windbgvisual-studio/ for more info. Really though, if you've got the time to learn WinDbg, it's much better at this.

Upvotes: 0

Related Questions