1. 程式人生 > 其它 >windows服務程式的安裝和解除安裝函式

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 LPTSTR pszBuffer = NULL; // 各處使用的臨時緩衝區 12 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()), 24 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()), 40 TEXT("\n")); 41 break; 42 } 43 44 // 取得檔案當前的路徑 45 46 if (GetModuleFileName(NULL, pszBuffer, BUFFER_SIZE) == BUFFER_SIZE) 47 { 48 ApiPrintf(1, TEXT("Path string is too long, copy install file to a shorter path.\n")); 49 break; 50 } 51 52 // Create the service 53 54 schService = CreateService( 55 schSCManager, // SCM database 56 pszName, // name of service 57 pszName, // service name to display 58 SERVICE_ALL_ACCESS, // desired access 59 SERVICE_WIN32_OWN_PROCESS, // service type 60 SERVICE_AUTO_START, // start type 61 SERVICE_ERROR_NORMAL, // error control type 62 pszBuffer, // path to service's binary 63 NULL, // no load ordering group 64 NULL, // no tag identifier 65 NULL, // no dependencies 66 NULL, // LocalSystem account 67 NULL); // no password 68 69 if (NULL == schService) 70 { 71 ApiPrintf(3, 72 TEXT("CreateService failed. "), 73 GetWindowsErrorString(pszBuffer, BUFFER_SIZE, GetLastError()), 74 TEXT("\n")); 75 break; 76 } 77 else 78 { 79 // 寫入服務描述內容 80 TCHAR pszText[] = SVCDESCRIPTION; 81 SERVICE_DESCRIPTION description{ pszText }; 82 ChangeServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, &description); 83 84 ApiPrintf(1, TEXT("Service installed successfully.\n")); 85 } 86 } while (0); 87 88 if (schSCManager) 89 { 90 CloseServiceHandle(schSCManager); 91 schSCManager = NULL; 92 } 93 if (schService) 94 { 95 CloseServiceHandle(schService); 96 schService = NULL; 97 } 98 if (!pszBuffer) 99 { 100 delete[]pszBuffer; 101 pszBuffer = NULL; 102 } 103 } 104 105 VOID SvcUnInstall(PCTSTR pszServiceName) 106 { 107 SC_HANDLE schSCManager = NULL; 108 SC_HANDLE schService = NULL; 109 LPTSTR pszBuffer = NULL; 110 SERVICE_STATUS svcStatus; 111 112 pszBuffer = new TCHAR[BUFFER_SIZE]; 113 do { 114 // Open the local default service control manager database 115 schSCManager = OpenSCManager( 116 NULL, // local computer 117 NULL, // ServiceActive database 118 SC_MANAGER_ALL_ACCESS); // full access rights 119 120 if (schSCManager == NULL) 121 { 122 ApiPrintf(3, 123 TEXT("OpenSCManager failed. "), 124 GetWindowsErrorString(pszBuffer, BUFFER_SIZE, GetLastError()), 125 TEXT("\n")); 126 break; 127 } 128 129 // Open the service with delete, stop, and query status permissions 130 schService = OpenService( 131 schSCManager, 132 pszServiceName, 133 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 }