diff -r 73b88125830c -r b8d1455fddc0 testconns/statdesktop/desktop/testsource/dlltester/src/utils.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testconns/statdesktop/desktop/testsource/dlltester/src/utils.cpp Mon Oct 04 02:58:21 2010 +0300 @@ -0,0 +1,519 @@ +/* +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* +*/ + + + + +// CUtils.cpp : Handy bits and pieces for apps +// + +#include "stdafx.h" +#include +#include +#include "Utils.h" + +CUtils::CUtils() +: m_hMutex((HANDLE)0) +{ + // open the Service Control Manager + hSCM = OpenSCManager(NULL, // local machine + NULL, // ServicesActive database + SC_MANAGER_ALL_ACCESS); // full access +} + +CUtils::~CUtils() +{ + if (hSCM) + CloseServiceHandle(hSCM); + + if (m_hMutex) + CloseHandle(m_hMutex); +} + + +// attempts to create a mutex for this process. +// if it fails, another process of the same name is already running +bool CUtils::AlreadyRunning(LPCTSTR szAppName) +{ + m_hMutex = CreateMutex(NULL, FALSE, szAppName); + if ((!m_hMutex) || (WaitForSingleObject( m_hMutex, 0 ) == WAIT_TIMEOUT)) + { + return true; + } + + return false; +} + + +// invokes an application then waits for it to exit +bool CUtils::CallProcessAndWait(LPCTSTR szApplication, LPTSTR szCommandLine, LPCTSTR szDirectory, bool bRunMinimised) +{ + bool valid = false; + STARTUPINFO startInfo = {0}; + startInfo.cb = sizeof(STARTUPINFO); + retCode = -1; + + // run window minimised and not active + if (bRunMinimised) + { + startInfo.dwFlags = STARTF_USESHOWWINDOW; + startInfo.wShowWindow = SW_SHOWMINIMIZED | SW_SHOWMINNOACTIVE; + } + + PROCESS_INFORMATION procInfo = {0}; + + // event attributes for the child process + SECURITY_ATTRIBUTES eventAttr; + eventAttr.nLength = sizeof(eventAttr); + eventAttr.lpSecurityDescriptor = NULL; + eventAttr.bInheritHandle = TRUE; + + // NOTE: if passing a command line, include the application name in szCommandLine and pass NULL for szApplication + if (CreateProcess(szApplication, szCommandLine, NULL, NULL, FALSE, NULL, NULL, + szDirectory, &startInfo, &procInfo)) + { + if (WAIT_OBJECT_0 == WaitForSingleObject(procInfo.hProcess, INFINITE)) + { + GetExitCodeProcess(procInfo.hProcess, &retCode); + CloseHandle(procInfo.hThread); + CloseHandle(procInfo.hProcess); + valid = true; + } + } + else + _GetWindowsError(); + + return valid; +} + + +bool CUtils::CallProcess(LPCTSTR szApplication, LPTSTR szCommandLine, LPCTSTR szDirectory) +{ + bool valid = false; + STARTUPINFO startInfo = {0}; + startInfo.cb = sizeof(STARTUPINFO); + + PROCESS_INFORMATION procInfo = {0}; + + // event attributes for the child process + SECURITY_ATTRIBUTES eventAttr; + eventAttr.nLength = sizeof(eventAttr); + eventAttr.lpSecurityDescriptor = NULL; + eventAttr.bInheritHandle = TRUE; + + if (CreateProcess(szApplication, szCommandLine, NULL, NULL, FALSE, NULL, NULL, + szDirectory, &startInfo, &procInfo)) + { + CloseHandle(procInfo.hThread); + CloseHandle(procInfo.hProcess); + valid = true; + } + else + _GetWindowsError(); + + return valid; +} + +// requests an NT service to perform a particular request +bool CUtils::ServiceRequest(const char *szServiceName, const unsigned int iRequest) +{ + bool valid = false; + + if (hSCM) + { + switch(iRequest) + { + case REQ_START: + if (Validate(hSCM, szServiceName, SERVICE_INACTIVE)) + { + SC_HANDLE hService = OpenService(hSCM, szServiceName, SERVICE_ALL_ACCESS); + if (hService) + { + DealWithDependentServices(szServiceName, iRequest); + + if (StartService(hService, 0, NULL)) + valid = true; + + CloseServiceHandle(hService); + } + else + _GetWindowsError(); + } + else + { + valid = true; // already started + } + break; + case REQ_STOP: + if (Validate(hSCM, szServiceName, SERVICE_ACTIVE)) + { + SC_HANDLE hService = OpenService(hSCM, szServiceName, SERVICE_STOP); + if (hService) + { + DealWithDependentServices(szServiceName, iRequest); + + SERVICE_STATUS status; + if (ControlService(hService, SERVICE_CONTROL_STOP, &status)) + valid = true; + + CloseServiceHandle(hService); + } + else + _GetWindowsError(); + } + else + { + valid = true; // already stopped + } + break; + default: + break; + }; + + } + return valid; +} + + + +// recurses through a directory structure, performing an action on the files/directories within +unsigned int CUtils::RecurseDir(const char *szDirectory, const unsigned int iCommand) +{ + iFileCommand = iCommand; + iFileCount = 0; + + Recurse(szDirectory); + + return iFileCount; +} + + +// adds a backslash to a path if it doesn't already have one +void CUtils::AddSlash(char *szPath) +{ + if (*(szPath + strlen(szPath) - 1) != '\\') + strcat(szPath, "\\"); +} + + +bool CUtils::ListServices(DWORD dwState) +{ + return Validate(hSCM, NULL, dwState, true); +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +// +// Private Methods +// +//////////////////////////////////////////////////////////////////////////////////////////////// + +bool CUtils::Validate(SC_HANDLE hSCM, const char *szServiceName, DWORD dwState, bool bDisplayOnly) +{ + bool valid = false; + DWORD dwBytes = 0; + DWORD dwNumServices = 0; + DWORD dwResumeHandle = 0; + static ENUM_SERVICE_STATUS status[200]; + + // get list of services in particular state + if (EnumServicesStatus(hSCM, // handle + SERVICE_WIN32, // service + dwState, // state + (ENUM_SERVICE_STATUS *)status, // returned info + 200 * sizeof(ENUM_SERVICE_STATUS), // length of buffer + &dwBytes, // returned length + &dwNumServices, // number services returned + &dwResumeHandle)) // point to continue + { + if (bDisplayOnly) + { + printf("\n\nServices\n========\n\n"); + } + + for (DWORD i=0;i