除了對 GUI 程式偵錯以外,有時候,像我這次一樣,必須對一台不能夠灌任何開發環境的電腦偵錯,這個時候,就需要用到 remote debugging。以下簡單整理一下,如何利用 Visual C++ 6[1] 進行 remote debugging。

  1. 若是 debug 版,要把 MSVCRTd.dll 安裝到執行檔所在目錄,因為沒有灌開發環境的 Windows 裡,預設不會有 debug 版的 Microsoft Visual C++ run-time library,即 MSVCRTd.dll[2]
  2. 將執行檔所需的完整目錄環境,利用網路芳鄰,分享給遠端機器,成為一個連線磁碟機。我的習慣是,在本地端準備一個目錄叫 runtimes,然後利用 NTFS Junction 於 runtimes 目錄下,建立指到各個專案執行目錄的 symbolic links。假設遠端機器看到的 runtimes 目錄是 R: 磁碟機,專案執行目錄就是 R:\foo-rt
  3. 執行下面附的 BATCH 程式,把準備在遠端執行,以便讓本地端可以用 debugger 遙控程式執行的工具程式,MSVCMON.EXE 安裝到指定的目錄下。我選擇裝到 runtimes\remote-dbg 目錄,這樣就可以直接在遠端執行 R:\foo-rt\remote-dbg\MSVCMON.EXE
    @ECHO OFF
    
    IF "%1"=="" GOTO :USAGE
    SET install_dir=%1
    SET vs6_dir=%ProgramFiles%\Microsoft Visual Studio
    
    XCOPY "%vs6_dir%\Common\MSDev98\Bin\DM.dll"       "%install_dir%" /Y /F /D
    XCOPY "%vs6_dir%\Common\MSDev98\Bin\MSDIS110.DLL" "%install_dir%" /Y /F /D
    XCOPY "%vs6_dir%\Common\MSDev98\Bin\MSVCMON.EXE"  "%install_dir%" /Y /F /D
    XCOPY "%vs6_dir%\Common\MSDev98\Bin\TLN0T.DLL"    "%install_dir%" /Y /F /D
    XCOPY "%SystemRoot%\system32\msvcp60.dll"         "%install_dir%" /Y /F /D
    XCOPY "%SystemRoot%\system32\psapi.dll"           "%install_dir%" /Y /F /D
    
    EXIT /B
    
    :USAGE
    ECHO Usage: vc6-remote_debugger.bat [install-dir]
    ECHO Install VC6 remote debugger files in directory [install-dir].
    GOTO :EOF
    
  4. 從 VC6 選單 Build -> Debugger remote connection... 選 Network(TCP/IP),按 Settings... 鈕輸入遠端機器 ip,然後按 OK。
  5. 執行檔 project setting 的 Debug 頁,設定如下:
    • Working directory: R:\foo-rt (遠端觀點)
    • Remote executable path and file name: R:\foo-rt\foo.exe (遠端觀點)
  6. 在遠端機器執行 R:\remote-dbg\MSVCMON.exe,按下 Connect... 鈕。
  7. 按 F5 開始 happy remote debugging。

更詳細的說明,請參見《Remote Debugging in MSVC++ 6.0 - Step by step in the OnPaint message handler》這篇文章。基本上,使用 remote desktop 操控遠端,進行 debug,還蠻方便的。不過 bug 若還是一直找不到,那就不太能愉快了。


  1. 其他版本的 visual c++,我懶得整理方法了。理論上應該差不多,大家自己研究囉。
  2. 事實上,太新的 Visual C++ 版本的 release 版 run-time library 也不一定有,故用新版開發工具編造出來的程式,放到舊版的 Windows 上,就需要類似 run-time distributable 的東西。