说是每学点知识就记录下,前段时间也没弄,还是自己懒惰了,今天逼着自己写写,当是在复习下,废话少说,进入正题。
windows的服务程序 需要SERVICE_TABLE_ENTRY这个结构体(描述服务名和对应启动的函数eg:ServiceMain),然后StartServiceCtrlDispatcher来调用前面的结构体,在ServiceMain函数里要注册服务状态控制的方法,不然就不能控制服务的初始化、启动等,这个是基本的服务程序
主要代码如下(只有服务程序,下面会有解说安装、卸载等):
1 //定义全局的服务状态 2 static SERVICE_STATUS ServerStatus = {0}; 3 //ServiceName就是服务名字,ServiceMain就是服务名对应的执行方法,有什么要执行的就加入到这个函数里面 4 int main(int argc, char *argv[]){ 5 SERVICE_TABLE_ENTRY ServerEntry[] = {{(LPSTR)ServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain }, {NULL, NULL}}; 6 StartServiceCtrlDispatcherA(ServerEntry); 7 } 8 9 void WINAPI ServiceMain() 10 { 11 //初始化服务配置 12 ServerStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; 13 ServerStatus.dwCurrentState = SERVICE_RUNNING; 14 ServerStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; 15 ServerStatus.dwWin32ExitCode = S_OK; 16 ServerStatus.dwServiceSpecificExitCode = 0; 17 ServerStatus.dwCheckPoint = 0; 18 ServerStatus.dwWaitHint = 0; 19 //注册控制 20 ServiceStatusHwnd = RegisterServiceCtrlHandler(ServiceName, ServiceStrl); 21 22 //默认设置服务为启动状态SERVICE_RUNNING 23 SetServiceStatus(ServiceStatusHwnd, &ServerStatus); 24 25 //在这执行自己的代码,如果要保持服务要一直运行,记得在下面阻塞不让执行下面的停止命令,如果不加下面的服务停止,也不加阻塞,服务就报异常的 26 27 28 ServerStatus.dwCurrentState = SERVICE_STOPPED; 29 SetServiceStatus (ServiceStatusHwnd, &ServerStatus); 30 } 31 32 //控制服务 33 void WINAPI ServiceStrl(DWORD dwOpcode) 34 { 35 switch (dwOpcode) 36 { 37 case SERVICE_CONTROL_STOP: 38 break; 39 case SERVICE_CONTROL_SHUTDOWN: 40 break; 41 } 42 }
差不多就这个一个服务程序就好了,下面就是安装服务等:
1 //安装服务封装了个简单的方法,传入服务程序的路劲即可 2 BOOLEAN Install(PCHAR szFilePath) 3 { 4 //打开服务控制管理器 5 SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); 6 if (hSCM == NULL) 7 { 8 LogEvent(TEXT("Couldn\'t open service manager")); 9 return FALSE; 10 } 11 12 //创建服务 13 SC_HANDLE hService = CreateService( 14 hSCM, ServiceName, ServiceName, 15 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, 16 SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, 17 szFilePath, NULL, NULL, TEXT(""), NULL, NULL); 18 19 if (hService == NULL) 20 { 21 CloseServiceHandle(hSCM); 22 LogEvent(TEXT("Couldn\'t create service")); 23 return FALSE; 24 } 25 26 //改变服务描述 27 SERVICE_DESCRIPTION Description; 28 TCHAR szDescription[1024]; 29 ZeroMemory(szDescription, 1024); 30 ZeroMemory(&Description,sizeof(SERVICE_DESCRIPTION)); 31 lstrcpy(szDescription, TEXT("服务端控制程序描述.")); 32 Description.lpDescription = szDescription; 33 ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &Description); 34 35 //开始服务 36 StartService(hService,0,NULL); 37 38 CloseServiceHandle(hService); 39 CloseServiceHandle(hSCM); 40 return TRUE; 41 } 42 //服务的卸载(先停止服务,在删除服务) 43 BOOLEAN Uninstall() 44 { 45 SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); 46 47 if (hSCM == NULL) 48 { 49 LogEvent(TEXT("Couldn\'t open service manager")); 50 return FALSE; 51 } 52 53 SC_HANDLE hService = OpenService(hSCM, ServiceName, SERVICE_STOP | DELETE); 54 55 if (hService == NULL) 56 { 57 CloseServiceHandle(hSCM); 58 LogEvent(TEXT("Couldn\'t open service")); 59 return FALSE; 60 } 61 SERVICE_STATUS status; 62 ControlService(hService, SERVICE_CONTROL_STOP, &status); 63 64 //删除服务 65 BOOLEAN bDelete = DeleteService(hService); 66 CloseServiceHandle(hService); 67 CloseServiceHandle(hSCM); 68 69 if (bDelete) 70 { 71 return TRUE; 72 } 73 74 LogEvent(TEXT("Service could not be deleted")); 75 return FALSE; 76 }