michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include "../../../../toolkit/mozapps/update/common/pathhash.h" michael@0: michael@0: #pragma comment(lib, "advapi32.lib") michael@0: michael@0: typedef struct _stack_t { michael@0: struct _stack_t *next; michael@0: TCHAR text[MAX_PATH]; michael@0: } stack_t; michael@0: michael@0: int popstring(stack_t **stacktop, LPTSTR str, int len); michael@0: void pushstring(stack_t **stacktop, LPCTSTR str, int len); michael@0: michael@0: /** michael@0: * Determines if the specified service exists or not michael@0: * michael@0: * @param serviceName The name of the service to check michael@0: * @param exists Whether or not the service exists michael@0: * @return TRUE if there were no errors michael@0: */ michael@0: static BOOL michael@0: IsServiceInstalled(LPCWSTR serviceName, BOOL &exists) michael@0: { michael@0: exists = FALSE; michael@0: michael@0: // Get a handle to the local computer SCM database with full access rights. michael@0: SC_HANDLE serviceManager = OpenSCManager(NULL, NULL, michael@0: SC_MANAGER_ENUMERATE_SERVICE); michael@0: if (!serviceManager) { michael@0: return FALSE; michael@0: } michael@0: michael@0: SC_HANDLE serviceHandle = OpenServiceW(serviceManager, michael@0: serviceName, michael@0: SERVICE_QUERY_CONFIG); michael@0: if (!serviceHandle && GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST) { michael@0: CloseServiceHandle(serviceManager); michael@0: return FALSE; michael@0: } michael@0: michael@0: if (serviceHandle) { michael@0: CloseServiceHandle(serviceHandle); michael@0: exists = TRUE; michael@0: } michael@0: michael@0: CloseServiceHandle(serviceManager); michael@0: return TRUE; michael@0: } michael@0: michael@0: /** michael@0: * Determines if the specified service is installed or not michael@0: * michael@0: * @param stacktop A pointer to the top of the stack michael@0: * @param variables A pointer to the NSIS variables michael@0: * @return 0 if the service does not exist michael@0: * 1 if the service does exist michael@0: * -1 if there was an error. michael@0: */ michael@0: extern "C" void __declspec(dllexport) michael@0: IsInstalled(HWND hwndParent, int string_size, michael@0: TCHAR *variables, stack_t **stacktop, void *extra) michael@0: { michael@0: TCHAR tmp[MAX_PATH] = { L'\0' }; michael@0: WCHAR serviceName[MAX_PATH] = { '\0' }; michael@0: popstring(stacktop, tmp, MAX_PATH); michael@0: michael@0: #if !defined(UNICODE) michael@0: MultiByteToWideChar(CP_ACP, 0, tmp, -1, serviceName, MAX_PATH); michael@0: #else michael@0: wcscpy(serviceName, tmp); michael@0: #endif michael@0: michael@0: BOOL serviceInstalled; michael@0: if (!IsServiceInstalled(serviceName, serviceInstalled)) { michael@0: pushstring(stacktop, TEXT("-1"), 3); michael@0: } else { michael@0: pushstring(stacktop, serviceInstalled ? TEXT("1") : TEXT("0"), 2); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Stops the specified service. michael@0: * michael@0: * @param serviceName The name of the service to stop michael@0: * @return TRUE if the operation was successful michael@0: */ michael@0: static BOOL michael@0: StopService(LPCWSTR serviceName) michael@0: { michael@0: // Get a handle to the local computer SCM database with full access rights. michael@0: SC_HANDLE serviceManager = OpenSCManager(NULL, NULL, michael@0: SC_MANAGER_ENUMERATE_SERVICE); michael@0: if (!serviceManager) { michael@0: return FALSE; michael@0: } michael@0: michael@0: SC_HANDLE serviceHandle = OpenServiceW(serviceManager, michael@0: serviceName, michael@0: SERVICE_STOP); michael@0: if (!serviceHandle) { michael@0: CloseServiceHandle(serviceManager); michael@0: return FALSE; michael@0: } michael@0: michael@0: //Stop the service so it deletes faster and so the uninstaller michael@0: // can actually delete its EXE. michael@0: DWORD totalWaitTime = 0; michael@0: SERVICE_STATUS status; michael@0: static const int maxWaitTime = 1000 * 60; // Never wait more than a minute michael@0: BOOL stopped = FALSE; michael@0: if (ControlService(serviceHandle, SERVICE_CONTROL_STOP, &status)) { michael@0: do { michael@0: Sleep(status.dwWaitHint); michael@0: // + 10 milliseconds to make sure we always approach maxWaitTime michael@0: totalWaitTime += (status.dwWaitHint + 10); michael@0: if (status.dwCurrentState == SERVICE_STOPPED) { michael@0: stopped = true; michael@0: break; michael@0: } else if (totalWaitTime > maxWaitTime) { michael@0: break; michael@0: } michael@0: } while (QueryServiceStatus(serviceHandle, &status)); michael@0: } michael@0: michael@0: CloseServiceHandle(serviceHandle); michael@0: CloseServiceHandle(serviceManager); michael@0: return stopped; michael@0: } michael@0: michael@0: /** michael@0: * Stops the specified service michael@0: * michael@0: * @param stacktop A pointer to the top of the stack michael@0: * @param variables A pointer to the NSIS variables michael@0: * @return 1 if the service was stopped, 0 on error michael@0: */ michael@0: extern "C" void __declspec(dllexport) michael@0: Stop(HWND hwndParent, int string_size, michael@0: TCHAR *variables, stack_t **stacktop, void *extra) michael@0: { michael@0: TCHAR tmp[MAX_PATH] = { L'\0' }; michael@0: WCHAR serviceName[MAX_PATH] = { '\0' }; michael@0: michael@0: popstring(stacktop, tmp, MAX_PATH); michael@0: michael@0: #if !defined(UNICODE) michael@0: MultiByteToWideChar(CP_ACP, 0, tmp, -1, serviceName, MAX_PATH); michael@0: #else michael@0: wcscpy(serviceName, tmp); michael@0: #endif michael@0: michael@0: if (StopService(serviceName)) { michael@0: pushstring(stacktop, TEXT("1"), 2); michael@0: } else { michael@0: pushstring(stacktop, TEXT("0"), 2); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Determines a unique registry path from a file or directory path michael@0: * michael@0: * @param stacktop A pointer to the top of the stack michael@0: * @param variables A pointer to the NSIS variables michael@0: * @return The unique registry path or an empty string on error michael@0: */ michael@0: extern "C" void __declspec(dllexport) michael@0: PathToUniqueRegistryPath(HWND hwndParent, int string_size, michael@0: TCHAR *variables, stack_t **stacktop, michael@0: void *extra) michael@0: { michael@0: TCHAR tmp[MAX_PATH] = { L'\0' }; michael@0: WCHAR installBasePath[MAX_PATH] = { '\0' }; michael@0: popstring(stacktop, tmp, MAX_PATH); michael@0: michael@0: #if !defined(UNICODE) michael@0: MultiByteToWideChar(CP_ACP, 0, tmp, -1, installBasePath, MAX_PATH); michael@0: #else michael@0: wcscpy(installBasePath, tmp); michael@0: #endif michael@0: michael@0: WCHAR registryPath[MAX_PATH + 1] = { '\0' }; michael@0: if (CalculateRegistryPathFromFilePath(installBasePath, registryPath)) { michael@0: pushstring(stacktop, registryPath, wcslen(registryPath) + 1); michael@0: } else { michael@0: pushstring(stacktop, TEXT(""), 1); michael@0: } michael@0: } michael@0: michael@0: BOOL WINAPI michael@0: DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) michael@0: { michael@0: return TRUE; michael@0: } michael@0: michael@0: /** michael@0: * Removes an element from the top of the NSIS stack michael@0: * michael@0: * @param stacktop A pointer to the top of the stack michael@0: * @param str The string to pop to michael@0: * @param len The max length michael@0: * @return 0 on success michael@0: */ michael@0: int popstring(stack_t **stacktop, TCHAR *str, int len) michael@0: { michael@0: // Removes the element from the top of the stack and puts it in the buffer michael@0: stack_t *th; michael@0: if (!stacktop || !*stacktop) { michael@0: return 1; michael@0: } michael@0: michael@0: th = (*stacktop); michael@0: lstrcpyn(str,th->text, len); michael@0: *stacktop = th->next; michael@0: GlobalFree((HGLOBAL)th); michael@0: return 0; michael@0: } michael@0: michael@0: /** michael@0: * Adds an element to the top of the NSIS stack michael@0: * michael@0: * @param stacktop A pointer to the top of the stack michael@0: * @param str The string to push on the stack michael@0: * @param len The length of the string to push on the stack michael@0: * @return 0 on success michael@0: */ michael@0: void pushstring(stack_t **stacktop, const TCHAR *str, int len) michael@0: { michael@0: stack_t *th; michael@0: if (!stacktop) { michael@0: return; michael@0: } michael@0: michael@0: th = (stack_t*)GlobalAlloc(GPTR, sizeof(stack_t) + len); michael@0: lstrcpyn(th->text, str, len); michael@0: th->next = *stacktop; michael@0: *stacktop = th; michael@0: }