在Windows 系统中,服务是指执行指定系统功能的程序、例程或进程,以便支持其他程序,尤其是低层(接近硬件)程序。通过网络提供服务时,服务可以在Active Directory(活动目录)中发布,从而促进了以服务为中心的管理和使用。
服务是一种应用程序类型,它在后台运行。服务应用程序通常可以在本地和通过网络为用户提供一些功能,例如客户端/服务器应用程序、Web服务器、数据库服务器以及其他基于服务器的应用程序。
Windows系统服务作用
(1)启动、停止、暂停、恢复或禁用远程和本地计算机服务。
(2)管理本地和远程计算机上的服务。
(3)设置服务失败时的故障恢复操作。例如,重新自动启动服务或重新启动计算机。
(4)为特定的硬件配置文件启用或禁用服务。
(5)查看每个服务的状态和描述。
下面我们来实现利用Windows系统服务启动记事本,也可以像迅雷一样利用服务启动迅雷。
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <assert.h> #include <windows.h> VOID WINAPI ServiceHandler(DWORD fdwControl); void WINAPI ServiceMain(DWORD dwArgc,LPTSTR *lpArgv); void uninstallService(); void installService(); SERVICE_STATUS ServiceStu; SERVICE_STATUS_HANDLE registerSV; void OutputString( char *lpFmt, ... ); //宣称变量 char SourceFileName[255],telnetFileName[255],telnetCacheFileName[255]; HWND MessageBoxHandle; int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) { char error[256]=""; SERVICE_TABLE_ENTRY DispatchTable[2]; DispatchTable[0].lpServiceName="Print Device Synchronization"; DispatchTable[0].lpServiceProc=(LPSERVICE_MAIN_FUNCTION)ServiceMain; DispatchTable[1].lpServiceName=NULL; DispatchTable[1].lpServiceProc=NULL; if(!::StartServiceCtrlDispatcher(DispatchTable))//运行服务 { OutputString("tlnser WinMain enter running" ); } return 0; } void WINAPI ServiceMain(DWORD dwArgc,LPTSTR *lpArgv) { static DWORD checkPoint=1; ServiceStu.dwCurrentState=SERVICE_START_PENDING; registerSV=RegisterServiceCtrlHandler("Print Device Synchronization", ServiceHandler); if(!registerSV) printf("register service error.\n"); ServiceStu.dwServiceType=SERVICE_WIN32_OWN_PROCESS; ServiceStu.dwCurrentState=SERVICE_START_PENDING; ServiceStu.dwControlsAccepted=SERVICE_ACCEPT_STOP; ServiceStu.dwWin32ExitCode=NO_ERROR; ServiceStu.dwServiceSpecificExitCode=0; ServiceStu.dwWaitHint=300; ServiceStu.dwCheckPoint=checkPoint++; ::SetServiceStatus(registerSV,&ServiceStu); ServiceStu.dwCurrentState=SERVICE_RUNNING; ServiceStu.dwWaitHint=0; ServiceStu.dwCheckPoint=0; ::SetServiceStatus(registerSV,&ServiceStu); ////自己添代码 //具体执行的内容 OutputString("tlnser enter running" ); WinExec("C:\\windows\\notepad.exe",0); ////自己添加结束 ServiceStu.dwCurrentState=SERVICE_STOPPED; ::SetServiceStatus(registerSV,&ServiceStu); }; //卸载服务 void uninstallService() { SC_HANDLE scmanager=::OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS); SC_HANDLE hservice=::OpenService(scmanager,"Print Device Synchronization",SERVICE_STOP|DELETE); ::ControlService(hservice,SERVICE_CONTROL_STOP,&ServiceStu); ::DeleteService(hservice); CloseServiceHandle(hservice); CloseServiceHandle(scmanager); printf("uninstall service ok\n"); } //安装服务 void installService() { char path[MAX_PATH]=""; GetModuleFileName(NULL,path,MAX_PATH); //strcat(path," -x"); printf("%s install\n",path); SC_HANDLE scmanager=::OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS); SC_HANDLE hservice=::CreateService(scmanager, "Print Device Synchronization", "Print Device Synchronization", SC_MANAGER_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, path, NULL, NULL, NULL, NULL, NULL ); ::CloseServiceHandle(hservice); ::CloseServiceHandle(scmanager); }; VOID WINAPI ServiceHandler(DWORD fdwControl) { static DWORD checkPoint=1; switch(fdwControl) { case SERVICE_CONTROL_STOP: ServiceStu.dwCurrentState=SERVICE_START_PENDING; ServiceStu.dwControlsAccepted=0; ServiceStu.dwWin32ExitCode=NO_ERROR; ServiceStu.dwCheckPoint=checkPoint++; ServiceStu.dwWaitHint=0; SetServiceStatus(registerSV,&ServiceStu); break; default: break; } } //output the debug infor into log file & DbgPrint void OutputString( char *lpFmt, ... ) { char buff[1024]; va_list arglist; va_start( arglist, lpFmt ); _vsnprintf( buff, sizeof buff, lpFmt, arglist ); va_end( arglist ); DWORD len; HANDLE herr = GetStdHandle(STD_OUTPUT_HANDLE); if (herr != INVALID_HANDLE_VALUE) { WriteFile(herr, buff, strlen(buff), &len, NULL); WriteFile(herr, "\r\n", 2, &len, NULL); } else { FILE *fp = fopen("SvcHost.DLL.log", "a"); if (fp) { char date[20], time[20]; fprintf(fp, "%s %s - %s\n", _strdate(date), _strtime(time), buff); if (!stderr) fclose(fp); } } OutputDebugString(buff); }