windows服务程序的安装和卸载函数
本文连接:https://www.cnblogs.com/jqdy/p/15079152.html
安装和卸载服务程序可以在命令行中使用 “sc install 服务名” 和 “sc uninstall 服务名”。
也可以将安装和卸载功能加入程序中去。
1 #include <Windows.h> 2 #include "WinApiFunc.h" // 引入 ApiPrinft 3 #include "definition.h" // 引入 SVCDESCRIPTION,即在服务管理中显示的简介内容 4 5 #define BUFFER_SIZE 1020 6 7 VOID SvcInstall(PCTSTR pszName) 8 { 9 SC_HANDLE schSCManager = NULL; 10 SC_HANDLE schService = NULL; 11 12 LPTSTR pszBuffer = NULL; // 各处使用的临时缓冲区 13 pszBuffer = new TCHAR[BUFFER_SIZE]; 14 15 do { 16 17 // Check directory length 18 19 if (0 == GetCurrentDirectory(BUFFER_SIZE - 100, pszBuffer)) // 要预留文件名的位置,100个应该足够 20 { 21 ApiPrintf(3, 22 TEXT("Path is too long, select a short directory to install. "), 23 GetWindowsErrorString(pszBuffer, BUFFER_SIZE, GetLastError()), TEXT("\n")); 25 break; 26 } 27 28 // Get a handle to the SCM database. 29 30 schSCManager = OpenSCManager( 31 NULL, // local computer 32 NULL, // ServicesActive database 33 SC_MANAGER_ALL_ACCESS); // full access rights 34 35 if (NULL == schSCManager) 36 { 37 ApiPrintf(3, 38 TEXT("OpenSCManager failed. "), 39 GetWindowsErrorString(pszBuffer, BUFFER_SIZE, GetLastError()), TEXT("\n")); 40 break; 41 } 42 43 // 取得文件当前的路径 44 45 if (GetModuleFileName(NULL, pszBuffer, BUFFER_SIZE) == BUFFER_SIZE) 46 { 47 ApiPrintf(1, TEXT("Path string is too long, copy install file to a shorter path.\n")); 48 break; 49 } 50 51 // Create the service 52 53 schService = CreateService( 54 schSCManager, // SCM database 55 pszName, // name of service 56 pszName, // service name to display 57 SERVICE_ALL_ACCESS, // desired access 58 SERVICE_WIN32_OWN_PROCESS, // service type 59 SERVICE_AUTO_START, // start type 60 SERVICE_ERROR_NORMAL, // error control type 61 pszBuffer, // path to service's binary 62 NULL, // no load ordering group 63 NULL, // no tag identifier 64 NULL, // no dependencies 65 NULL, // LocalSystem account 66 NULL); // no password 67 68 if (NULL == schService) 69 { 70 ApiPrintf(3, 71 TEXT("CreateService failed. "), 72 GetWindowsErrorString(pszBuffer, BUFFER_SIZE, GetLastError()), 73 TEXT("\n")); 74 break; 75 } 76 else 77 { 78 // 写入服务描述内容 79 TCHAR pszText[] = SVCDESCRIPTION; 80 SERVICE_DESCRIPTION description{ pszText }; 81 ChangeServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, &description); 82 83 ApiPrintf(1, TEXT("Service installed successfully.\n")); 84 } 85 } while (0); 86 87 if (schSCManager) 88 { 89 CloseServiceHandle(schSCManager); 90 schSCManager = NULL; 91 } 92 if (schService) 93 { 94 CloseServiceHandle(schService); 95 schService = NULL; 96 } 97 if (!pszBuffer) 98 { 99 delete[]pszBuffer; 100 pszBuffer = NULL; 101 } 102 } 103 104 VOID SvcUnInstall(PCTSTR pszServiceName) 105 { 106 SC_HANDLE schSCManager = NULL; 107 SC_HANDLE schService = NULL; 108 LPTSTR pszBuffer = NULL; 109 SERVICE_STATUS svcStatus; 110 111 pszBuffer = new TCHAR[BUFFER_SIZE]; 112 do { 113 // Open the local default service control manager database 114 schSCManager = OpenSCManager( 115 NULL, // local computer 116 NULL, // ServiceActive database 117 SC_MANAGER_ALL_ACCESS); // full access rights 118 119 if (schSCManager == NULL) 120 { 121 ApiPrintf(3, 122 TEXT("OpenSCManager failed. "), 123 GetWindowsErrorString(pszBuffer, BUFFER_SIZE, GetLastError()), 124 TEXT("\n")); 125 break; 126 } 127 128 // Open the service with delete, stop, and query status permissions 129 schService = OpenService( 130 schSCManager, 131 pszServiceName, 132 SERVICE_STOP | SERVICE_QUERY_STATUS | DELETE); 134 135 if (schService == NULL) 136 { 137 ApiPrintf(3, 138 TEXT("OpenService failed. "), 139 GetWindowsErrorString(pszBuffer, BUFFER_SIZE, GetLastError()), 140 TEXT("\n")); 141 break; 142 } 143 144 // Try to stop the service 145 if (ControlService(schService, SERVICE_CONTROL_STOP, &svcStatus)) 146 { 147 ApiPrintf(2, pszServiceName, TEXT(" is stopping.\n")); 148 149 Sleep(1000); 150 151 while (QueryServiceStatus(schService, &svcStatus)) 152 { 153 if (svcStatus.dwCurrentState == SERVICE_STOP_PENDING) 154 { 155 Sleep(1000); 156 } 157 else 158 { 159 break; 160 } 161 } 162 163 ApiPrintf(3, 164 pszServiceName, 165 (svcStatus.dwCurrentState == SERVICE_STOPPED ? TEXT(" is stopping.") : TEXT(" failed to stop.")), 166 TEXT("\n")); 167 } 168 169 // Now remove the service by calling DeleteService. 170 if (!DeleteService(schService)) 171 { 172 ApiPrintf(3, 173 TEXT("DeleteService failed. "), 174 GetWindowsErrorString(pszBuffer, BUFFER_SIZE, GetLastError()), 175 TEXT("\n")); 176 break; 177 } 178 179 ApiPrintf(2, pszServiceName, TEXT(" is uninstalled.\n")); 180 181 } while (0); 182 183 // Centralized cleanup for all allocated resources. 184 if (schSCManager) 185 { 186 CloseServiceHandle(schSCManager); 187 schSCManager = NULL; 188 } 189 if (schService) 190 { 191 CloseServiceHandle(schService); 192 schService = NULL; 193 } 194 if (!pszBuffer) 195 { 196 delete[] pszBuffer; 197 pszBuffer = NULL; 198 } 199 }