Пример #1
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;
	}
Пример #2
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 nsclient::nsclient_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 != 0) {
			int err = GetLastError();
			if (err != ERROR_MORE_DATA) {
				throw nsclient::nsclient_exception("Failed to enumerate service status: " + error::format::from_system(err));
			}
		}

		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 nsclient::nsclient_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 nsclient::nsclient_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;

			fetch_delayed(hService, info);
			fetch_triggers(hService, info);
			ret.push_back(info);
		}
		return ret;
	}