|
参照ACE/example中的NT_Service的例子,我写了自己的NT_Service类
并且在NT_Service 的函数svc()中启动Service_Config.open()开启静态服务
----------------------------------------- int
- Win_NT_Service::svc (void)
- {
- ACE_DEBUG ((LM_DEBUG,
- "Service::svc\n"));
- // As an NT service, we come in here in a different thread than the
- // one which created the reactor. So in order to do anything, we
- // need to own the reactor. If we are not a service, report_status
- // will return -1.
- if (report_status (SERVICE_RUNNING) == 0)
- ACE_Reactor::instance ()->owner (ACE_Thread::self ());
- ACE_Service_Config::open
- (argc, argv, ACE_DEFAULT_LOGGER_KEY, 0);
- ACE_STATIC_SVC_REGISTER (Service_Descriptor);
- stop_ = 0;
- while(!stop_) ACE_Reactor::instance ()->run_reactor_event_loop ();
- // Cleanly terminate connections, terminate threads.
- ACE_DEBUG ((LM_DEBUG,
- "Shutting down\n"));
- return 0;
- }
复制代码 --------------------------------------------
出现的问题是:
如果我在main函数中简单的调用NT_Service的svc()函数,则服务正常的运行
--------------------------------------------- ACE_NT_SERVICE_DEFINE (Server,
- Win_NT_Service,
- ACE_TEXT ("Message Service"));
- int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) {
- WIN_NT_SERVICE::instance ()->name (ACE_TEXT ("Server"),
- ACE_TEXT ("Message Service"));
- WIN_NT_SERVICE::instance ()->svc ();
- return 0;
- }
复制代码 -------------------------------------------
然而把NT_Service对象注册到WIndows中后,在服务管理器中启动它,服务却没有启动(客户端无法连接)
并且,在windows服务管理器中选择停止也无法停止该服务,只能在进程管理器中杀死它。
----------------------------------------------------- ACE_NT_SERVICE_DEFINE (Server,
- Win_NT_Service,
- ACE_TEXT ("Message Service"));
- int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) {
- WIN_NT_SERVICE::instance ()->set_arg (argc, argv);
- WIN_NT_SERVICE::instance ()->name (ACE_TEXT ("Server"),
- ACE_TEXT ("Message Service"));
- ofstream *output_file = new ofstream("ntsvc.log", ios::out);
- if (output_file && output_file->rdstate() == ios::goodbit)
- ACE_LOG_MSG->msg_ostream(output_file, 1);
- ACE_LOG_MSG->open(argv[0],
- ACE_Log_Msg::STDERR | ACE_Log_Msg::OSTREAM,
- 0);
- ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%T (%t): Starting service.\n")));
- ACE_NT_SERVICE_RUN (RMServer,
- WIN_NT_SERVICE::instance (),
- ret);
- if (ret == 0)
- ACE_ERROR ((LM_ERROR,
- ACE_TEXT ("%p\n"),
- ACE_TEXT ("Couldn't start service")));
- else
- ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%T (%t): Service stopped.\n")));
-
- return 0;
- }
复制代码
不清楚当NT_Service在windows服务管理器中被启动的时候,是不是直接调用我注册的exe,并且是指向svc()这个函数的句柄,它的调用喝平常的启动exe程序有没有什么本质的区别,以至于两种方式会产生完全不同的结果。
另外在windows管理器中选择停止服务不能结束也是很奇怪的问题,我不知道该怎么调试,因为一旦这个服务被注册到windows服务管理器中,就不能象在VC中哪样单步跟踪和命令行输出DEBUG信息了,多谢指点~~!! |
|