Overview
Features
Download
Documentation
Community
Add-Ons & Services

PID of Windows Service

A general discussion forum.

PID of Windows Service

Postby codecandy2k » 24 Jan 2012, 01:54

It is sometimes useful to be able to obtain the PID of a running WIndows Service. It's a fairly trivial task to add it to Poco::WinService.


Code: Select all
Poco::UInt32 WinService::pid() const
{
   if (!isRunning())
      throw Poco::SystemException("Cannot get the PID of a service that isn't running.");

   open();
   try
   {
      DWORD dwBytesNeeded = 0;
      SERVICE_STATUS_PROCESS status;
      if (QueryServiceStatusEx( _svcHandle, SC_STATUS_PROCESS_INFO, (LPBYTE)&status, sizeof(status), &dwBytesNeeded ))
      {
         close();
         return static_cast<Poco::UInt32>(status.dwProcessId);
      }
      else throw SystemException( "cannot query service status", GetLastError() );
   }
   catch( Poco::Exception& e )
   {
      close();
      throw;
   }
}
codecandy2k
 
Posts: 40
Joined: 03 Dec 2011, 18:48

Re: PID of Windows Service

Postby marlowabnp » 09 Feb 2012, 16:17

Wouldn't it be better to return a poco process handle, from which you can get the pid?
marlowabnp
 
Posts: 89
Joined: 08 Nov 2010, 17:29

Re: PID of Windows Service

Postby codecandy2k » 09 Feb 2012, 19:21

It's an alternative, but I don't know if it's better. You may want the PID without opening a handle to it. And in order to actually make the handle useful the ProcessHandle class would have to be heavily extended. Right now it is only made to work with processes that are launched by your application and has very little utility outside of setting up pipes to the child process. Making the class useful for working with arbitrary processes could be another good thing to do, but I'm not sure how well that would translate to all of the platforms Poco supports. WinService is already Windows only, so no worries there ;)
codecandy2k
 
Posts: 40
Joined: 03 Dec 2011, 18:48


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest