Windows NT Service Manager

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

For the last few weeks I had been working on manipulating the Windows NT Services. Actually I was assigned a task to implement an already running server as a Windows NT Service. I had never done that sort of work before but when I started working on it, I found my self enjoying this one. After complementing my assigned job, I thought about writing a generic programm that allows you to manipulate the services in a flexible way. And I started working on the "Windows NT Service Manager". I could not find the time to organize the service specific code into one class. So as the result the code is scattered little bit. You can find service specific code in the following two classes included in the demo project:

1) CServiceInstallerDlg
2) CServiceControlDlg


Well, I will try to explain a little bit of my code, rest I leave upto u :o)

The first function that I am going to introduce is the function that is the message handler for "Istall the service" button.


void CServiceInstallerDlg::OnInstall()
{
 // TODO: Add your control notification handler code here
 CWaitCursor cur1;
 UpdateData(TRUE); // update the data

 if (schSCManager)
  schSCManager = 0;

 if(myServiceHandle)
  myServiceHandle = 0;

 DWORD dwDesiredAccess;
 DWORD dwServiceType;
 DWORD dwStartType;
 DWORD dwErrorControl;

 GetParams(&dwDesiredAccess,&dwServiceType,&dwStartType,&dwErrorControl);
 // All set, let us call CreateService

 // First open the handle to the SCM
 schSCManager = OpenSCManager(NULL,  NULL, GENERIC_WRITE);

 if (schSCManager == NULL)
 {
  AfxMessageBox("Error while opening Service Control Manager.",MB_OK|MB_ICONSTOP);
  return;
 }

 // Now create the handle to the service
 myServiceHandle = 
  CreateService(schSCManager, m_ServiceName.GetBuffer(0), 
   m_DisplayName.GetBuffer(0), dwDesiredAccess, dwServiceType, 
   dwStartType, dwErrorControl, m_BinaryFilePath.GetBuffer(0),
   "", NULL, "", NULL, NULL);

 if (myServiceHandle == NULL)
 {
  DWORD er = GetLastError();

  switch(er)
  {
   case ERROR_ACCESS_DENIED: 
    AfxMessageBox("Error while creating the service."
     "\rError type:\r\tAccess is denied.",MB_OK|MB_ICONSTOP);
   break;

   case ERROR_INVALID_NAME: 
    AfxMessageBox("Error while creating the service."
     "\rError type:\r\tInvalid service name.",MB_OK|MB_ICONSTOP);
   break;

   case ERROR_SERVICE_EXISTS: 
    AfxMessageBox("Error while creating the service."
     "\rError type:\r\tService already exists.",MB_OK|MB_ICONSTOP);
   break;

   case ERROR_DUP_NAME: 
    AfxMessageBox("Error while creating the service."
    "\rError type:\r\tThe display name is already in use.",MB_OK|MB_ICONSTOP);
   break;

   default: 
    AfxMessageBox("Error while creating the service."
     "\rError type:\r\tNot known.",MB_OK|MB_ICONSTOP);
   break;
  }
 }
 else
 {
  AfxMessageBox("New service registered. Installation completed successfuly",MB_OK|MB_ICONINFORMATION);
 }

 CloseServiceHandle(schSCManager);

 // close the handle to SCM
 CloseServiceHandle(myServiceHandle);

 // close the handle to service, we will reopen it if we need it further
 myServiceHandle = 0;
 schSCManager = 0;
}

Ok, the above presented code might seem lengthy but it's key points are very few, only two. Yes the first function is OpenSCManager which gives us the handle to Service Control Manager of Windows NT. And the second function is CreateService. CreateService is the function that registers our service to the SCM(Service Control Manager).Don't worry about function GetParams it is just a member function of class CServiceInstallerDlg. The rest of code is for getting values from different dialog controls and to for doing some error checking.

Well no more code, u can find it in the source project. But know I will give you some instructions to go through the code,

i ) Any function that starts with or ends at word Service and is not the member function of classes defined in project, is a Windows NT service API function.

ii ) I have tried to place comments at various places in code, do read them.

And one last thing, any comments will be welcomed.

Special thanks to Giancarlo Iovino his CHyperLink class.

Download source- 65 KB

Download application - 116 KB

Date Last Updated: February 12, 1999



Comments

  • Help!!!!!!

    Posted by Legacy on 03/12/2003 12:00am

    Originally posted by: david

    I've created a nt service and want to copy files with CopyFile within the service over the network.

    But I get always ERROR_ACCESS_DENIED!!

    When I copy files with CopyFile within a normal exe it workes fine.

    Is it a Microsoft bug???

    Reply
  • and in C# please?

    Posted by Legacy on 10/09/2002 12:00am

    Originally posted by: Micha

    Hi u all,

    how could I do this all, especially to install and uninstall services. I want to make a executable file run as a windows service in C# within runtime. How could I do that?
    Thx!

    Reply
  • how do i notify SCM ?

    Posted by Legacy on 12/13/2001 12:00am

    Originally posted by: mracid

    I would like to start one of my tools which starts as trayicon as
    service with help of your service-manager
    and wondered how to to tell the NT-SCM out of my code that my programm was
    started correctly.
    (like you do ReportStatusToSCMgr(SERVICE_RUNNING,NO_ERROR,0))
    or how could I receive SCM Messages in my programm if somebody says
    ../services/myservice/stop or pause ...)

    Reply
  • Can I make any program as service by using this program ?

    Posted by Legacy on 11/08/2001 12:00am

    Originally posted by: prem

    Can I use this program to make any VC++ program as a service. I used the monitor and pointed to VC++ program thinking that I can run it as a service but the program was not starting as a service.

    Should I make any changes in my VC++ program to run as service ?

    Thanks
    -Prem

    Reply
  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date