fix: msi, delete service (#7867)

* fix: msi, delete service

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* refact: msi, replace 1060 to ERROR_SERVICE_DOES_NOT_EXIST

Signed-off-by: fufesou <shuanglongchen@yeah.net>

---------

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2024-04-30 08:21:34 +08:00 committed by GitHub
parent 54d041701c
commit a686849bb6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 13 deletions

View File

@ -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);

View File

@ -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.

View File

@ -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<LPBYTE>(&serviceStatus), sizeof(serviceStatus), &bytesNeeded)) {
BOOL success = QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, reinterpret_cast<LPBYTE>(&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);
}