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.
Date Last Updated: February 12, 1999

Comments
Help!!!!!!
Posted by Legacy on 03/12/2003 12:00amOriginally 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???
Replyand in C# please?
Posted by Legacy on 10/09/2002 12:00amOriginally 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?
ReplyThx!
how do i notify SCM ?
Posted by Legacy on 12/13/2001 12:00amOriginally posted by: mracid
I would like to start one of my tools which starts as trayicon as
Replyservice 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 ...)
Can I make any program as service by using this program ?
Posted by Legacy on 11/08/2001 12:00amOriginally 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
Reply-Prem