From a686849bb6fc8a8c80b8e4283f9db8487a3a0199 Mon Sep 17 00:00:00 2001 From: fufesou Date: Tue, 30 Apr 2024 08:21:34 +0800 Subject: [PATCH] fix: msi, delete service (#7867) * fix: msi, delete service Signed-off-by: fufesou * refact: msi, replace 1060 to ERROR_SERVICE_DOES_NOT_EXIST Signed-off-by: fufesou --------- Signed-off-by: fufesou --- res/msi/CustomActions/Common.h | 1 + res/msi/CustomActions/CustomActions.cpp | 29 +++++++++++++++++++++---- res/msi/CustomActions/ServiceUtils.cpp | 20 +++++++++-------- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/res/msi/CustomActions/Common.h b/res/msi/CustomActions/Common.h index e01262f00..5dcd529c0 100644 --- a/res/msi/CustomActions/Common.h +++ b/res/msi/CustomActions/Common.h @@ -5,6 +5,7 @@ bool AddFirewallRule(bool add, LPWSTR exeName, LPWSTR exeFile); +bool QueryServiceStatusExW(LPCWSTR serviceName, SERVICE_STATUS_PROCESS* status); bool IsServiceRunningW(LPCWSTR serviceName); bool MyCreateServiceW(LPCWSTR serviceName, LPCWSTR displayName, LPCWSTR binaryPath); bool MyDeleteServiceW(LPCWSTR serviceName); diff --git a/res/msi/CustomActions/CustomActions.cpp b/res/msi/CustomActions/CustomActions.cpp index c21035b8b..e600403ba 100644 --- a/res/msi/CustomActions/CustomActions.cpp +++ b/res/msi/CustomActions/CustomActions.cpp @@ -422,6 +422,8 @@ UINT __stdcall TryStopDeleteService(__in MSIHANDLE hInstall) LPWSTR pwzData = NULL; wchar_t szExeFile[500] = { 0 }; DWORD cchExeFile = sizeof(szExeFile) / sizeof(szExeFile[0]); + SERVICE_STATUS_PROCESS svcStatus; + DWORD lastErrorCode = 0; hr = WcaInitialize(hInstall, "TryStopDeleteService"); ExitOnFailure(hr, "Failed to initialize"); @@ -443,17 +445,36 @@ UINT __stdcall TryStopDeleteService(__in MSIHANDLE hInstall) break; } } - WcaLog(LOGMSG_STANDARD, "Service \"%ls\" is stopped", svcName); } else { - WcaLog(LOGMSG_STANDARD, "Failed to stop service: \"%ls\"", svcName); + WcaLog(LOGMSG_STANDARD, "Failed to stop service: \"%ls\", error: 0X%02X.", svcName, GetLastError()); + } + + if (IsServiceRunningW(svcName)) { + WcaLog(LOGMSG_STANDARD, "Service \"%ls\" is not stoped after 1000 ms.", svcName); + } + else { + WcaLog(LOGMSG_STANDARD, "Service \"%ls\" is stoped.", svcName); } if (MyDeleteServiceW(svcName)) { - WcaLog(LOGMSG_STANDARD, "Service \"%ls\" is deleted", svcName); + WcaLog(LOGMSG_STANDARD, "Service \"%ls\" deletion is completed without errors.", svcName); } else { - WcaLog(LOGMSG_STANDARD, "Failed to delete service: \"%ls\"", svcName); + WcaLog(LOGMSG_STANDARD, "Failed to delete service: \"%ls\", error: 0X%02X.", svcName, GetLastError()); + } + + if (QueryServiceStatusExW(svcName, &svcStatus)) { + WcaLog(LOGMSG_STANDARD, "Failed to delete service: \"%ls\", current status: %d.", svcName, svcStatus.dwCurrentState); + } + else { + lastErrorCode = GetLastError(); + if (lastErrorCode == ERROR_SERVICE_DOES_NOT_EXIST) { + WcaLog(LOGMSG_STANDARD, "Service \"%ls\" is deleted.", svcName); + } + else { + WcaLog(LOGMSG_STANDARD, "Failed to query service status: \"%ls\", error: 0X%02X.", svcName, lastErrorCode); + } } // It's really strange that we need sleep here. diff --git a/res/msi/CustomActions/ServiceUtils.cpp b/res/msi/CustomActions/ServiceUtils.cpp index 234f70371..27c5c2e7c 100644 --- a/res/msi/CustomActions/ServiceUtils.cpp +++ b/res/msi/CustomActions/ServiceUtils.cpp @@ -140,7 +140,7 @@ bool MyStopServiceW(LPCWSTR serviceName) return true; } -bool IsServiceRunningW(LPCWSTR serviceName) +bool QueryServiceStatusExW(LPCWSTR serviceName, SERVICE_STATUS_PROCESS* status) { SC_HANDLE hSCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT); if (hSCManager == NULL) { @@ -155,19 +155,21 @@ bool IsServiceRunningW(LPCWSTR serviceName) return false; } - SERVICE_STATUS_PROCESS serviceStatus; DWORD bytesNeeded; - if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, reinterpret_cast(&serviceStatus), sizeof(serviceStatus), &bytesNeeded)) { + BOOL success = QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, reinterpret_cast(&status), sizeof(status), &bytesNeeded); + if (!success) { WcaLog(LOGMSG_STANDARD, "Failed to query service: %ls", serviceName); - CloseServiceHandle(hService); - CloseServiceHandle(hSCManager); - return false; } - bool isRunning = (serviceStatus.dwCurrentState == SERVICE_RUNNING); - CloseServiceHandle(hService); CloseServiceHandle(hSCManager); - return isRunning; + return success; +} + +bool IsServiceRunningW(LPCWSTR serviceName) +{ + SERVICE_STATUS_PROCESS serviceStatus; + QueryServiceStatusExW(serviceName, &serviceStatus); + return (serviceStatus.dwCurrentState == SERVICE_RUNNING); }