Пример #1
0
void enableService(const char* szName)
{
	gcWString wName(szName);
	SC_HANDLE scm, Service;
	
	//open connection to SCM
	scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);

	if (!scm)
		throw gcException(ERR_NULLSCMANAGER, GetLastError(), "Failed to open the Service Control Manager");

	//open service
	Service = OpenService(scm, wName.c_str(), SERVICE_CHANGE_CONFIG|SERVICE_QUERY_STATUS);
	if (!Service)
	{
		CloseServiceHandle(scm);
		throw gcException(ERR_NULLSERVICE, GetLastError(), gcString("Failed to open service: {0}", szName));
	}

	BOOL res = ChangeServiceConfig(Service, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, SERVICE_NO_CHANGE, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);

	CloseServiceHandle(scm);
	CloseServiceHandle(Service); 

	if (!res)
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to to renable service: {0}", szName)); 
}
Пример #2
0
	bool D3D11RenderWindow::create(unsigned int width, unsigned int height, const StringStringMap *miscParams)
	{
		// Compute window rectangle dimensions based on requested client area dimensions.
		_processParams(miscParams);
		RenderWindow::create(width, height);
		DWORD dwStyle = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU);

		RECT R = { 0, 0, mWidth, mHeight };
		AdjustWindowRect(&R, dwStyle, false);
		int widthCalc = R.right - R.left;
		int heightCalc = R.bottom - R.top;

		std::wstring wName(mTitle.begin(), mTitle.end());
		mhMainWnd = CreateWindow(L"D3D11WndClassName", wName.c_str(),
			dwStyle, CW_USEDEFAULT, CW_USEDEFAULT, widthCalc, heightCalc, 0, 0, mhAppInst, 0);

		if (!mhMainWnd)
		{
			return false;
		}

		_createSwapChain();
		//mForceRedraw = true;
		return true;
	}
