// sessions.cpp : Implementation of CsmtpadmApp and DLL registration. #include "stdafx.h" #include "smtpadm.h" #include "sessions.h" #include "oleutil.h" #include "smtpcmn.h" #include "smtptype.h" #include "smtpapi.h" #include // Must define THIS_FILE_* macros to use SmtpCreateException() #define THIS_FILE_HELP_CONTEXT 0 #define THIS_FILE_PROG_ID _T("Smtpadm.Sessions.1") #define THIS_FILE_IID IID_ISmtpAdminSessions ///////////////////////////////////////////////////////////////////////////// // // // Use a macro to define all the default methods // DECLARE_METHOD_IMPLEMENTATION_FOR_STANDARD_EXTENSION_INTERFACES(SmtpAdminSessions, CSmtpAdminSessions, IID_ISmtpAdminSessions) STDMETHODIMP CSmtpAdminSessions::InterfaceSupportsErrorInfo(REFIID riid) { static const IID* arr[] = { &IID_ISmtpAdminSessions, }; for (int i=0;icEntries; } else { hr = SmtpCreateExceptionFromWin32Error ( err ); goto Exit; } Exit: if ( FAILED(hr) && hr != DISP_E_EXCEPTION ) { hr = SmtpCreateExceptionFromHresult ( hr ); } return hr; } STDMETHODIMP CSmtpAdminSessions::GetNth ( long lIndex ) { HRESULT hr = NOERROR; // Did we enumerate first? if ( m_pSessionInfo == NULL ) { return SmtpCreateException ( IDS_SMTPEXCEPTION_DIDNT_ENUMERATE ); } // Is the index valid? if ( lIndex < 0 || (DWORD) lIndex >= m_cCount ) { return SmtpCreateException ( IDS_SMTPEXCEPTION_INVALID_INDEX ); } // // Copy the properties from m_pSessionInfo [ lIndex ] to member variables: // // ( CComBSTR handles free-ing of old properties ) m_dwId = m_pSessionInfo->aConnUserEntry[ lIndex ].dwUserId; m_dwConnectTime = m_pSessionInfo->aConnUserEntry[ lIndex ].dwConnectTime; m_strUsername = m_pSessionInfo->aConnUserEntry[ lIndex ].lpszName; if ( m_strUsername == NULL ) { hr = E_OUTOFMEMORY; goto Exit; } m_strHost = m_pSessionInfo->aConnUserEntry[ lIndex ].lpszHost; if ( m_strHost == NULL ) { hr = E_OUTOFMEMORY; goto Exit; } // GetNth sets the cursor: m_fSetCursor = TRUE; Exit: if ( FAILED(hr) && hr != DISP_E_EXCEPTION ) { hr = SmtpCreateExceptionFromHresult ( hr ); } return hr; } STDMETHODIMP CSmtpAdminSessions::Terminate ( ) { HRESULT hr = NOERROR; DWORD err; // Validate Server & Service Instance: if ( m_iadsImpl.QueryInstance() == 0 ) { return SmtpCreateException ( IDS_SMTPEXCEPTION_SERVICE_INSTANCE_CANT_BE_ZERO ); } // Call the TerminateSession RPC: err = SmtpDisconnectUser ( m_iadsImpl.QueryComputer(), m_dwId, m_iadsImpl.QueryInstance() ); if( err != NOERROR ) { hr = SmtpCreateExceptionFromWin32Error ( err ); goto Exit; } Exit: if ( FAILED(hr) && hr != DISP_E_EXCEPTION ) { hr = SmtpCreateExceptionFromHresult ( hr ); } return hr; } STDMETHODIMP CSmtpAdminSessions::TerminateAll ( ) { // Did we enumerate first? HRESULT hr = NOERROR; DWORD ErrResult = 0; DWORD Err; DWORD i; // Validate Server & Service Instance: if ( m_iadsImpl.QueryInstance() == 0 ) { return SmtpCreateException ( IDS_SMTPEXCEPTION_SERVICE_INSTANCE_CANT_BE_ZERO ); } // Make sure the user has enumerated: if ( m_pSessionInfo == NULL ) { return SmtpCreateException ( IDS_SMTPEXCEPTION_DIDNT_ENUMERATE ); } // For Each Session: for ( i = 0; i < m_cCount; i++ ) { // Call the terminate session RPC: Err = SmtpDisconnectUser ( m_iadsImpl.QueryComputer(), m_pSessionInfo->aConnUserEntry[ i ].dwUserId, m_iadsImpl.QueryInstance() ); if ( Err != 0 && ErrResult == 0 ) { ErrResult = Err; } } if( ErrResult != NOERROR ) { hr = SmtpCreateExceptionFromWin32Error ( ErrResult ); goto Exit; } Exit: if ( FAILED(hr) && hr != DISP_E_EXCEPTION ) { hr = SmtpCreateExceptionFromHresult ( hr ); } return hr; }