
- •Contents of the Report
- •Introduction
- •1. Algorithm of the service program
- •2. Algorithms of the service management program
- •3. Algorithm of the client program
- •4. Implementation of service program
- •5. Implementation of service management program
- •6. Implementation of client program
- •Appendix a Source code of service program
- •Appendix b Source code of scm_console program
- •Appendix c Source code of client program
Appendix b Source code of scm_console program
AD. 091501.06/67833.
Pages______
Donetsk - 2010
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#define BUF_LEN 4096
#ifdef UNICODE
#define tcin wcin
#define tcout wcout
#else
#define tcin cin
#define tcout cout
#endif
using namespace std;
//install service func
void install_serv(void)
{
LPTSTR path = new TCHAR[255];
SC_HANDLE scService,scManager;
cout<<"Enter full path+name to service program"<<endl;
tcin>>path;
scManager = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
scService = CreateService(scManager,_T("serv_cs06g_v17"),_T("serv_cs06g_v17"),SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,path,NULL,NULL,NULL,NULL,NULL);
if (scService == NULL)
cout<<"\tsome errors in registering service!"<<endl;
else
cout<<"Service is registered succesfully"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
free(path);
}
//remove service func
void remove_serv(void)
{
SC_HANDLE scService,scManager;
SERVICE_STATUS status;
//открытие сервис_менеджера
scManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
//открытие сервиса с параметром запроса состояния и остановки
scService = OpenService(scManager,_T("serv_cs06g_v17"),SERVICE_STOP|SERVICE_QUERY_STATUS);
if (scService == NULL)
{
cout<<"\tOpenService Error!"<<endl;
return;
}
//запрос состояния сервиса
QueryServiceStatus( scService, &status );
if (!(status.dwCurrentState == SERVICE_STOPPED))
{
if (! ControlService(scService,SERVICE_CONTROL_STOP,&status))//остановка сервиса
{
cout<<"\tContRolService Error."<<endl;
return;
}
}
CloseServiceHandle(scService);
//отрытие сервиса с правами УДАЛИТЬ
scService = OpenService(scManager,_T("serv_cs06g_v17"),DELETE);
if (scService == NULL)
{
cout<<"\tError in OpenService"<<endl;
CloseServiceHandle(scManager);
return;
}
if (! DeleteService(scService) )
cout<<"\tDeleteService error."<<endl;
else
cout<<"\tDeleteService SUCCESS."<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
}
//start service func
void start_serv(void)
{
SC_HANDLE scService,scManager;
SERVICE_STATUS status;
//открытие сервис_менеджера
scManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
//открытие сервиса c правами запроса состояние и запуска
scService = OpenService(scManager,_T("serv_cs06g_v17"),SERVICE_QUERY_STATUS|SERVICE_START);
if (scService == NULL)
{
cout<<"\tOpenService Error!"<<endl;
CloseServiceHandle(scManager);
return;
}
QueryServiceStatus( scService, &status );
if (status.dwCurrentState == SERVICE_RUNNING)
{
cout<<"service is running"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
if (StartService(scService,0,NULL)==0)
{
cout<<"error in StartService"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
cout<<"\t\tStarted succsesfully"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
}
//stop service func
void stop_serv(void)
{
SC_HANDLE scService,scManager;
SERVICE_STATUS status;
//
scManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
//
scService = OpenService(scManager,_T("serv_cs06g_v17"),SERVICE_ALL_ACCESS);
if (scService == NULL)
{
cout<<"\tOpenService Error!"<<endl;
return;
}
QueryServiceStatus( scService, &status );
if (status.dwCurrentState == SERVICE_STOPPED)
{
cout<<"service is stopped"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
if (!ControlService(scService,SERVICE_CONTROL_STOP,&status))
{
cout<<"\tContRolService Error"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
cout<<"Service is stopped successfuly"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
}
//restart service func
void restart_serv(void)
{
stop_serv();
start_serv();
}
void menu(void)
{
cout<<"\t\t\tMenu SCM"<<endl;
cout<<"1. install service"<<endl;
cout<<"2. remove service"<<endl;
cout<<"3. start service"<<endl;
cout<<"4. stop service"<<endl;
cout<<"5. restart service"<<endl;
cout<<"6. pause service"<<endl;
cout<<"7. resume service"<<endl;
cout<<"8. show status of service (running, pause, stoped)"<<endl;
cout<<"9. show and modify logon properties"<<endl;
cout<<"0. show and modify recovery properties"<<endl;
cout<<"q. show dependensies of service"<<endl;
cout<<"w. exit."<<endl;
}
//pause service func
void pause_serv(void)
{
SC_HANDLE scService,scManager;
SERVICE_STATUS status;
//открытие менеджера
scManager = OpenSCManagerW(NULL,NULL,SC_MANAGER_ALL_ACCESS);
//открытие сервиса с параметром доступа пауза_родолжение т запроса статуса
scService = OpenService(scManager,_T("serv_cs06g_v17"),SERVICE_PAUSE_CONTINUE|SERVICE_QUERY_STATUS);
if (scService == NULL)
{
cout<<"Error OpenService"<<endl;
CloseServiceHandle(scManager);
return;
}
QueryServiceStatus( scService, &status );
if (!(status.dwCurrentState == SERVICE_RUNNING))
{
cout<<"Service is NOt in running state"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
if (! ControlService(scService,SERVICE_CONTROL_PAUSE,&status))
{
cout<<"\tContRolService Error"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
cout<<"\tservice is paused."<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
}
//continue service func
void cont_serv(void)
{
SC_HANDLE scService,scManager;
SERVICE_STATUS status;
//открытие менеджера
scManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
//открытие сервиса с параметром доступа пауза_родолжение т запроса статуса
scService = OpenService(scManager,_T("serv_cs06g_v17"),SERVICE_PAUSE_CONTINUE|SERVICE_QUERY_STATUS);
if (scService == NULL)
{
cout<<"Error OpenService"<<endl;
CloseServiceHandle(scManager);
return;
}
QueryServiceStatus( scService, &status );
if (!(status.dwCurrentState == SERVICE_PAUSED))
{
cout<<"Service is NOt in paused state"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
if (! ControlService(scService,SERVICE_CONTROL_CONTINUE,&status))
{
cout<<"\tContRolService Error"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
cout<<"\t\tservice is paused."<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
}
//current status service
void status_serv(void)
{
SC_HANDLE scService,scManager;
SERVICE_STATUS status;
//
scManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
//
scService = OpenService(scManager,_T("serv_cs06g_v17"),SERVICE_QUERY_STATUS);
if (scService == NULL)
{
cout<<"\tOpenService Error!"<<endl;
return;
}
//
QueryServiceStatus( scService, &status );
//cont pending
if (status.dwCurrentState == SERVICE_CONTINUE_PENDING)
{
cout<<"Service state is SERVICE_CONTINUE_PENDING"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
//pause pend
if (status.dwCurrentState == SERVICE_PAUSE_PENDING)
{
cout<<"Service state is SERVICE_PAUSE_PENDING"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
//paused
if (status.dwCurrentState == SERVICE_PAUSED)
{
cout<<"Service state is SERVICE_PAUSED"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
//running
if (status.dwCurrentState == SERVICE_RUNNING)
{
cout<<"Service state is SERVICE_RUNNING"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
//sart pend
if (status.dwCurrentState == SERVICE_START_PENDING)
{
cout<<"Service state is SERVICE_START_PENDING"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
//stop pend
if (status.dwCurrentState == SERVICE_STOP_PENDING)
{
cout<<"Service state is SERVICE_STOP_PENDING"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
//stoped
if (status.dwCurrentState == SERVICE_STOPPED)
{
cout<<"Service state is SERVICE_STOPPED"<<endl;
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
}
//logon props
void logon_prop(void)
{
char ch;
SC_HANDLE scService,scManager;
LPQUERY_SERVICE_CONFIG lqsc;
DWORD dwBytesNeeded;
//открытие сервис_менеджера
scManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
//открытие сервиса c правами запроса состояние и запуска
scService = OpenService(scManager,_T("serv_cs06g_v17"),SERVICE_QUERY_CONFIG|SERVICE_CHANGE_CONFIG);
if (scService == NULL)
{
cout<<"\tOpenService Error!:["<<endl;
CloseServiceHandle(scManager);
return;
}
lqsc = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 4096);
if (lqsc == NULL)
{
cout<<"Error. Can nor allocate mem for buffer in heap."<<endl;
return;
}
if (!QueryServiceConfig(scService,lqsc,BUF_LEN,&dwBytesNeeded))
{
printf("QueryServiceConfig error.");
LocalFree(lqsc);
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
//start type
cout<<"\tStart type of service"<<endl;
switch(lqsc->dwStartType)
{
case SERVICE_AUTO_START:
cout<<"A service started automatically."<<endl;
break;
case SERVICE_BOOT_START:
cout<<"A device driver started by the system loader. "<<endl;
break;
case SERVICE_DEMAND_START:
cout<<"A service started by the service control manager when a process calls the StartService function."<<endl;
break;
case SERVICE_DISABLED:
cout<<"A service that cannot be started."<<endl;
break;
case SERVICE_SYSTEM_START:
cout<<"A device driver started by the IoInitSystem function."<<endl;
break;
}
//service start account
cout<<"Service Start account: \t";
tcout.write(lqsc->lpServiceStartName,_tcslen(lqsc->lpServiceStartName));
cout<<endl;
cout<<"If you want to modify start account press 1 "<<endl;
cin>>ch;
if (ch == '1')
{
cout<<"\tGroups"<<endl<<"1. LocalService"<<endl<<"2. NetworkService"<<endl<<"3. LocalSystem"<<endl<<"Select group(1,2,3), wich will load service program"<<endl;
cin>>ch;
switch (ch)
{
case '1':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,lqsc->dwStartType,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,_T("LocalService"),NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
case '2':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,lqsc->dwStartType,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,_T("NetworkService"),NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
case '3':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,lqsc->dwStartType,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,_T("localSystem"),NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
default:
cout<<"Wrong selection for modifying account."<<endl;
}
}
cout<<"If you want to modify start-up parameters, press 1"<<endl;
cin>>ch;
if (ch == '1')
{
cout<<"\tStart-Up modes:"<<endl<<"1. SERVICE_AUTO_START (A service started automatically by the SCM during system startup.)"<<endl<<"2. SERVICE_BOOT_START (A device driver started by the system loader.)"<<endl<<"3. SERVICE_DEMAND_START (A service started by the SCM when a process calls the StartService function.)"<<endl<<"4. SERVICE_DISABLED (A service that cannot be started.)"<<endl<<"5. SERVICE_SYSTEM_START (A device driver started by the IoInitSystem function.)"<<endl;
cin>>ch;
switch (ch)
{
case '1':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,SERVICE_AUTO_START,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
case '2':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,SERVICE_BOOT_START,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
case '3':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,SERVICE_DEMAND_START,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
case '4':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,SERVICE_DISABLED,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
case '5':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,SERVICE_SYSTEM_START,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
default:
cout<<"Wrong selection for modifying start-up."<<endl;
}
}
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
LocalFree(lqsc);
}
//recovery props
void recover_prop(void)
{
char ch;
SC_HANDLE scService,scManager;
LPQUERY_SERVICE_CONFIG lqsc;
DWORD dwBytesNeeded;
//открытие сервис_менеджера
scManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
//открытие сервиса c правами запроса состояние и запуска
scService = OpenService(scManager,_T("serv_cs06g_v17"),SERVICE_QUERY_CONFIG|SERVICE_CHANGE_CONFIG);
if (scService == NULL)
{
cout<<"\tOpenService Error!:["<<endl;
CloseServiceHandle(scManager);
return;
}
lqsc = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 4096);
if (lqsc == NULL)
{
cout<<"Error. Can nor allocate mem for buffer in heap."<<endl;
return;
}
if (!QueryServiceConfig(scService,lqsc,BUF_LEN,&dwBytesNeeded))
{
printf("QueryServiceConfig error.");
LocalFree(lqsc);
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
//start type
cout<<"\tCurrent ErrorControl property "<<endl;
switch(lqsc->dwErrorControl)
{
case SERVICE_ERROR_CRITICAL:
cout<<"SERVICE_ERROR_CRITICAL. The startup program logs the error, if possible. If the last-known-good configuration is being started, the startup operation fails. Otherwise, the system is restarted with the last-known good configuration."<<endl;
break;
case SERVICE_ERROR_IGNORE:
cout<<"SERVICE_ERROR_IGNORE. The startup program logs the error but continues the startup operation."<<endl;
break;
case SERVICE_ERROR_NORMAL:
cout<<"SERVICE_ERROR_NORMAL. The startup program logs the error and puts up a message box pop-up but continues the startup operation."<<endl;
break;
case SERVICE_ERROR_SEVERE:
cout<<"SERVICE_ERROR_SEVERE. The startup program logs the error. If the last-known-good configuration is being started, the startup operation continues. Otherwise, the system is restarted with the last-known-good configuration."<<endl;
break;
}
cout<<"If you want to modify ErrorControl params press 1 "<<endl;
cin>>ch;
if (ch == '1')
{
cout<<"\tErrorControl"<<endl<<"1. SERVICE_ERROR_CRITICAL"<<endl<<"2. SERVICE_ERROR_IGNORE"<<endl<<"3. SERVICE_ERROR_NORMAL"<<endl<<"4. SERVICE_ERROR_SEVERE"<<endl<<"Select ErrorControl(1,2,3,4), what to do after errors"<<endl;
cin>>ch;
switch (ch)
{
case '1':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,lqsc->dwStartType,SERVICE_ERROR_CRITICAL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
case '2':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,lqsc->dwStartType,SERVICE_ERROR_IGNORE,NULL,NULL,NULL,NULL,NULL,NULL,NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
case '3':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,lqsc->dwStartType,SERVICE_ERROR_NORMAL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
case '4':
if (!(ChangeServiceConfig(scService,SERVICE_NO_CHANGE,lqsc->dwStartType,SERVICE_ERROR_SEVERE,NULL,NULL,NULL,NULL,NULL,NULL,NULL)))
cout<<"ChangeServiceConfig Error"<<endl;
break;
default:
cout<<"Wrong selection for modifying ErrorControl."<<endl;
}
}
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
LocalFree(lqsc);
}
//show dependences
void show_depend(void)
{
SC_HANDLE scService,scManager;
LPQUERY_SERVICE_CONFIG lqsc;
DWORD dwBytesNeeded;
//открытие сервис_менеджера
scManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
//открытие сервиса c правами запроса состояние и запуска
scService = OpenService(scManager,_T("serv_cs06g_v17"),SERVICE_QUERY_CONFIG);
if (scService == NULL)
{
cout<<"\tOpenService Error!"<<endl;
CloseServiceHandle(scManager);
return;
}
lqsc = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 4096);
if (lqsc == NULL)
{
cout<<"Error. Can nor allocate mem for buffer in heap."<<endl;
return;
}
if (!QueryServiceConfig(scService,lqsc,BUF_LEN,&dwBytesNeeded))
{
printf("\tQueryServiceConfig error.");
LocalFree(lqsc);
CloseServiceHandle(scService);
CloseServiceHandle(scManager);
return;
}
if (sizeof(lqsc->lpDependencies) >1)
cout<<"Service Dependensies: NONE"<<endl;
else cout<<"Service Dependensies: "<<lqsc->lpDependencies<<endl;
//вывести службы от которых завист данная и службы, зависяшие от данной.
}
//main func
int _tmain(int argc, _TCHAR* argv[])
{
bool cond=true;
char ch;
while (cond)
{
menu();
ch=getch();
switch(ch)
{
case '1':
//регистрация сервиса
install_serv();
break;
case '2':
//удаление записей о сервисе из реестра remove_serv();
break;
case '3':
//запуск сервиса
start_serv();
break;
case '4':
//остановка сервиса
stop_serv();
break;
case '5':
//перезапуск сервиса
restart_serv();
break;
case '6':
//приостановка сервиса
pause_serv();
break;
case '7':
//продолжение работы после приостановки
cont_serv();
break;
case '8':
//данные о работе сервиса
status_serv();
break;
case '9':
//параметры запуска
logon_prop();
break;
case '0':
//работа в случае ошибки
recover_prop();
break;
case 'q':
//показать зависимости
show_depend();
break;
case 'w':
cond=false;
break;
default:
cout<<"err selection"<<endl;
}
}
getch();
return 0;
}