/** Remove the windows service. This requires administrator privileges. */ void UninstallService() { SC_HANDLE InspServiceHandle = 0, SCMHandle = 0; try { SCMHandle = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, DELETE); if (!SCMHandle) throw CWin32Exception(); InspServiceHandle = OpenService(SCMHandle, TEXT("InspIRCd"), DELETE); if (!InspServiceHandle) throw CWin32Exception(); if (!DeleteService(InspServiceHandle) && GetLastError() != ERROR_SERVICE_MARKED_FOR_DELETE) { throw CWin32Exception(); } CloseServiceHandle(InspServiceHandle); CloseServiceHandle(SCMHandle); std::cout << "Service removed." << std::endl; } catch(CWin32Exception e) { if(InspServiceHandle) CloseServiceHandle(InspServiceHandle); if(SCMHandle) CloseServiceHandle(SCMHandle); std::cout << "Service deletion failed: " << e.what() << std::endl; } }
void CEvent::Set() { if ( !::SetEvent( m_hEvent ) ) { throw CWin32Exception( _T("CEvent::Set()"), ::GetLastError() ); } }
/** Install the windows service. This requires administrator privileges. */ void InstallService() { SC_HANDLE InspServiceHandle = 0, SCMHandle = 0; try { TCHAR tszBinaryPath[MAX_PATH]; if(!GetModuleFileName(NULL, tszBinaryPath, _countof(tszBinaryPath))) { throw CWin32Exception(); } SCMHandle = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS); if (!SCMHandle) { throw CWin32Exception(); } InspServiceHandle = CreateService(SCMHandle, TEXT("InspIRCd"),TEXT("InspIRCd Daemon"), SERVICE_CHANGE_CONFIG, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, tszBinaryPath, 0, 0, 0, TEXT("NT AUTHORITY\\NetworkService"), NULL); if (!InspServiceHandle) { throw CWin32Exception(); } TCHAR tszDescription[] = TEXT("The InspIRCd service hosts IRC channels and conversations. If this service is stopped, the IRC server will be unavailable."); SERVICE_DESCRIPTION svDescription = { tszDescription }; if(!ChangeServiceConfig2(InspServiceHandle, SERVICE_CONFIG_DESCRIPTION, &svDescription)) { throw CWin32Exception(); } CloseServiceHandle(InspServiceHandle); CloseServiceHandle(SCMHandle); std::cout << "Service installed." << std::endl; } catch(CWin32Exception e) { if(InspServiceHandle) CloseServiceHandle(InspServiceHandle); if(SCMHandle) CloseServiceHandle(SCMHandle); std::cout << "Service installation failed: " << e.what() << std::endl; } }
CIOCompletionPort::CIOCompletionPort( size_t maxConcurrency ) : m_iocp( ::CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, maxConcurrency ) ) { if ( 0 == m_iocp ) { throw CWin32Exception( _T("CIOCompletionPort::CIOCompletionPort() - CreateIoCompletionPort"), ::GetLastError() ); } }
CUsesWinsock::CUsesWinsock() { WORD wVersionRequested = 0x202; if ( 0 != ::WSAStartup( wVersionRequested, &m_data ) ) { throw CWin32Exception( _T("CUsesWinsock::CUsesWinsock()"), ::WSAGetLastError() ); } }
void CIOCompletionPort::AssociateDevice( HANDLE hDevice, ULONG_PTR completionKey) { if ( m_iocp != ::CreateIoCompletionPort( hDevice, m_iocp, completionKey, 0 ) ) { throw CWin32Exception(_T("CIOCompletionPort::AssociateDevice() - CreateIoCompletionPort"), ::GetLastError()); } }
void CIOCompletionPort::GetStatus( ULONG_PTR *pCompletionKey, PDWORD pdwNumBytes, OVERLAPPED **ppOverlapped) { if ( 0 == ::GetQueuedCompletionStatus( m_iocp, pdwNumBytes, pCompletionKey, ppOverlapped, INFINITE ) ) { throw CWin32Exception( _T("CIOCompletionPort::GetStatus() - GetQueuedCompletionStatus"), ::GetLastError() ); } }
void CIOCompletionPort::PostStatus( ULONG_PTR completionKey, DWORD dwNumBytes /* = 0 */, OVERLAPPED *pOverlapped /* = 0 */) { if ( 0 == ::PostQueuedCompletionStatus( m_iocp, dwNumBytes, completionKey, pOverlapped ) ) { throw CWin32Exception( _T("CIOCompletionPort::PostStatus() - PostQueuedCompletionStatus"), ::GetLastError() ); } }
static HANDLE Create( LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, BOOL bNeedOnlyOne, LPCTSTR lpName) { HANDLE hMutex = ::CreateMutex( lpMutexAttributes, bInitialOwner, lpName ); if ( NULL == hMutex ) { throw CWin32Exception( _T("hMutex::Create()"), ::GetLastError() ); } if ( bNeedOnlyOne && ERROR_ALREADY_EXISTS == ::GetLastError() ) { throw CWin32Exception( _T("hMutex::Create()"), ::GetLastError() ); } return hMutex; }
/* This is called when all startup is done */ void SetServiceRunning() { if (!g_bRunningAsService) return; g_ServiceStatus.dwCurrentState = SERVICE_RUNNING; g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; if( !SetServiceStatus( g_ServiceStatusHandle, &g_ServiceStatus ) ) throw CWin32Exception(); }
static HANDLE Create( LPSECURITY_ATTRIBUTES lpEventAttributes, bool bManualReset, bool bInitialState, LPCTSTR lpName) { HANDLE hEvent = ::CreateEvent( lpEventAttributes, bManualReset, bInitialState, lpName ); if ( NULL == hEvent ) { throw CWin32Exception( _T("CEvent::Create()"), ::GetLastError() ); } return hEvent; }
void CThread::Start() { if (m_hThread == INVALID_HANDLE_VALUE) { unsigned int threadID = 0; m_hThread = (HANDLE)::_beginthreadex(0, 0, ThreadFunction, (void*)this, 0, &threadID); if (m_hThread == INVALID_HANDLE_VALUE) { throw CWin32Exception(_T("CThread::Start() - _beginthreadex"), GetLastError()); } } else { throw CException(_T("CThread::Start()"), _T("Thread already running - you can only call Start() once!")); } }
bool CMutex::Wait( DWORD timeoutMillis ) const { bool ok; DWORD result = ::WaitForSingleObject( m_hMutex, timeoutMillis ); if ( result == WAIT_TIMEOUT ) { ok = false; } else if ( result == WAIT_OBJECT_0 ) { ok = true; } else { throw CWin32Exception( _T("CMutex::Wait() - WaitForSingleObject"), ::GetLastError() ); } return ok; }
bool CIOCompletionPort::GetStatus( ULONG_PTR *pCompletionKey, PDWORD pdwNumBytes, OVERLAPPED **ppOverlapped, DWORD dwMilliseconds) { bool ok = true; if ( 0 == ::GetQueuedCompletionStatus( m_iocp, pdwNumBytes, pCompletionKey, ppOverlapped, dwMilliseconds ) ) { DWORD lastError = ::GetLastError(); if ( WAIT_TIMEOUT != lastError ) { throw CWin32Exception( _T("CIOCompletionPort::GetStatus() - GetQueuedCompletionStatus"), lastError ); } ok = false; } return ok; }
bool CThread::Wait(DWORD timeoutMillis) const { // TODO base class? Waitable? bool ok; DWORD result = ::WaitForSingleObject(m_hThread, timeoutMillis); if (result == WAIT_TIMEOUT) { ok = false; } else if (result == WAIT_OBJECT_0) { ok = true; } else { throw CWin32Exception(_T("CThread::Wait() - WaitForSingleObject"), ::GetLastError()); } return ok; }