|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionA Windows service is an EXE specially designed to communicate with the SCM (Service Control Manager) of Windows NT/2000. The Service Control Manager (SCM) maintains a database of installed services and driver services, and provides a unified and secure means of controlling them. SCM is started at system boot and it is a remote procedure call (RPC) server. As a developer to try a simple service, we can divide the program into four parts.
Firstly, let us take a look at the #include "Winsvc.h" //Header file for Services. main() { SERVICE_TABLE_ENTRY Table[]={{"Service1",ServiceMain},{NULL,NULL}}; StartServiceCtrlDispatcher(Table); } The only thing done by the The declaration of a typical void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
Now, let us analyze our The main steps of this function are:
For proceeding, we need two global variables here:
The dwServiceType = SERVICE_WIN32; dwCurrentState = SERVICE_START_PENDING; //Means Trying To Start(Initially)
In the beginning of the The API function Once we get till here, we now set Now, let us analyze our Service Control Handler function: The Service Control Handler function is used by the SCM to communicate to the Service program about a user action on the service, like a start, stop, pause or continue. It basically contains a Now Service Installer/ Uninstaller For installing a service, we need to make some entries in the system registry. Windows has some APIs to do these steps, instead of using the registry functions. They are Thank You Anish C.V. The Code Goes Here:#include "stdafx.h" #include "Windows.h" #include "Winsvc.h" #include "time.h" SERVICE_STATUS m_ServiceStatus; SERVICE_STATUS_HANDLE m_ServiceStatusHandle; BOOL bRunning=true; void WINAPI ServiceMain(DWORD argc, LPTSTR *argv); void WINAPI ServiceCtrlHandler(DWORD Opcode); BOOL InstallService(); BOOL DeleteService(); int main(int argc, char* argv[]) { if(argc>1) { if(strcmp(argv[1],"-i")==0) { if(InstallService()) printf("\n\nService Installed Sucessfully\n"); else printf("\n\nError Installing Service\n"); } if(strcmp(argv[1],"-d")==0) { if(DeleteService()) printf("\n\nService UnInstalled Sucessfully\n"); else printf("\n\nError UnInstalling Service\n"); } else { printf("\n\nUnknown Switch Usage\n\nFor Install use Srv1 -i\n\nFor UnInstall use Srv1 -d\n"); } } else { SERVICE_TABLE_ENTRY DispatchTable[]= {{"Service1",ServiceMain},{NULL,NULL}}; StartServiceCtrlDispatcher(DispatchTable); } return 0; } void WINAPI ServiceMain(DWORD argc, LPTSTR *argv) { DWORD status; DWORD specificError; m_ServiceStatus.dwServiceType = SERVICE_WIN32; m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING; m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; m_ServiceStatus.dwWin32ExitCode = 0; m_ServiceStatus.dwServiceSpecificExitCode = 0; m_ServiceStatus.dwCheckPoint = 0; m_ServiceStatus.dwWaitHint = 0; m_ServiceStatusHandle = RegisterServiceCtrlHandler("Service1", ServiceCtrlHandler); if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) { return; } m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; m_ServiceStatus.dwCheckPoint = 0; m_ServiceStatus.dwWaitHint = 0; if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus)) { } bRunning=true; while(bRunning) { Sleep(3000); //Place Your Code for processing here.... } return; } void WINAPI ServiceCtrlHandler(DWORD Opcode) { switch(Opcode) { case SERVICE_CONTROL_PAUSE: m_ServiceStatus.dwCurrentState = SERVICE_PAUSED; break; case SERVICE_CONTROL_CONTINUE: m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; break; case SERVICE_CONTROL_STOP: m_ServiceStatus.dwWin32ExitCode = 0; m_ServiceStatus.dwCurrentState = SERVICE_STOPPED; m_ServiceStatus.dwCheckPoint = 0; m_ServiceStatus.dwWaitHint = 0; SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus); bRunning=false; break; case SERVICE_CONTROL_INTERROGATE: break; } return; } BOOL InstallService() { char strDir[1024]; HANDLE schSCManager,schService; GetCurrentDirectory(1024,strDir); strcat(strDir,"\\Srv1.exe"); schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (schSCManager == NULL) return false; LPCTSTR lpszBinaryPathName=strDir; schService = CreateService(schSCManager,"Service1", "The Display Name Needed", // service name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICE_DEMAND_START, // start type SERVICE_ERROR_NORMAL, // error control type lpszBinaryPathName, // service's binary NULL, // no load ordering group NULL, // no tag identifier NULL, // no dependencies NULL, // LocalSystem account NULL); // no password if (schService == NULL) return false; CloseServiceHandle(schService); return true; } BOOL DeleteService() { HANDLE schSCManager; SC_HANDLE hService; schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (schSCManager == NULL) return false; hService=OpenService(schSCManager,"Service1",SERVICE_ALL_ACCESS); if (hService == NULL) return false; if(DeleteService(hService)==0) return false; if(CloseServiceHandle(hService)==0) return false; return true; }
|
||||||||||||||||||||||