Пример #3
0
void uninstallService(const char* szName)
{
	SC_HANDLE service;
	SC_HANDLE scm;
	BOOL SUCCESS;
	SERVICE_STATUS status;
	gcWString wName(szName);

	//Open connection to SCM
	scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE);
	if (!scm)
		throw gcException(ERR_NULLSCMANAGER, GetLastError(), gcString("Failed to open the Service Control Manager"));

	//Get service's handle
	service = OpenService(scm, wName.c_str(), SERVICE_ALL_ACCESS | DELETE);
	if (!service)
	{
		CloseServiceHandle(scm);
		throw gcException(ERR_NULLSERVICE, GetLastError(), gcString("Failed to open service: {0}", szName));
	}

	//Get service status
	SUCCESS	= QueryServiceStatus(service, &status);
	if (!SUCCESS)
	{
		CloseServiceHandle(service);
		CloseServiceHandle(scm);
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName));
	}
	
	//Stop service if necessary		
	if (status.dwCurrentState != SERVICE_STOPPED)
	{
		SUCCESS = ControlService(service, SERVICE_CONTROL_STOP, &status);

		if (!SUCCESS)
		{
			CloseServiceHandle(service);
			CloseServiceHandle(scm);
			throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to stop service: {0}", szName));
		}

		gcSleep(500);
	}

	//Delete service
	SUCCESS = DeleteService(service);

	//Clean up
	CloseServiceHandle(service);
	CloseServiceHandle(scm);

	if (!SUCCESS)
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to delete service: {0}", szName));
}
Пример #4
0
SERVICE_STATUS_E queryService(const char* szName)
{
	SC_HANDLE service;
	SC_HANDLE scm;
	BOOL SUCCESS;
	SERVICE_STATUS status;

	gcWString wName(szName);

	//Open connection to SCM
	scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);
	if (!scm)
		throw gcException(ERR_NULLSCMANAGER, GetLastError(), gcString("Failed to open the Service Control Manager"));

	//Get service's handle
	service = OpenService(scm, wName.c_str(), SERVICE_QUERY_STATUS);
	if (!service)
	{
		CloseServiceHandle(scm);
		return SERVICE_STATUS_NOTINSTALLED;
	}

	//Get service status
	SUCCESS	= QueryServiceStatus(service, &status);
	if (!SUCCESS)
	{
		CloseServiceHandle(service);
		CloseServiceHandle(scm);
		return SERVICE_STATUS_NOTINSTALLED;
	}

	switch (status.dwCurrentState)
	{
	case SERVICE_CONTINUE_PENDING: return SERVICE_STATUS_CONTINUE_PENDING;
	case SERVICE_PAUSE_PENDING	: return SERVICE_STATUS_PAUSE_PENDING;
	case SERVICE_PAUSED			: return SERVICE_STATUS_PAUSED;
	case SERVICE_START_PENDING	: return SERVICE_STATUS_START_PENDING;
	case SERVICE_RUNNING		: return SERVICE_STATUS_RUNNING;
	case SERVICE_STOP_PENDING	: return SERVICE_STATUS_STOP_PENDING;
	case SERVICE_STOPPED		: return SERVICE_STATUS_STOPPED;
	}

	return SERVICE_STATUS_UNKNOWN;
}
Пример #5
0
void installService(const char* szName, const char* szPath, const char* szDispName)
{
	gcAssert(szName);
	gcAssert(szPath);

	gcWString wName(szName);
	gcWString wPath(szPath);
	gcWString wDispName(szDispName?szDispName:szName);

	SC_HANDLE newService;
	SC_HANDLE scm; 

	//open connection to SCM
	scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE);

	if (!scm)
		throw gcException(ERR_NULLSCMANAGER, GetLastError(), "Failed to open the Service Control Manager");

	//install service
	newService = CreateService(
		scm,						//scm database
		wName.c_str(),					//service name
		wDispName.c_str(),				//display name
		GENERIC_READ|GENERIC_EXECUTE,	//access rights to the service
		SERVICE_WIN32_OWN_PROCESS,	//service type
		SERVICE_DEMAND_START,		//service start type
		SERVICE_ERROR_NORMAL,		//error control type
		wPath.c_str(),				//service path
		nullptr,						//no load ordering group 
		nullptr,						//no tag identifier
		nullptr,						//no dependencies	
		nullptr,						//LocalSystem account
		nullptr);						//no password

	if(!newService)
	{
		CloseServiceHandle(scm);
		throw gcException(ERR_NULLSERVICE, GetLastError(), gcString("Failed to create new service: {0}", szName));
	}

	//clean up
	CloseServiceHandle(newService);
	CloseServiceHandle(scm);
}
Пример #6
0
void stopService(const char* szName)
{
	gcAssert(szName);
	gcWString wName(szName);

	SC_HANDLE scm, Service;
	SERVICE_STATUS ssStatus; 
	
	//open connection to SCM
	scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);

	if (!scm)
		throw gcException(ERR_NULLSCMANAGER, GetLastError(), "Failed to open the Service Control Manager");

	//open service
	Service = OpenService(scm, wName.c_str(), SERVICE_STOP|SERVICE_QUERY_STATUS);
	if (!Service)
	{
		CloseServiceHandle(scm);
		throw gcException(ERR_NULLSERVICE, GetLastError(), gcString("Failed to open service: {0}", szName));
	}

	// Check the status until the service is no longer start pending. 
	if (!QueryServiceStatus( Service, &ssStatus) )
	{
		CloseServiceHandle(Service);
		CloseServiceHandle(scm);
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName));
	}

	if (ssStatus.dwCurrentState & (SERVICE_STOP_PENDING|SERVICE_STOPPED))
		return;

	BOOL SUCCES = ControlService(Service, SERVICE_CONTROL_STOP, &ssStatus);

	CloseServiceHandle(Service);
	CloseServiceHandle(scm);

	if (!SUCCES)
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to stop service: {0}", szName));
}
Пример #7
0
void changeServiceAccess(const char* szName)
{
	gcAssert(szName);

	SC_HANDLE scm = nullptr;
	SC_HANDLE Service = nullptr;

	BOOL                 bDaclPresent   = FALSE;
	BOOL                 bDaclDefaulted = FALSE;
	DWORD                dwSize         = 0;
	EXPLICIT_ACCESS      ea;
	PACL                 pacl           = nullptr;
	PACL                 pNewAcl        = nullptr;
	PSECURITY_DESCRIPTOR psd            = nullptr;
	SECURITY_DESCRIPTOR  sd;


	//open connection to SCM
	scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);

	if (!scm)
		throw gcException(ERR_NULLSCMANAGER, GetLastError(), "Failed to open the Service Control Manager");

	gcWString wName(szName);

	try
	{
		//open service
		Service = OpenService(scm, wName.c_str(), READ_CONTROL|WRITE_DAC);
		if (!Service)
			throw gcException(ERR_NULLSERVICE, GetLastError(), gcString("Failed to open service: {0}", szName));


		// Get the current security descriptor.
		if (!QueryServiceObjectSecurity(Service, DACL_SECURITY_INFORMATION, psd, 0, &dwSize))
		{
			if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
			{
				psd = (PSECURITY_DESCRIPTOR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);

				if (!psd)
					throw gcException(ERR_SERVICE, gcString("Failed heap allocation for service service: {0}", szName));

				//get securtty info
				if (!QueryServiceObjectSecurity(Service, DACL_SECURITY_INFORMATION, psd, dwSize, &dwSize))
					throw gcException(ERR_SERVICE, GetLastError(), gcString("QueryServiceObjectSecurity failed for service: {0}", szName));
			}
		}

		if (!psd)
			throw gcException(ERR_SERVICE, gcString("Failed heap allocation for service service: {0}", szName));

		// Get the DACL.
		if (!GetSecurityDescriptorDacl(psd, &bDaclPresent, &pacl, &bDaclDefaulted))		
			throw gcException(ERR_SERVICE, GetLastError(), gcString("GetSecurityDescriptorDacl failed for service: {0}", szName));


		DWORD SidSize;
		PSID TheSID;

		SidSize = SECURITY_MAX_SID_SIZE;
		// Allocate enough memory for the largest possible SID.
		if(!(TheSID = LocalAlloc(LMEM_FIXED, SidSize)))
		{    
			LocalFree(TheSID);
			throw gcException(ERR_SERVICE, GetLastError(), gcString("LocalAlloc failed for  service: {0}", szName));
		}

		if(!CreateWellKnownSid(WinWorldSid, nullptr, TheSID, &SidSize))
		{
			LocalFree(TheSID);
			throw gcException(ERR_SERVICE, GetLastError(), gcString("CreateWellKnownSid failed for  service: {0}", szName));
		}

		wchar_t everyone[255];
		wchar_t domain[255];
		DWORD eSize = 255;
		DWORD dSize = 255;
		SID_NAME_USE rSidNameUse;

		if (!LookupAccountSid(nullptr, TheSID, everyone, &eSize, domain, &dSize, &rSidNameUse))
		{
			LocalFree(TheSID);
			throw gcException(ERR_SERVICE, GetLastError(), gcString("LookupAccountSid failed for  service: {0}", szName));
		}

		LocalFree(TheSID);

		// Build the ACE.
		BuildExplicitAccessWithName(&ea, everyone, SERVICE_START|SERVICE_STOP|READ_CONTROL|SERVICE_QUERY_STATUS|PROCESS_QUERY_INFORMATION, SET_ACCESS, NO_INHERITANCE);

		if (SetEntriesInAcl(1, &ea, pacl, &pNewAcl) != ERROR_SUCCESS)
		{
			throw gcException(ERR_SERVICE, GetLastError(), gcString("SetEntriesInAcl failed for  service: {0}", szName));
		}

		// Initialize a NEW Security Descriptor.
		if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))			
			throw gcException(ERR_SERVICE, GetLastError(), gcString("InitializeSecurityDescriptor failed for  service: {0}", szName));

		// Set the new DACL in the Security Descriptor.
		if (!SetSecurityDescriptorDacl(&sd, TRUE, pNewAcl, FALSE))						
			throw gcException(ERR_SERVICE, GetLastError(), gcString("SetSecurityDescriptorDacl failed for  service: {0}", szName));

		// Set the new DACL for the service object.
		if (!SetServiceObjectSecurity(Service, DACL_SECURITY_INFORMATION, &sd))		
			throw gcException(ERR_SERVICE, GetLastError(), gcString("SetServiceObjectSecurity failed for  service: {0}", szName));

	}
	catch (gcException &e)
	{
		if (Service)
			CloseServiceHandle(Service);

		if (scm)
			CloseServiceHandle(scm);

		// Free buffers.
		LocalFree((HLOCAL)pNewAcl);
		HeapFree(GetProcessHeap(), 0, (LPVOID)psd);

		throw e;
	}

	CloseServiceHandle(scm);
	CloseServiceHandle(Service);

	// Free buffers.
	LocalFree((HLOCAL)pNewAcl);
	HeapFree(GetProcessHeap(), 0, (LPVOID)psd);
}
Пример #8
0
void startService(const char* szName, std::vector<std::string> &args)
{
	gcAssert(szName);
	gcWString wName(szName);

	SC_HANDLE scm, Service;
	SERVICE_STATUS ssStatus; 
	DWORD dwWaitTime;
	
	//open connection to SCM
	scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);

	if (!scm)
		throw gcException(ERR_NULLSCMANAGER, GetLastError(), "Failed to open the Service Control Manager");

	//open service
	Service = OpenService(scm, wName.c_str(), SERVICE_START|SERVICE_QUERY_STATUS);
	if (!Service)
	{
		CloseServiceHandle(scm);
		throw gcException(ERR_NULLSERVICE, GetLastError(), gcString("Failed to open service: {0}", szName));
	}

	// Check the status until the service is no longer start pending. 
	if (!QueryServiceStatus( Service, &ssStatus) )
	{
		CloseServiceHandle(Service);
		CloseServiceHandle(scm);
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName));
	}

	while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
	{
		gcSleep(1000);
	}

	if (ssStatus.dwCurrentState != SERVICE_STOPPED) 
	{
		CloseServiceHandle(Service);
		CloseServiceHandle(scm);
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service: Service {0} is not stopped [{1}]", szName, ssStatus.dwCurrentState));
	}

	BOOL res = doStartService(Service, szName, args);

	if (res == 0)
	{
		if (GetLastError() == 1058)
			throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service {0} as the service is disabled. Please use msconfig and renable service.", szName));

		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service: {0}", szName));
	}

	// Check the status until the service is no longer start pending. 
	if (!QueryServiceStatus( Service, &ssStatus) )
	{
		CloseServiceHandle(Service);
		CloseServiceHandle(scm);
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName));
	}
		
	DWORD totalTime = 0;

	while (ssStatus.dwCurrentState != SERVICE_RUNNING) 
	{ 
		if (ssStatus.dwCurrentState == SERVICE_STOPPED)
		{
			//start service
			BOOL res = doStartService(Service, szName, args);

			if (res == 0)
				throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service: {0}", szName));
		}

		dwWaitTime = 1000;
		totalTime += dwWaitTime;

		gcSleep( dwWaitTime );

		// Check the status again. 
		if (!QueryServiceStatus( Service, &ssStatus) )
		{
			CloseServiceHandle(Service);
			CloseServiceHandle(scm);
			throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName));
		}

		if (totalTime > 30000)
		{
			CloseServiceHandle(Service);
			CloseServiceHandle(scm);
			throw gcException(ERR_SERVICE, gcString("Service {0} Startup timed out after 30 seconds.", szName));
		}
	}
		
	// Check the status again. 
	if (!QueryServiceStatus( Service, &ssStatus) )
	{
		CloseServiceHandle(Service);
		CloseServiceHandle(scm);
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName));
	}

	CloseServiceHandle(scm);
	CloseServiceHandle(Service); 

	if (ssStatus.dwCurrentState != SERVICE_RUNNING) 
		throw gcException(ERR_SERVICE, gcString("Failed to start service: {0} [{1}]", szName, ssStatus.dwCurrentState));
}
Пример #9
0
static std::vector<std::string> listFiles(std::string path)
{
    std::vector<std::string> results;

#ifdef _WIN32
    if (path.back() != '/')
        path.append("/*");
    else
        path.append("*");

    //convert to wide chars for windows
    std::basic_string<TCHAR> wPath;
    wPath.assign(path.begin(), path.end());

    WIN32_FIND_DATA findData;
    HANDLE hFind = FindFirstFile(wPath.c_str(), &findData);
    if (hFind == INVALID_HANDLE_VALUE)
    {
        std::cout << "Failed to find file data, invalid file handle returned" << std::endl;
        return results;
    }

    do
    {
        if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) //not a directory
        {
            //convert from wide char
            std::basic_string<TCHAR> wName(findData.cFileName);
            std::string fileName;
            fileName.assign(wName.begin(), wName.end());
            results.push_back(fileName);
        }

    } while (FindNextFile(hFind, &findData) != 0);
    FindClose(hFind);

    return std::move(results);
#else
    if (path.back() != '/')
        path.append("/.");
    else
        path.append(".");

    struct dirent* dp;
    DIR* dir = opendir(path.c_str());

    if (dir)
    {
        while ((dp = readdir(dir)) != nullptr)
        {
            std::string workingPath(path);
            workingPath.append("/");
            workingPath.append((dp->d_name));

            struct stat buf;
            if (!stat(workingPath.c_str(), &buf))
            {
                if (!S_ISDIR(buf.st_mode))
                {
                    results.emplace_back(dp->d_name);
                }
            }
        }
        closedir(dir);
    }
    return std::move(results);
#endif //_WIN32
}