bool Module::StopAndDisableServices() { _SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!_SCManager) { return false; } for (size_t i = 0; i != ProximityServicesCount; ++i) { PCWSTR serviceName = ProximityServicesNames[i]; LOG_COMMENT(L"Stopping and disabling service '%s'.", serviceName); SC_HANDLE serviceHandle = OpenService(_SCManager, serviceName, SC_MANAGER_ALL_ACCESS); if (!serviceHandle) { if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) { // Service doesn't exist on this platform. // Just skip it. LOG_COMMENT(L"Service doesn't exist. Skipping."); continue; } return false; } std::unique_ptr<QUERY_SERVICE_CONFIG> serviceConfig; if (!GetServiceConfig(serviceHandle, &serviceConfig)) { CloseServiceHandle(serviceHandle); return false; } if (!ChangeServiceStartType(serviceHandle, SERVICE_DISABLED)) { LOG_COMMENT(L"Failed to disable service."); CloseServiceHandle(serviceHandle); return false; } LOG_COMMENT(L"Disabled service."); if (!StopService(serviceHandle)) { CloseServiceHandle(serviceHandle); return false; } _ServiceStateCache[i].ServiceHandle = serviceHandle; _ServiceStateCache[i].StartType = serviceConfig->dwStartType; } return true; }
static VOID AddServiceNamesToStop(HWND hServiceListBox, LPWSTR lpServiceList) { LPQUERY_SERVICE_CONFIG lpServiceConfig; LPWSTR lpStr; lpStr = lpServiceList; /* Loop through all the services in the list */ while (TRUE) { /* Break when we hit the double null */ if (*lpStr == L'\0' && *(lpStr + 1) == L'\0') break; /* If this isn't our first time in the loop we'll have been left on a null char */ if (*lpStr == L'\0') lpStr++; /* Get the service's display name */ lpServiceConfig = GetServiceConfig(lpStr); if (lpServiceConfig) { /* Add the service to the listbox */ SendMessageW(hServiceListBox, LB_ADDSTRING, 0, (LPARAM)lpServiceConfig->lpDisplayName); HeapFree(GetProcessHeap(), 0, lpServiceConfig); } /* Move onto the next string */ while (*lpStr != L'\0') lpStr++; } }
VOID TV1_AddDependantsToTree(PSERVICEPROPSHEET pDlgInfo, HTREEITEM hParent, LPTSTR lpServiceName) { SC_HANDLE hSCManager; SC_HANDLE hService; LPQUERY_SERVICE_CONFIG lpServiceConfig; LPTSTR lpDependants; LPTSTR lpStr; LPTSTR lpNoDepends; BOOL bHasChildren; hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCManager) { hService = OpenService(hSCManager, lpServiceName, SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS | SERVICE_QUERY_CONFIG); if (hService) { /* Get a list of service dependents */ lpDependants = TV1_GetDependants(pDlgInfo, hService); if (lpDependants) { lpStr = lpDependants; /* Make sure this isn't the end of the list */ while (*lpStr) { /* Get the info for this service */ lpServiceConfig = GetServiceConfig(lpStr); if (lpServiceConfig) { /* Does this item need a +/- box? */ if (lpServiceConfig->lpDependencies && *lpServiceConfig->lpDependencies != '\0') { bHasChildren = TRUE; } else { bHasChildren = FALSE; } /* Add it */ AddItemToTreeView(pDlgInfo->hDependsTreeView1, hParent, lpServiceConfig->lpDisplayName, lpStr, lpServiceConfig->dwServiceType, bHasChildren); HeapFree(GetProcessHeap(), 0, lpServiceConfig); } /* Move to the end of the string */ while (*lpStr++) ; } HeapFree(GetProcessHeap(), 0, lpDependants); } else { /* If there is no parent, set the tree to 'no dependencies' */ if (!hParent) { /* Load the 'No dependencies' string */ AllocAndLoadString(&lpNoDepends, hInstance, IDS_NO_DEPENDS); AddItemToTreeView(pDlgInfo->hDependsTreeView1, NULL, lpNoDepends, NULL, 0, FALSE); LocalFree(lpNoDepends); /* Disable the window */ EnableWindow(pDlgInfo->hDependsTreeView1, FALSE); } } CloseServiceHandle(hService); } CloseServiceHandle(hSCManager); } }
VOID SetMenuAndButtonStates(PMAIN_WND_INFO Info) { HMENU hMainMenu; UINT i; /* get handle to menu */ hMainMenu = GetMenu(Info->hMainWnd); /* set all to greyed */ for (i = ID_START; i <= ID_RESTART; i++) { EnableMenuItem(hMainMenu, i, MF_GRAYED); EnableMenuItem(GetSubMenu(Info->hShortcutMenu, 0), i, MF_GRAYED); SendMessage(Info->hTool, TB_SETSTATE, i, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0)); } if (Info->SelectedItem != NO_ITEM_SELECTED) { LPQUERY_SERVICE_CONFIG lpServiceConfig = NULL; DWORD Flags, State; /* allow user to delete service */ if (Info->bIsUserAnAdmin) { SendMessage(Info->hTool, TB_SETSTATE, ID_DELETE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0)); EnableMenuItem(hMainMenu, ID_DELETE, MF_ENABLED); EnableMenuItem(GetSubMenu(Info->hShortcutMenu, 0), ID_DELETE, MF_ENABLED); } Flags = Info->pCurrentService->ServiceStatusProcess.dwControlsAccepted; State = Info->pCurrentService->ServiceStatusProcess.dwCurrentState; lpServiceConfig = GetServiceConfig(Info->pCurrentService->lpServiceName); if (lpServiceConfig && lpServiceConfig->dwStartType != SERVICE_DISABLED) { if (State == SERVICE_STOPPED) { EnableMenuItem(hMainMenu, ID_START, MF_ENABLED); EnableMenuItem(GetSubMenu(Info->hShortcutMenu, 0), ID_START, MF_ENABLED); SendMessage(Info->hTool, TB_SETSTATE, ID_START, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0)); } if ( (Flags & SERVICE_ACCEPT_STOP) && (State == SERVICE_RUNNING) ) { EnableMenuItem(hMainMenu, ID_RESTART, MF_ENABLED); EnableMenuItem(GetSubMenu(Info->hShortcutMenu, 0), ID_RESTART, MF_ENABLED); SendMessage(Info->hTool, TB_SETSTATE, ID_RESTART, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0)); } } if(lpServiceConfig) HeapFree(GetProcessHeap(), 0, lpServiceConfig); if ( (Flags & SERVICE_ACCEPT_STOP) && (State == SERVICE_RUNNING) ) { EnableMenuItem(hMainMenu, ID_STOP, MF_ENABLED); EnableMenuItem(GetSubMenu(Info->hShortcutMenu, 0), ID_STOP, MF_ENABLED); SendMessage(Info->hTool, TB_SETSTATE, ID_STOP, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0)); } if ( (Flags & SERVICE_ACCEPT_PAUSE_CONTINUE) && (State == SERVICE_RUNNING) ) { EnableMenuItem(hMainMenu, ID_PAUSE, MF_ENABLED); EnableMenuItem(GetSubMenu(Info->hShortcutMenu, 0), ID_PAUSE, MF_ENABLED); SendMessage(Info->hTool, TB_SETSTATE, ID_PAUSE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0)); } } else { /* disable tools which rely on a selected service */ EnableMenuItem(hMainMenu, ID_PROP, MF_GRAYED); EnableMenuItem(hMainMenu, ID_DELETE, MF_GRAYED); EnableMenuItem(GetSubMenu(Info->hShortcutMenu, 0), ID_PROP, MF_GRAYED); EnableMenuItem(GetSubMenu(Info->hShortcutMenu, 0), ID_DELETE, MF_GRAYED); SendMessage(Info->hTool, TB_SETSTATE, ID_PROP, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0)); SendMessage(Info->hTool, TB_SETSTATE, ID_DELETE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0)); } }