Windows NT Service Manager

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

  • how the clarisonic cleaning technique to pan out

    Posted by iouwanzi on 06/06/2013 02:58pm

    [url=http://www.australiaclarisonic.com/]clarisonic brush[/url] ghd pas cher peut être une marque japonaise de qui vend le fer à lisser en céramique et même des produits qui comprennent un sèche-cheveux, bonne prise en charge ainsi que des brosses. Que les modèles de plaque de céramique habituellement sont associés à premium et en particulier le lisseur chauffe en quelques minutes. At taper l’ultramoderne sera même petit bip qui vous avertit qu’il peut être tous ensemble.Superb confortable ! [url=http://www.miaclarisonicaustralia.org/clarisonic-pro]clarisonic pro[/url] vous vous rendrez probablement compte généralement le type ghd pink styler, à qui les rend étanches tête de cheveux ou de style et de défriser les cheveux crépus même.Nous ne sommes pas nécessairement à vous informer de vos solutions parce que j’ai consommera jamais. (Ouais, donner Document il temps de séché mes cheveux crépus en plein air et que j’ai des vagues saines alors zéro doivent avoir de tels dispositifs pour le personnel). [url=http://www.australiaclarisonic.com/clarisonic-pro]clarisonic pro[/url] Mon conjoint et j’ai très certainement besoin personne se produit et en outre choisir de me donner personnellement un Styler F HI-DEF en ce qui concerne la période des fêtes… donc si il l’homme devrions vraiment besoin pour gagner me réjoui, mon conjoint et moi obtenir la connexion pointe GHD Gold Styler et Document a parlé du fait que le Styler juste est au prix de 179 kilos, ainsi que la boîte proverbiale coûte 215 euros nuit gamme. (Enorme wink énorme rustre certainement pas très discret en toute façon.

    Reply
  • 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

  • You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this webcast that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base application that will jumpstart you into building your own more complex app …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds