📄 service.c
字号:
if (!_getcwd(cwdstr, sizeof(cwdstr))) strcpy (cwdstr, "."); strcat (szPath, TEXT(" -runservice \"")); strcat (szPath, cwdstr); strcat (szPath, "\""); for (i = 1; i < argc; i++) { /* We will add the given command line arguments to the command */ /* We are not interested in the install and remove options */ if ((stricmp("-install", argv[i]) != 0) && (stricmp("-installa", argv[i]) != 0) && (stricmp("-remove", argv[i]) != 0)) { strcat(szPath, TEXT(" ")); strcat(szPath, argv[i]); } } schSCManager = OpenSCManager(NULL, /* machine (NULL == local) */ NULL, /* database (NULL == default) */ SC_MANAGER_ALL_ACCESS); /* access required */ if (schSCManager) { schService = CreateService(schSCManager, /* SCManager database */ TEXT(pService->pServiceName), /* name of service */ TEXT(pService->pServiceDisplayName), /* name to display */ SERVICE_ALL_ACCESS, /* desired access */ SERVICE_WIN32_OWN_PROCESS, /* service type */ bAutoStart ? SERVICE_AUTO_START : SERVICE_DEMAND_START, /* start type */ SERVICE_ERROR_NORMAL, /* error control type */ szPath, /* service's binary */ NULL, /* no load ordering group */ NULL, /* no tag identifier */ TEXT(pService->pDependancies), /* dependencies */ NULL, /* LocalSystem account */ NULL); /* no password */ if (schService) { _tprintf(TEXT("%s installed.\n"), TEXT(pService->pServiceDisplayName)); CloseServiceHandle(schService); } else { _tprintf(TEXT("CreateService failed - %s\n"), GetLastErrorText(pService->szErr, 256)); } CloseServiceHandle(schSCManager); } else _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(pService->szErr,256)); } }}void CmdRemoveService(){ if (pService != NULL) { SC_HANDLE schService; SC_HANDLE schSCManager; schSCManager = OpenSCManager(NULL, /* machine (NULL == local) */ NULL, /* database (NULL == default) */ SC_MANAGER_ALL_ACCESS); /* access required */ if (schSCManager) { schService = OpenService(schSCManager, TEXT(pService->pServiceName), SERVICE_ALL_ACCESS); if (schService) { /* try to stop the service */ if (ControlService(schService, SERVICE_CONTROL_STOP, &pService->ServiceStatus)) { _tprintf(TEXT("Stopping %s."), TEXT(pService->pServiceDisplayName)); Sleep(1000); while (QueryServiceStatus(schService, &pService->ServiceStatus)) { if (pService->ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING) { _tprintf(TEXT(".")); Sleep( 1000 ); } else break; } if (pService->ServiceStatus.dwCurrentState == SERVICE_STOPPED) _tprintf(TEXT("\n%s stopped.\n"), TEXT(pService->pServiceDisplayName)); else _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(pService->pServiceDisplayName)); } /* now remove the service */ if(DeleteService(schService)) _tprintf(TEXT("%s removed.\n"), TEXT(pService->pServiceDisplayName)); else _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(pService->szErr,256)); CloseServiceHandle(schService); } else _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(pService->szErr,256)); CloseServiceHandle(schSCManager); } else _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(pService->szErr,256)); }}LPTSTR GetLastErrorText(LPTSTR lpszBuf, DWORD dwSize){ DWORD dwRet; LPTSTR lpszTemp = NULL; dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY, NULL, GetLastError(), LANG_NEUTRAL, (LPTSTR)&lpszTemp, 0, NULL); /* supplied buffer is not long enough */ if (!dwRet || ((long)dwSize < (long)dwRet + 14)) lpszBuf[0] = TEXT('\0'); else { lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); /* remove cr and newline character */ _stprintf(lpszBuf, TEXT("%s (0x%x)"), lpszTemp, GetLastError()); } if (lpszTemp) LocalFree((HLOCAL)lpszTemp); return(lpszBuf);}BOOL CheckServiceArguments(int argc, char *argv[]){ int i; /* Lets process the arguments */ for (i = 1; i < argc; i++) { if (stricmp("-install", argv[i]) == 0) { /* They want to install the service */ CmdInstallService(argc, argv, FALSE); /* We don't carry on, after we have installed the service */ return(FALSE); } else if (stricmp("-installa", argv[i]) == 0) { /* They want to install the service */ CmdInstallService(argc, argv, TRUE); /* We don't carry on, after we have installed the service */ return(FALSE); } else if (stricmp("-remove", argv[i]) == 0) { /* Here they want to remove it */ CmdRemoveService(); /* We don't carry on, after we have removed the service */ return(FALSE); } else if (stricmp ("-runservice", argv[i]) == 0) { /* We can carry on, if we reached here */ chdir(argv[i+1]); argv[i] = ""; argv[i+1] = ""; return(TRUE); } } bRunAsService = FALSE; return(TRUE);}BOOL SetupService(int argc, char *argv[], void *pHandle, LPTSTR pAppName, LPTSTR pServiceName, LPTSTR pServiceDisplayName, LPTSTR pDependancies){ BOOL bDeleteService = TRUE; BOOL bResult = FALSE; /* Save the handle for later use */ pAppHandle = pHandle; /* Create our service class */ Service_Create(pAppName, pServiceName, pServiceDisplayName, pDependancies, argc, argv); if (CheckServiceArguments(argc, argv)) { if (bRunAsService) { /* No need to set the console control handler, as the service manager handles all this for us */ Service_Initialize(); bDeleteService = FALSE; } else { /* Set the console control handler for exiting the program */ SetConsoleCtrlHandler((PHANDLER_ROUTINE)EventHandlerRoutine, TRUE); /* Now do the main work */ ServiceMain(argc, argv); } /* We have been successful initializing, so let the caller know */ bResult = TRUE; } if (bDeleteService) { /* Finished with the service now */ Service_Delete(); } return(bResult);}BOOL EventHandlerRoutine(DWORD dwCtrlType){ /* This routine dosn't seem to get called all the time, Why ??? */ switch (dwCtrlType) { case CTRL_C_EVENT: /* A CTRL+C signal was received, either from keyboard input or from a signal generated by the GenerateConsoleCtrlEvent function.*/ case CTRL_BREAK_EVENT: /* A CTRL+BREAK signal was received, either from keyboard input or from a signal generated by GenerateConsoleCtrlEvent.*/ case CTRL_CLOSE_EVENT: /* A signal that the system sends to all processes attached to a console when the user closes the console (either by choosing the Close command from the console window's System menu, or by choosing the End Task command from the Task List).*/ case CTRL_LOGOFF_EVENT: /* A signal that the system sends to all console processes when a user is logging off. This signal does not indicate which user is logging off, so no assumptions can be made.*/ case CTRL_SHUTDOWN_EVENT: /* A signal that the system sends to all console processes when the system */ /* We are basically shutting down, so call Service_Delete */ Service_Delete(); return(FALSE); break; default: /* we are not handling this one, so return FALSE */ return(FALSE); }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -