描述:
我写的服务在启动和运行的时候都很正常,但是停止的时候报一个访问内存出错,所以想调试跟踪一下。
解决方案1:
没有上面说的这么复杂,在VC中打开你写的服务程序,在客户端能调用的代码断中设置断点,F5启动调试器;启动客户端,调用服务程序,服务程序则运行到断点处,就可以调试啦。
对于非本地服务程序(DCOM),需要在客户端先注册Service,设置为在服务器端运行,VC装在服务器端。接下来步骤跟上面相似。
办法1:可以不注册成服务,注册成Register as Local Server后,
当作本地exe来调试,客户创建接口后自动启动.exe在进程中,直接跟即可
_tWinMain
{
// Register as Local Server
if (lstrcmpi(lpszToken, _T("RegServer"))==0)
return _Module.RegisterServer(TRUE, FALSE);
// Register as Service
if (lstrcmpi(lpszToken, _T("Service"))==0)
//return _Module.RegisterServer(TRUE, TRUE);
return _Module.RegisterServer(TRUE, FALSE);//<------
}
或者:
可以临时更改注册表,使程序作为普通的本地服务器运行。只需将 AppID 下的 LocalService 值重命名为 _LocalService,并确保在 CLSID 下正确设置 LocalServer32 项。
办法2:
是使用 Windows NT 4.0 或 Windows 2000 中的任务管理器。当服务正在运行时,启动任务管理器并单击“进程”选项卡。右击 EXE 的名称然后单击“调试”。这将启动附加到运行进程的 Visual C++。现在,在“调试”菜单上单击“中断”以在代码中设置断点。单击“运行”运行到选定的断点。
HOWTO: Debugging a service
ID: Q98890
--------------------------------------------------------------------------------
The information in this article applies to:
Microsoft Win32 Application Programming Interface (API), used with:
the operating system Microsoft Windows NT, versions 3.51, 4.0
the operating system Microsoft Windows 2000
There are two techniques for debugging a service. The first technique involves adding a DebugBreak() statement to the service's code and letting the Just-in-time(JIT) debugging feature of Windows NT spawn the debugger when the service executes the DebugBreak(). The second technique involves attaching the debugger to the service while it is running.
有2个方法可以调试service, 这里的调试指的是用Debug工具,例如Msdev.exe或WinDbg进行语句跟踪,而不是指输出event或log file之类.
第一种方法:
在你的service's code中加入产生Debug中断的语句DebugBreak().当程序执行到此时弹出一个DLG框,你可以选择启动Msdev.exe从DebugBreak()语句后,开始跟踪.
从实际使用来说,我觉得这种方法适合于调试个别简单错误,比较初级,而且需要额外修改service程序,
第二种方法:
attaching debugger(Msdev.exe)到一个正在运行的service上.