Windows NT Service Manager | CodeGuru

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. […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 12, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.