win32 c++编写系统服务程序
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
首先先创建一个控制台程序,或者win32程序,都没关系。因为后台服务是看不到界面的。 服务程序应具有这样的功能: 一个服务主函数,注册服务控制句柄,启动要运行的代码。 二是服务控制函数,注册给系统,提供对服务管理程序SCM的响应,就是我的电脑-》管理里面的服务,不是有开始,停止,暂停这些功能么,这个函数就是对这个响应,停止的时候就做退出清理工作,关闭进程。 就两个函数而已。一般是ServiceMain 和ServiceCtrlHandler 这就是服务的全部必须代码了。 然后为什么别人的服务还有很多函数呢,什么 InstallService DeleteService之类的。 因为服务需要安装呀,一般一个程序写在一起,方便一点,但安装服务不是服务必须的,这必须理清楚,服务只需要一个主函数就行,安装卸载可以由其他的程序安装卸载。比如sc 或者直接操作注册表。 下面就是贴代码了 // GtServ.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Windows.h" #include "LogWriter.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(); //log类 CLogWriter log("D:\\log.TXT"); void DoTask() { } int _tmain(int argc, _TCHAR* argv[]) { if(argc>1) { if( wcscmp(argv[1],L"-i")==0) { if(InstallService()) printf("\n Service Installed Sucessfully\n"); else printf("\n Service has been installed\n"); } else if(wcscmp(argv[1],L"-d")==0) { if(DeleteService()) printf("\n Service UnInstalled Sucessfully\n"); else printf("\nInstalled Easin Central Service Not Found\n"); } else { printf("\nUnknown Switch Usage\nFor Install use Servicetest -i\nFor UnInstall use Servicetest -d\n"); } } else { SERVICE_TABLE_ENTRY DispatchTable[]={{L"GtServ",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(L"GtServ",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.... DoTask(); } 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() { WCHAR strDir[1024]; SC_HANDLE schSCManager,schService; GetCurrentDirectory(1024,strDir); lstrcat(strDir,L"\\GtServ.exe"); schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (schSCManager == NULL) return false; LPCTSTR lpszBinaryPathName=strDir; schService = CreateService((struct SC_HANDLE__ *)schSCManager,L"GtServ",L"GtServ", // service name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICE_AUTO_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; SERVICE_DESCRIPTION sdBuf; sdBuf.lpDescription = L"GT即时同步软件后台服务"; ChangeServiceConfig2( schService, SERVICE_CONFIG_DESCRIPTION, &sdBuf); CloseServiceHandle((struct SC_HANDLE__ *)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((struct SC_HANDLE__ *)schSCManager,L"GtServ",SERVICE_ALL_ACCESS); if (hService == NULL) return false; if(DeleteService(hService)==0) return false; if(CloseServiceHandle(hService)==0) return false; else return true; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=465