Monday, 9 March 2015

Synchrolock Service

This is the service handler.


#include <stdio.h> #include <windows.h> #include <winsvc.h> void WINAPI ServiceMain(DWORD argc, LPTSTR *argv); void WINAPI ServiceCtrlHandler(DWORD Opcode); SERVICE_STATUS MyServiceStatus; SERVICE_STATUS_HANDLE MyServiceStatusHandle; SERVICE_TABLE_ENTRY Table[]={ {"Service1",ServiceMain}, {NULL,NULL}}; BOOL srvcRunning = TRUE; BOOL handleMutexLock=FALSE; BOOL handleFileLock=FALSE; STARTUPINFO si = { sizeof(si) }; PROCESS_INFORMATION piMutexLock, piFileLock; HANDLE HandleMutex; HANDLE FileLock; // Change of Service state handled in switch case void WINAPI ServiceCtrlHandler(DWORD Opcode) { switch(Opcode) { case SERVICE_CONTROL_STOP: // Asked to stop service on request MyServiceStatus.dwWin32ExitCode = 0; MyServiceStatus.dwCurrentState = SERVICE_STOPPED; MyServiceStatus.dwCheckPoint = 0; MyServiceStatus.dwWaitHint = 0; SetServiceStatus (MyServiceStatusHandle,&MyServiceStatus); if (srvcRunning==TRUE){ // Kill the child processes! HandleMutex = OpenProcess(PROCESS_ALL_ACCESS, FALSE, piMutexLock.dwProcessId); TerminateProcess(HandleMutex, -1); // argghh!! FileLock = OpenProcess(PROCESS_ALL_ACCESS, FALSE, piFileLock.dwProcessId); TerminateProcess(FileLock, -1); // argghh!! } // end if srvcRunning=FALSE; break; case SERVICE_CONTROL_CONTINUE: // Handle Start and Continue states MyServiceStatus.dwCurrentState = SERVICE_RUNNING; break; case SERVICE_CONTROL_INTERROGATE: // Interrogate state break; } // end case return; } // end handler // Service Controller will enter here to check on status of service void WINAPI ServiceMain(DWORD argc, LPTSTR *argv) { DWORD status; DWORD specificError; MyServiceStatus.dwServiceType = SERVICE_WIN32; MyServiceStatus.dwCurrentState = SERVICE_START_PENDING; // Tell SCM service is starting up MyServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; MyServiceStatus.dwWin32ExitCode = 0; MyServiceStatus.dwServiceSpecificExitCode = 0; MyServiceStatus.dwCheckPoint = 0; MyServiceStatus.dwWaitHint = 0; MyServiceStatusHandle = RegisterServiceCtrlHandler("SynchroLock", ServiceCtrlHandler); if (MyServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) { return; } MyServiceStatus.dwCurrentState = SERVICE_RUNNING; MyServiceStatus.dwCheckPoint = 0; MyServiceStatus.dwWaitHint = 0; if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus)) { } srvcRunning=TRUE; while(srvcRunning) { if (!handleMutexLock) { // Attempt to start MutexLock handleMutexLock = CreateProcess(0, "C:\\Synchrolock\\MutexLock.exe", 0, 0, FALSE, CREATE_NO_WINDOW, 0, 0, &si, &piMutexLock); if (!handleMutexLock) { if(MessageBox(0,"Would you like to retry","MutexLock Failed.", MB_RETRYCANCEL)==IDCANCEL) { ServiceCtrlHandler(SERVICE_CONTROL_STOP); srvcRunning=FALSE; // Set SERVICE CONTROLLER to fail if user gives up trying else try again } } } if (!handleFileLock) { // Attempt to start FileLock process handleFileLock = CreateProcess(0, "C:\\Synchrolock\\FileLock.exe", 0, 0, FALSE, CREATE_NO_WINDOW, 0, 0, &si, &piFileLock); if (!handleFileLock) { if(MessageBox(0,"Would you like to retry","FileLock Failed", MB_RETRYCANCEL)==IDCANCEL) { ServiceCtrlHandler(SERVICE_CONTROL_STOP); srvcRunning=FALSE; // Set SERVICE CONTROLLER to fail if user gives up trying else try again } } // end if !handleFileLock } // End if else Sleep(3000); } // end while loop return; } void main() { StartServiceCtrlDispatcher(Table); return; }



No comments:

Post a Comment