[Ace-users] How to integrate Service_Config frame with NT_Service frame?

zhengfish zhengfish at gmail.com
Wed Jan 23 21:04:37 CST 2008


I want to port my application from linux to win32 platform.

On Linux, I use the ACE_Service_Config to load my dynamic services in
my main program. I run it with parameter "-d" for debug, or with
parameter "-b" as a daemon.
But on win32 platform, the main program can't work with parameter "-b"
to be a daemon(nt_service). So I must implement a win32 service
program( using ACE_NT_Service) to load my dynamic services(using
ACE_Service_Config).

I copy and modify the ACE_ROOT/examples/NT_Service source codes, I
found it works in debug mode, but it can't work in service/daemon
mode.

The ACE_Service_Config::open( "NTSVC" ) always return 0(=success)
whether there are the svc.conf file and the dynamic service program.
As a result the dynamic service can't be loaded.

==========================Source slice===================
int
Service::svc (void)
{
#if 1
	ofstream *output_file = new ofstream("svc.log", ios::out);

	if (output_file && output_file->rdstate() == ios::goodbit)
		ACE_LOG_MSG->msg_ostream(output_file, 1);

	ACE_LOG_MSG->open( "NTSVC",
		ACE_Log_Msg::STDERR
		| ACE_Log_Msg::SYSLOG
		| ACE_Log_Msg::OSTREAM,
		0);

	ACE_LOG_MSG->priority_mask ( LM_DEBUG | LM_INFO | LM_ERROR,
ACE_Log_Msg::PROCESS );

#endif

	ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Service::svc ...\n") ));

	ACE_TCHAR Buffer[1024]={0};
	DWORD dwRet = GetCurrentDirectory(1024, Buffer);
	ACE_DEBUG ((LM_DEBUG, "Service::svc @ %s\n", Buffer ));

	// 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)
		reactor ()->owner (ACE_Thread::self ());

	this->stop_ = 0;

	// Schedule a timer every two seconds.
	ACE_Time_Value tv (2, 0);
	ACE_Reactor::instance ()->schedule_timer (this, 0, tv, tv);


#if 1

	int rc = ACE_Service_Config::open( "NTSVC" );
	if ( rc != 0 )
	{
		ACE_ERROR_RETURN( ( LM_ERROR,
			ACE_TEXT( "(%N|%l).%p\n" ), ACE_TEXT( "open" ) ), -1 );
	}
	else
	{
		ACE_DEBUG( ( LM_INFO, "ACE_Service_Config::open( ) is OK\n" ) );
	}

#endif

	while (!this->stop_)
	{
		reactor ()->handle_events ();
	}

	// Cleanly terminate connections, terminate threads.
	ACE_DEBUG ((LM_DEBUG,
		ACE_TEXT ("Shutting down\n")));

	reactor ()->cancel_timer (this);

	return 0;
}



More information about the Ace-users mailing list