Exemplo n.º 1
0
	service_info get_service_info(const std::string computer, const std::string service) {
		std::wstring comp = utf8::cvt<std::wstring>(computer);

		service_handle sc = OpenSCManager(comp.empty()?NULL:comp.c_str(),NULL,SC_MANAGER_ENUMERATE_SERVICE);
		if (!sc) 
			throw nscp_exception("Failed to open service manager: " + error::lookup::last_error());

		service_handle hService = OpenService(sc, utf8::cvt<std::wstring>(service).c_str(), SERVICE_QUERY_CONFIG|SERVICE_QUERY_STATUS );
		if (!hService)
			throw nscp_exception("Failed to open service: " + service);

		hlp::buffer<BYTE, SERVICE_STATUS_PROCESS*> ssp = queryServiceStatusEx(hService, service);

		service_info info(service, "TODO");
		info.pid = ssp.get()->dwProcessId;
		info.state = ssp.get()->dwCurrentState;
		info.type = ssp.get()->dwServiceType;

		DWORD bytesNeeded2 = 0;
		DWORD deErr = 0;
		if (QueryServiceConfig(hService, NULL, 0, &bytesNeeded2) || (deErr = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
			throw nscp_exception("Failed to open service " + info.name + ": " + error::lookup::last_error(deErr));
		hlp::buffer<BYTE> buf2(bytesNeeded2+10);

		if (!QueryServiceConfig(hService, reinterpret_cast<QUERY_SERVICE_CONFIG*>(buf2.get()), bytesNeeded2, &bytesNeeded2))
			throw nscp_exception("Failed to open service: " + info.name);
		QUERY_SERVICE_CONFIG *data2 = reinterpret_cast<QUERY_SERVICE_CONFIG*>(buf2.get());
		info.start_type = data2->dwStartType;
		info.binary_path = utf8::cvt<std::string>(data2->lpBinaryPathName);
		info.error_control = data2->dwErrorControl;
		return info;
	}
Exemplo n.º 2
0
		system_info::memory_usage getMemoryStatus() {
			system_info::memory_usage ret;
			if (FEGlobalMemoryStatusEx != NULL) {
				MEMORYSTATUSEX buffer;
				buffer.dwLength = sizeof(buffer);
				if (!FEGlobalMemoryStatusEx(&buffer))
					throw nscp_exception("GlobalMemoryStatusEx failed: " + error::lookup::last_error());
				ret.physical.total = buffer.ullTotalPhys;
				ret.physical.avail = buffer.ullAvailPhys;
				ret.virtual_memory.total = buffer.ullTotalVirtual;
				ret.virtual_memory.avail = buffer.ullAvailVirtual;
				ret.pagefile.total = buffer.ullTotalPageFile;
				ret.pagefile.avail = buffer.ullAvailPageFile;
				return ret;
			} else if (FEGlobalMemoryStatus != NULL) {
				MEMORYSTATUS buffer;
				buffer.dwLength = sizeof(buffer);
				if (!FEGlobalMemoryStatus(&buffer))
					throw nscp_exception("GlobalMemoryStatus failed: " + error::lookup::last_error());
				ret.physical.total = buffer.dwTotalPhys;
				ret.physical.avail = buffer.dwAvailPhys;
				ret.virtual_memory.total = buffer.dwTotalVirtual;
				ret.virtual_memory.avail = buffer.dwAvailVirtual;
				ret.pagefile.total = buffer.dwTotalPageFile;
				ret.pagefile.avail = buffer.dwAvailPageFile;
				return ret;
			} else {
				throw nscp_exception("Failed to check memory: No method found");
			}
		}
Exemplo n.º 3
0
		BOOL QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded) {
			if (pQueryServiceStatusEx == NULL) {
				HMODULE hMod = ::LoadLibrary(_TEXT("Advapi32.dll"));
				if (hMod == NULL)
					throw nscp_exception("Failed to load: Advapi32: " + error::lookup::last_error());
				pQueryServiceStatusEx = reinterpret_cast<tQueryServiceStatusEx>(GetProcAddress(hMod, "QueryServiceStatusEx"));
				if (pQueryServiceStatusEx == NULL)
					throw nscp_exception("Failed to load: QueryServiceStatusEx: " + error::lookup::last_error());
			}
			return pQueryServiceStatusEx(hService, InfoLevel, lpBuffer, cbBufSize, pcbBytesNeeded);
		}
Exemplo n.º 4
0
		BOOL EnumServicesStatusEx(SC_HANDLE hSCManager, SC_ENUM_TYPE InfoLevel, DWORD dwServiceType, DWORD dwServiceState, LPBYTE lpServices, DWORD cbBufSize, LPDWORD pcbBytesNeeded, LPDWORD lpServicesReturned, LPDWORD lpResumeHandle, LPCTSTR pszGroupName) {
			if (pEnumServicesStatusEx == NULL) {
				HMODULE hMod = ::LoadLibrary(_TEXT("Advapi32.dll"));
				if (hMod == NULL)
					throw nscp_exception("Failed to load: Advapi32: " + error::lookup::last_error());
				pEnumServicesStatusEx = reinterpret_cast<tEnumServicesStatusEx>(GetProcAddress(hMod, "EnumServicesStatusExW"));
				if (pEnumServicesStatusEx == NULL)
					throw nscp_exception("Failed to load: EnumServicesStatusEx: " + error::lookup::last_error());
			}
			return pEnumServicesStatusEx(hSCManager, InfoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pszGroupName);
		}
Exemplo n.º 5
0
	hlp::buffer<BYTE, SERVICE_STATUS_PROCESS*> queryServiceStatusEx(SC_HANDLE hService, std::string service) {
		DWORD bytesNeeded = 0;
		DWORD deErr = 0;

		if (windows::winapi::QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, NULL, 0, &bytesNeeded) || (deErr = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
			throw nscp_exception("Failed to query service: " + service + ": " + error::lookup::last_error(deErr));

		hlp::buffer<BYTE, SERVICE_STATUS_PROCESS*> buf(bytesNeeded+10);
		if (!windows::winapi::QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, buf, bytesNeeded, &bytesNeeded))
			throw nscp_exception("Failed to query service: " + service + ": " + error::lookup::last_error());
		return buf;
	}
Exemplo n.º 6
0
	hlp::buffer<BYTE, QUERY_SERVICE_CONFIG*> queryServiceConfig(SC_HANDLE hService, std::string service) {
		DWORD bytesNeeded = 0;
		DWORD deErr = 0;

		if (QueryServiceConfig(hService, NULL, 0, &bytesNeeded) || (deErr = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
			throw nscp_exception("Failed to query service: " + service + ": " + error::lookup::last_error(deErr));

		hlp::buffer<BYTE, QUERY_SERVICE_CONFIG*> buf(bytesNeeded+10);
		if (!QueryServiceConfig(hService, buf.get(), bytesNeeded, &bytesNeeded))
			throw nscp_exception("Failed to query service: " + service + ": " + error::lookup::last_error());
		return buf;
	}
Exemplo n.º 7
0
	std::list<service_info> enum_services(const std::string computer, DWORD dwServiceType, DWORD dwServiceState) {
		std::list<service_info> ret;
		std::wstring comp = utf8::cvt<std::wstring>(computer);

		service_handle sc = OpenSCManager(comp.empty()?NULL:comp.c_str(),NULL,SC_MANAGER_ENUMERATE_SERVICE);
		if (!sc) 
			throw nscp_exception("Failed to open service manager: " + error::lookup::last_error());

		DWORD bytesNeeded = 0;
		DWORD count = 0;
		DWORD handle = 0;
		BOOL bRet = windows::winapi::EnumServicesStatusEx(sc, SC_ENUM_PROCESS_INFO, dwServiceType, dwServiceState, NULL, 0, &bytesNeeded, &count, &handle, NULL);
		if (bRet || GetLastError() != ERROR_MORE_DATA)
			throw nscp_exception("Failed to enumerate services");

		hlp::buffer<BYTE, ENUM_SERVICE_STATUS_PROCESS*> buf(bytesNeeded+10);
		bRet = windows::winapi::EnumServicesStatusEx(sc, SC_ENUM_PROCESS_INFO, dwServiceType, dwServiceState, buf, bytesNeeded, &bytesNeeded, &count, &handle, NULL);
		if (!bRet) 
			throw nscp_exception("Failed to enumerate service: " + error::lookup::last_error());
		ENUM_SERVICE_STATUS_PROCESS *data = buf.get();
		for (DWORD i=0; i<count;++i) {
			service_info info(utf8::cvt<std::string>(data[i].lpServiceName), utf8::cvt<std::string>(data[i].lpDisplayName));
			info.pid = data[i].ServiceStatusProcess.dwProcessId;
			info.state = data[i].ServiceStatusProcess.dwCurrentState;
			info.type = data[i].ServiceStatusProcess.dwServiceType;

			service_handle hService = OpenService(sc, data[i].lpServiceName, SERVICE_QUERY_CONFIG);
			if (!hService)
				throw nscp_exception("Failed to open service: " + info.name);

			hlp::buffer<BYTE, QUERY_SERVICE_CONFIG*> qscData = queryServiceConfig(hService, info.name);
			info.start_type = qscData.get()->dwStartType;
			info.binary_path = utf8::cvt<std::string>(qscData.get()->lpBinaryPathName);
			info.error_control = qscData.get()->dwErrorControl;


			SERVICE_DELAYED_AUTO_START_INFO delayed;
// 			typedef struct _SERVICE_DELAYED_AUTO_START_INFO {
// 				BOOL fDelayedAutostart;
// 			} SERVICE_DELAYED_AUTO_START_INFO, *LPSERVICE_DELAYED_AUTO_START_INFO;
			
			DWORD size=sizeof(SERVICE_DELAYED_AUTO_START_INFO);
			if (windows::winapi::QueryServiceConfig2W(hService, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, reinterpret_cast<LPBYTE>(&delayed), size, &size) != 0) {
				info.delayed = delayed.fDelayedAutostart;
			}
			ret.push_back(info);
		}
		return ret;
	}
Exemplo n.º 8
0
		LONG NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength) {
			if (pNtQuerySystemInformation == NULL)
				pNtQuerySystemInformation = reinterpret_cast<tNtQuerySystemInformation>(GetProcAddress(LoadLibrary(_T("Ntdll")), "NtQuerySystemInformation"));
			if (pNtQuerySystemInformation == NULL)
				throw nscp_exception("Failed to load: NtQuerySystemInformation: " + error::lookup::last_error());
			return pNtQuerySystemInformation(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
		}
Exemplo n.º 9
0
	CComPtr<ITaskSettings> new_filter_obj::get_settings() {
		if (settings)
			return settings;
		if (!SUCCEEDED(get_def()->get_Settings(&settings)))
			throw nscp_exception("Failed to get ITaskSettings: " + error::com::get());
		return settings;
	}
Exemplo n.º 10
0
	value_type get_average(long time) const {
		value_type ret;
		if (time <= 60) {
			for (const_iterator cit = seconds.end()-time; cit != seconds.end(); ++cit) {
				ret.add(*cit);
			}
			ret.normalize(time);
			return ret;
		}
		time /= 60;
		if (time <= 60) {
			for (const_iterator cit = minutes.end()-time; cit != minutes.end(); ++cit) {
				ret.add(*cit);
			}
			ret.normalize(time);
			return ret;
		}
		time/=60;
		if (time >= 24)
			throw nscp_exception("Size larger than buffer");
		for (const_iterator cit = hours.end()-time; cit != hours.end(); ++cit) {
			ret.add(*cit);
		}
		ret.normalize(time);
		return ret;
	}
Exemplo n.º 11
0
	CComPtr<IRegistrationInfo> new_filter_obj::get_reginfo() {
		if (reginfo)
			return reginfo;
		if (!SUCCEEDED(get_def()->get_RegistrationInfo(&reginfo)))
			throw nscp_exception("Failed to get IRegistrationInfo: " + error::com::get());
		return reginfo;
	}
Exemplo n.º 12
0
	CComPtr<ITaskDefinition> new_filter_obj::get_def() {
		if (def)
			return def;
		if (!SUCCEEDED(task->get_Definition(&def)))
			throw nscp_exception("Failed to get ITaskDefinition: " + error::com::get());
		return def;
	}
Exemplo n.º 13
0
		LONG NtQueryInformationProcess(HANDLE ProcessHandle, DWORD ProcessInformationClass, PVOID ProcessInformation, DWORD ProcessInformationLength, PDWORD ReturnLength) {
			if (pNtQueryInformationProcess == NULL)
				pNtQueryInformationProcess = reinterpret_cast<tNtQueryInformationProcess>(GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess"));
			if (pNtQueryInformationProcess == NULL)
				throw nscp_exception("Failed to load NtQueryInformationProcess");
			return pNtQueryInformationProcess(ProcessHandle, ProcessInformationClass, ProcessInformation, ProcessInformationLength, ReturnLength);
		}
Exemplo n.º 14
0
		INT VDMEnumTaskWOWEx(DWORD dwProcessId, tTASKENUMPROCEX fp, LPARAM lparam) {
			if (pVDMEnumTaskWOWEx == NULL)
				pVDMEnumTaskWOWEx = reinterpret_cast<tVDMEnumTaskWOWEx>(GetProcAddress(GetModuleHandleA("VDMDBG"), "VDMEnumTaskWOWEx"));
			if (pVDMEnumTaskWOWEx == NULL)
				throw nscp_exception("Failed to load NtQueryInformationProcess");
			return pVDMEnumTaskWOWEx(dwProcessId, fp, lparam);
		}
Exemplo n.º 15
0
		DWORD GetProcessImageFileName(HANDLE hProcess, LPWSTR lpImageFileName, DWORD nSize) {
			if (pGetProcessImageFileName == NULL)
				pGetProcessImageFileName = reinterpret_cast<tGetProcessImageFileName>(GetProcAddress(GetModuleHandleA("PSAPI"), "GetProcessImageFileNameW"));
			if (pGetProcessImageFileName == NULL)
				throw nscp_exception("Failed to load GetProcessImageFileName: " + error::lookup::last_error());
			return pGetProcessImageFileName(hProcess, lpImageFileName, nSize);
		}
Exemplo n.º 16
0
	std::vector<system_info::pagefile_info> system_info::get_pagefile_info() {
		std::vector<system_info::pagefile_info> ret;
		hlp::buffer<BYTE,LPVOID> buffer(4096);
		DWORD retLen = 0;
		NTSTATUS status = windows::winapi::NtQuerySystemInformation(windows::winapi::SystemPageFileInformation, buffer, buffer.size(), &retLen);
		if (status == STATUS_INFO_LENGTH_MISMATCH) {
			buffer.resize(retLen+10);
			status = windows::winapi::NtQuerySystemInformation(windows::winapi::SystemPageFileInformation, buffer, buffer.size(), &retLen);
		}
		if (status != STATUS_SUCCESS)
			throw nscp_exception("Failed to get pagefile info");
		ULONG offset = 0;
		while (true) {
			windows::winapi::SYSTEM_PAGEFILE_INFORMATION *info = buffer.get_t<windows::winapi::SYSTEM_PAGEFILE_INFORMATION*>(offset);
			system_info::pagefile_info data(utf8::cvt<std::string>(std::wstring(info->PageFileName.Buffer)));
			data.peak_usage = info->PeakUsage;
			data.usage = info->TotalInUse;
			data.size = info->TotalSize;
			data.peak_usage*=8*1024;
			data.usage*=8*1024;
			data.size*=8*1024;
			ret.push_back(data);
			if (info->NextEntryOffset == 0)
				break;
			offset += info->NextEntryOffset;
		}
		return ret;
	}
Exemplo n.º 17
0
			bool inline fetch_data(object_type *object, data_type &data, const std::string &title) {
				raw_type tmp;
				HRESULT hr = (object->*fun_)(&tmp);
				if (traits::has_failed(hr)) {
					throw nscp_exception("Failed to fetch " + title + ": " + ::error::format::from_system(hr));
				} else {
					data = traits::convert(hr, tmp);
					traits::cleanup(tmp);
					return true;
				}
			}
Exemplo n.º 18
0
	DWORD parse_service_state(const std::string str) {
		DWORD ret = 0;
		BOOST_FOREACH(const std::string key, strEx::s::splitEx(str, std::string(","))) {
			if (key == "active")
				ret |= SERVICE_ACTIVE;
			else if (key == "inactive")
				ret |= SERVICE_INACTIVE;
			else if (key == "all")
				ret |= SERVICE_STATE_ALL;
			else
				throw nscp_exception("Invalid service type specified: " + key);
		}
		return ret;
	}
Exemplo n.º 19
0
	system_info::cpu_load system_info::get_cpu_load() {
		int cores = get_numberOfProcessorscores();
		init_old_buffer(g_CPUIdleTimeOld, cores);
		init_old_buffer(g_CPUTotalTimeOld, cores);
		init_old_buffer(g_CPUKernelTimeOld, cores);

		boost::scoped_array<winapi::SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> buffer(new winapi::SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[cores]);
		if (winapi::NtQuerySystemInformation(winapi::SystemProcessorPerformanceInformation, &buffer[0], sizeof(winapi::SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * cores, NULL) != 0) {
			throw nscp_exception("Whoops");
		}

		cpu_load result;
		result.cores = cores;
		result.core.resize(cores);
		result.total.idle = result.total.kernel = result.total.total = 0.0;

		for (int i=0; i < cores; i++) {
			unsigned long long CPUIdleTime = buffer[i].IdleTime.QuadPart;
			unsigned long long CPUKernelTime = buffer[i].KernelTime.QuadPart - buffer[i].IdleTime.QuadPart;
			unsigned long long CPUTotalTime = buffer[i].KernelTime.QuadPart + buffer[i].UserTime.QuadPart;

			unsigned long long CPUIdleTimeDiff = CPUIdleTime - g_CPUIdleTimeOld[i];
			unsigned long long CPUKernelTimeDiff = CPUKernelTime - g_CPUKernelTimeOld[i];
			unsigned long long CPUTotalTimeDiff = CPUTotalTime - g_CPUTotalTimeOld[i];

			if (CPUTotalTimeDiff != 0) {
				result.core[i].core = i;
				result.core[i].idle = static_cast<double>(((CPUIdleTimeDiff * 100) / CPUTotalTimeDiff));
				result.core[i].kernel = static_cast<double>(((CPUKernelTimeDiff * 100) / CPUTotalTimeDiff));
				result.core[i].total = 100.0 - result.core[i].idle;
				result.total.idle += result.core[i].idle;
				result.total.kernel += result.core[i].kernel;
				result.total.total += result.core[i].total;
			} else {
				result.core[i].idle = 0;
				result.core[i].kernel = 0;
				result.core[i].total = 0;
			}
			g_CPUTotalTimeOld[i] = CPUTotalTime;
			g_CPUIdleTimeOld[i] = CPUIdleTime;
			g_CPUKernelTimeOld[i] = CPUKernelTime;
		}
		result.total.idle /= result.cores;
		result.total.kernel /= result.cores;
		result.total.total /= result.cores;
		return result;
	}