Attaching to a process without a dialog


In http://blogs.msdn.com/shaykatc/archive/2004/04/19/116222.aspx, Shaykat gave one alternative to the ntsd ‘-pn’ option. I figured I would give two more.


Alternative #1 – Attach project. Open the exe as a project (File->Open->Project, then select the exe). Open project properties. Set the ‘Attach’ setting to ‘yes’. Hit F5.


Alternative #2 – command script. It is pretty easy to write a script using tlist.exe that can attach to a process by name. The script is below. Hopefully in some future product, you will even be able to set an option to skip the JIT chooser dialog. Note: This only works for native debugging.

@echo off
if “%1″==”-?” goto help
if “%1″==”/?” goto help
if “%1″==”” echo The syntax of the command is incorrect.& exit /b -1
if NOT “%2″==”” echo The syntax of the command is incorrect.& exit /b -1

call :find_file tlist.exe
if NOT %ERRORLEVEL%==0 exit /b %ERRORLEVEL%

if not exist “%CommonProgramFiles%\Microsoft Shared\VS7Debug\vs7jit.exe” echo Could not find vs7jit.exe & exit /b 1

setlocal
set __found=0
for /f “tokens=1,2” %%d in (‘tlist.exe’) do call :parse_process %1 %%e %%d
if %__found%==0 echo Process ‘%1’ is not running
endlocal
exit /b 0

:parse_process
if /i “%1″==”%2” goto run_command
if /i “%1.exe”==”%2” goto run_command
exit /b 0

:run_command
set __found=1
call “%CommonProgramFiles%\Microsoft Shared\VS7Debug\vs7jit.exe” -p %3
exit /b 0

:find_file
if “%~$PATH:1″==”” if “%~z1” == “” echo Error! Could not find %1. & exit /b -1
exit /b 0

:help
echo attach.cmd ^<process_name^>
echo.
echo Debug a process with VS7/VS7.1. example: attach.cmd notepad.exe
echo.


Comments (8)

  1. RichB says:

    Didn’t tlist die with the dinosaurs! TaskList rules! (XP & Win2K3 only)

  2. dave vespa says:

    Greg,

    In your first example you talk about opening the .exe as a project. Are you speaking of using VS.NET 7.1? I tried doing that but it would not let me choose an .exe. It just kept looking for *.vbproj, *.csproj, etc.

    Help

    D

  3. Gregg Miskelly says:

    Opening the exe as a project uses the C++ project system, so you need a SKU which supports the C++ project system.

  4. Dumky says:

    Use a VS.net macro. MSDN has some sample on how to attach to a process by name.

  5. Woo Seo Seok says:

    I think your batch file is useful, but it doesn’t work in Windows XP. So I modified it and now it works!

    @echo off

    if "%1"=="-?" goto help

    if "%1"=="/?" goto help

    if "%1"=="" echo The syntax of the command is incorrect.& exit /b -1

    if NOT "%2"=="" echo The syntax of the command is incorrect.& exit /b -1

    call :find_file tasklist.exe

    if NOT %ERRORLEVEL%==0 exit /b %ERRORLEVEL%

    if not exist "%CommonProgramFiles%Microsoft SharedVS7Debugvs7jit.exe" echo Could not find vs7jit.exe & exit /b 1

    setlocal

    set __found=0

    for /f "tokens=1,2" %%d in (‘tasklist.exe’) do call :parse_process %1 %%e %%d

    if %__found%==0 echo Process ‘%1’ is not running

    endlocal

    exit /b 0

    :parse_process

    if /i "%1"=="%3" goto run_command

    if /i "%1.exe"=="%3" goto run_command

    exit /b 0

    :run_command

    set __found=1

    call "%CommonProgramFiles%Microsoft SharedVS7Debugvs7jit.exe" -p %2

    exit /b 0

    :find_file

    if "%~$PATH:1"=="" if "%~z1" == "" echo Error! Could not find %1. & exit /b -1

    exit /b 0

    :help

    echo attach.cmd ^<process_name^>

    echo.

    echo Debug a process with VS7/VS7.1. example: attach.cmd notepad.exe

    echo.

  6. Gregg Miskelly says:

    Opening an EXE as a project is done by the VC project system, so you need to have VC installed. As long as you have that it should ‘just work’ in 2002, 2003 or 2005 versions.