Welcome to MSDN Blogs Sign in | Join | Help
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.
Posted: Thursday, April 22, 2004 2:35 PM by greggm

Comments

RichB said:

Didn't tlist die with the dinosaurs! TaskList rules! (XP & Win2K3 only)
# April 23, 2004 6:55 AM

Sam Gentile's Blog said:

# April 23, 2004 3:01 PM

Code/Tea/Etc... said:

# April 25, 2004 3:07 AM

dave vespa said:

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
# May 6, 2004 9:58 AM

Gregg Miskelly said:

Opening the exe as a project uses the C++ project system, so you need a SKU which supports the C++ project system.
# May 6, 2004 11:28 AM

Dumky said:

Use a VS.net macro. MSDN has some sample on how to attach to a process by name.
# May 23, 2004 11:34 PM

Woo Seo Seok said:

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 Shared\VS7Debug\vs7jit.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 Shared\VS7Debug\vs7jit.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.
# July 6, 2004 2:55 AM

Gregg Miskelly said:

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.
# August 2, 2004 8:53 AM
New Comments to this post are disabled
Page view tracker