C++ (Windows Service)服务编程 - 注册新服务
一个简单的windows服务注册例子,练手之作。如有不足之处请指出,O(∩_∩)O谢谢。
如有需要,可以直接沿用我的代码增加程序主逻辑
程序安装后在重启后会自启动,并Copy 到System32 目录。
希望可以认识多一点热爱编程的朋友
1 // ExampleService.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <Windows.h> 6 7 SERVICE_STATUS_HANDLE ssh=NULL; // 全局句柄,保存服务控制请求的句柄 8 SERVICE_STATUS ss = {0}; //保存服务信息的结构 9 void PrintError(wchar_t *err) //打印错误信息到控制台 10 { 11 printf("%s ErrorCode : %d\r\n",err,GetLastError()); 12 } 13 14 BOOL InstallService() //安装服务 15 { 16 wchar_t DirBuf[1024]={0},SysDir[1024]={0}; 17 GetCurrentDirectory(1024,DirBuf); 18 GetModuleFileName(NULL,DirBuf,sizeof(DirBuf)); 19 GetSystemDirectory(SysDir,sizeof(SysDir)); 20 wcscat_s(SysDir,L"\\ExampleService.exe"); 21 if(!CopyFile(DirBuf,SysDir,FALSE)) 22 { 23 PrintError(L"CopyFile Fail"); 24 return FALSE; 25 } 26 27 SC_HANDLE sch = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); 28 if(!sch) 29 { 30 PrintError(L"OpenSCManager Failed"); 31 return FALSE; 32 } 33 34 SC_HANDLE schNewSrv = CreateService(sch,L"ExampleService",L"SampleServiceApp",SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START, 35 SERVICE_ERROR_NORMAL,SysDir,NULL,NULL,NULL,NULL,NULL); 36 37 if(!schNewSrv) 38 { 39 PrintError(L"CreateService Failed"); 40 return FALSE; 41 } 42 43 SERVICE_DESCRIPTION sd; 44 sd.lpDescription = L"A Sample Service , Test Example"; 45 46 ChangeServiceConfig2(schNewSrv,SERVICE_CONFIG_DESCRIPTION,&sd); 47 CloseServiceHandle(schNewSrv); 48 CloseServiceHandle(sch); 49 50 printf("Install Service Success!"); 51 return TRUE; 52 } 53 54 BOOL UnInstallService() //卸载服务 55 { 56 SC_HANDLE scm = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); 57 if(!scm) 58 { 59 PrintError(L"OpenSCManager Failed"); 60 return FALSE; 61 } 62 63 SC_HANDLE scml = OpenService(scm,L"ExampleService",SC_MANAGER_ALL_ACCESS); 64 if(!scml) 65 { 66 PrintError(L"OpenService Failed"); 67 return FALSE; 68 } 69 SERVICE_STATUS ss; 70 if(!QueryServiceStatus(scml,&ss)) 71 { 72 PrintError(L"QueryServiceStatus Failed"); 73 return FALSE; 74 } 75 if(ss.dwCurrentState != SERVICE_STOPPED) 76 { 77 if(!ControlService(scml,SERVICE_CONTROL_STOP,&ss) && ss.dwCurrentState !=SERVICE_CONTROL_STOP) 78 { 79 PrintError(L"ControlService Stop Failed"); 80 return FALSE; 81 } 82 } 83 if(!DeleteService(scml)) 84 { 85 PrintError(L"DeleteService Failed"); 86 return FALSE; 87 } 88 printf("Delete Service Success!"); 89 return TRUE; 90 } 91 92 void WINAPI ServiceCtrlHandler(DWORD dwOpcode) //服务控制函数 93 { 94 switch(dwOpcode) 95 { 96 case SERVICE_CONTROL_STOP: 97 ss.dwCurrentState = SERVICE_STOPPED; 98 break; 99 case SERVICE_CONTROL_PAUSE: 100 ss.dwCurrentState = SERVICE_PAUSED; 101 break; 102 case SERVICE_CONTROL_CONTINUE: 103 ss.dwCurrentState = SERVICE_CONTINUE_PENDING; 104 break; 105 case SERVICE_CONTROL_INTERROGATE: 106 break; 107 case SERVICE_CONTROL_SHUTDOWN: 108 break; 109 default: 110 PrintError(L"bad service request"); 111 } 112 SetServiceStatus(ssh,&ss); 113 } 114 //服务入口函数 115 VOID WINAPI ServiceMain( 116 DWORD dwArgc, // number of arguments 117 LPTSTR *lpszArgv // array of arguments 118 ) 119 { 120 ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS; 121 ss.dwCurrentState = SERVICE_START_PENDING; 122 ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE; 123 ss.dwCheckPoint = 0; 124 ss.dwServiceSpecificExitCode = 0; 125 ss.dwWaitHint = 0; 126 ss.dwWin32ExitCode = 0; 127 128 ssh = RegisterServiceCtrlHandler(L"ExampleService",ServiceCtrlHandler); 129 130 if(!ssh) 131 { 132 PrintError(L"RegisterService Fail"); 133 return; 134 } 135 if(!SetServiceStatus(ssh,&ss)) 136 { 137 PrintError(L"SetServiceStatus 0x01 Fail"); 138 return; 139 } 140 141 ss.dwWin32ExitCode = S_OK; 142 ss.dwCheckPoint = 0; 143 ss.dwWaitHint = 0; 144 ss.dwCurrentState = SERVICE_RUNNING; 145 if(!SetServiceStatus(ssh,&ss)) 146 { 147 PrintError(L"SetServiceStatus 0x02 Fail"); 148 return; 149 } 150 //SC_HANDLE scm = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); 151 //SC_HANDLE scml = OpenService(scm,L"ExampleService",SC_MANAGER_ALL_ACCESS); 152 //StartService(scml,0,NULL); 153 //CloseServiceHandle(scml); 154 //CloseServiceHandle(scm); 155 156 while(1) 157 { 158 //do something, 在此处执行程序主逻辑 159 } 160 161 } 162 163 void usage() //打印帮助信息 164 { 165 printf("[[-i Install],[-r UnInstall]]"); 166 } 167 168 int _tmain(int argc, _TCHAR* argv[]) //入口函数 169 { 170 if(argc == 2) 171 { 172 //if arguments has 2 173 wchar_t buf[10]={0}; 174 wcscpy_s(buf,argv[1]); 175 if(0 == wcscmp(buf,L"-i")) 176 { 177 if(!InstallService()) 178 { 179 PrintError(L"Install Service Failed"); 180 return -1; 181 } 182 } 183 else if(0 == wcscmp(buf,L"-r")) 184 { 185 if(!UnInstallService()) 186 return -1; 187 else 188 return 0; 189 } 190 } 191 else if(argc > 2) 192 { 193 usage(); 194 return -1; 195 } 196 197 198 SERVICE_TABLE_ENTRY srvEntry[] = { 199 {L"ExampleService",ServiceMain}, 200 {NULL,NULL} 201 }; 202 StartServiceCtrlDispatcher(srvEntry); 203 return 0; 204 }
欢迎 C++/Python 的朋友一起交流学习, My QQ Number: 2303186535
-- 来自m星人的问候