home *** CD-ROM | disk | FTP | other *** search
- /////////////////////////////////////////////////////////////////////////////
- //
- // File : MsgTracerApp.cpp
- // Project : MsgTrace
- // Component : MsgTracer
- //---------------------------------------------------------------------------
- // Description : the trtacer application
- //
- /////////////////////////////////////////////////////////////////////////////
- //
- // SourceSafe Strings. Do not change.
- //---------------------------------------------------------------------------
- // $Author: jeskes $
- // $Date: $
- // $Revision: $
- //
- /////////////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "resource.h"
- #include "initguid.h"
- #include "MsgTracer.h"
- #include "MsgTracerApp.h"
-
- #include "MsgTracer_i.c"
- #include "MsgTracerComp.h"
-
- #include "MainFrm.h"
- #include "OutputDoc.h"
- #include "OutputView.h"
-
- #include "AboutDlg.h"
- #include "AttachDlg.h"
- #include "SettingsDlg.h"
-
- #include "MsgTracerThread.h"
- #include "MsgTracerProcess.h"
-
- /////////////////////////////////////////////////////////////////////////////
-
- #define MSG_TRACER_RUNNING_EVENT "MsgTracerRunningEvent"
-
- /////////////////////////////////////////////////////////////////////////////
- // ATL Module
- /////////////////////////////////////////////////////////////////////////////
-
- CComModule _Module;
-
- BEGIN_OBJECT_MAP(ObjectMap)
- OBJECT_ENTRY(CLSID_MsgTracerComp, CMsgTracerComp)
- END_OBJECT_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // static helpers
- /////////////////////////////////////////////////////////////////////////////
-
- static LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
- {
- while (*p1 != NULL)
- {
- LPCTSTR p = p2;
- while (*p != NULL)
- {
- if (*p1 == *p++)
- return p1+1;
- }
- p1++;
- }
- return( NULL );
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMsgTracerApp
- /////////////////////////////////////////////////////////////////////////////
-
- CMsgTracerApp theApp;
-
- BEGIN_MESSAGE_MAP(CMsgTracerApp, CWinApp)
- //{{AFX_MSG_MAP(CMsgTracerApp)
- ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
- ON_COMMAND(ID_TRACE_ATTACH, OnTraceAttach)
- ON_COMMAND(ID_TRACE_SETTINGS, OnTraceSettings)
- ON_COMMAND(ID_TRACE_CONTINUE, OnTraceContinue)
- ON_UPDATE_COMMAND_UI(ID_TRACE_CONTINUE, OnUpdateTraceContinue)
- ON_COMMAND(ID_TRACE_PAUSE, OnTracePause)
- ON_UPDATE_COMMAND_UI(ID_TRACE_PAUSE, OnUpdateTracePause)
- ON_UPDATE_COMMAND_UI(ID_INDICATOR_RUNNING, OnUpdateIndicatorRun)
- ON_UPDATE_COMMAND_UI(ID_INDICATOR_PROCESS, OnUpdateIndicatorProcess)
- //}}AFX_MSG_MAP
- ON_COMMAND(ID_HELP, CWinApp::OnHelp)
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CMsgTracerApp construction
- /////////////////////////////////////////////////////////////////////////////
-
- CMsgTracerApp::CMsgTracerApp() :
-
- m_hRunningEvent( NULL ),
- m_bFactoriedRegistered( FALSE ),
-
- m_pOutputDoc( NULL ),
- m_pOutputTemplate( NULL ),
-
- m_bAllowAutoAttach( FALSE ),
- m_bShowOnStartup( TRUE ),
- m_nTimeoutWaitForDebugEvent( 5000 ),
- m_nBufferedLines( 200 )
-
- {
- m_pTracerThread = new CMsgTracerThread;
- m_pProcessList = new CMsgTracerProcessList;
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- CMsgTracerApp::~CMsgTracerApp()
- {
- if( NULL != m_hRunningEvent )
- {
- CloseHandle( m_hRunningEvent );
- }
-
- delete m_pTracerThread;
- m_pTracerThread = NULL;
-
- delete m_pProcessList;
- m_pProcessList = NULL;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMsgTracerApp Initialisierung
- /////////////////////////////////////////////////////////////////////////////
-
- BOOL CMsgTracerApp::InitInstance()
- {
- //-----------------------------------------------------------------------
- // only one instance allowed!
- //-----------------------------------------------------------------------
-
- if( IsTracerAlreadyRunning() )
- {
- return( FALSE );
- }
-
- //-----------------------------------------------------------------------
- // settings are read from registry
- //-----------------------------------------------------------------------
-
- SetRegistryKey( _T( "msg" ) );
- ReadSettings();
-
- //-----------------------------------------------------------------------
- // Initialize COM and ATL (apartment - for easier MFC access)
- //-----------------------------------------------------------------------
-
- HRESULT hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
- _ASSERTE( SUCCEEDED( hr ) );
-
- HINSTANCE hInstance = AfxGetInstanceHandle();
- _Module.Init( ObjectMap, hInstance );
-
- //-----------------------------------------------------------------------
- // Check commandine arguments
- //-----------------------------------------------------------------------
-
- TCHAR szTokens[] = _T("-/");
- LPCTSTR lpCmdLine = GetCommandLine();
- LPCTSTR lpszToken = FindOneOf( lpCmdLine, szTokens );
-
- while( lpszToken != NULL )
- {
- if( 0 == lstrcmpi( lpszToken, _T( "UnregServer" ) ) )
- {
- _Module.UpdateRegistryFromResource( IDR_MsgTracer, FALSE );
- _Module.UnregisterServer();
- return( FALSE );
- }
- if( 0 == lstrcmpi( lpszToken, _T( "RegServer" ) ) )
- {
- _Module.UpdateRegistryFromResource( IDR_MsgTracer, TRUE );
- // typelib sits in MsgTracerPS
- _Module.RegisterServer( FALSE );
- return( FALSE );
- }
-
- lpszToken = FindOneOf( lpszToken, szTokens );
- }
-
- //-----------------------------------------------------------------------
- // create main window
- //-----------------------------------------------------------------------
-
- #ifdef _AFXDLL
- Enable3dControls();
- #else
- Enable3dControlsStatic();
- #endif
-
- m_pOutputTemplate = new CSingleDocTemplate(
- IDR_MAINFRAME,
- RUNTIME_CLASS(COutputDoc),
- RUNTIME_CLASS(CMainFrame), // main SDI frame window
- RUNTIME_CLASS(COutputView));
-
- AddDocTemplate( m_pOutputTemplate );
-
- m_pOutputDoc = (COutputDoc*) m_pOutputTemplate->OpenDocumentFile( NULL, FALSE );
-
- if( NULL == m_pOutputDoc )
- {
- return( FALSE );
- }
-
- if( m_bShowOnStartup )
- {
- m_pMainWnd->ShowWindow(SW_SHOW);
- m_pMainWnd->UpdateWindow();
- }
-
- //-----------------------------------------------------------------------
- // now we are ready to create components
- //-----------------------------------------------------------------------
-
- hr = _Module.RegisterClassObjects( CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE);
- _ASSERTE( SUCCEEDED( hr ) );
-
- m_bFactoriedRegistered = TRUE;
-
- return( TRUE );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- int CMsgTracerApp::ExitInstance()
- {
- if( ( NULL != m_pTracerThread ) && m_pTracerThread->IsRunning() )
- {
- m_pTracerThread->Stop();
- m_pTracerThread->Resume();
- m_pTracerThread->IsRunning( INFINITE );
- }
-
- if( m_bFactoriedRegistered )
- {
- _Module.RevokeClassObjects();
- }
-
- CoUninitialize();
-
- return( CWinApp::ExitInstance() );
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMsgTracerApp: running stuff
- /////////////////////////////////////////////////////////////////////////////
-
- BOOL CMsgTracerApp::IsTracerAlreadyRunning()
- {
- m_hRunningEvent = OpenEvent( EVENT_ALL_ACCESS, FALSE, MSG_TRACER_RUNNING_EVENT );
-
- if( NULL != m_hRunningEvent )
- {
- SetEvent( m_hRunningEvent );
- CloseHandle( m_hRunningEvent );
- return( TRUE );
- }
-
- m_hRunningEvent = CreateEvent( NULL, TRUE, FALSE, MSG_TRACER_RUNNING_EVENT );
-
- if( NULL != m_hRunningEvent )
- {
- DWORD dwThreadId;
- ::CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) RunningProc, this, 0, &dwThreadId );
- }
-
- return( FALSE );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- DWORD CMsgTracerApp::RunningProc( CMsgTracerApp* pThis )
- {
- while( WAIT_OBJECT_0 == WaitForSingleObject( pThis->m_hRunningEvent, INFINITE ) )
- {
- ResetEvent( pThis->m_hRunningEvent );
- ShowWindow( pThis->m_pMainWnd->m_hWnd, SW_RESTORE );
- SetForegroundWindow( pThis->m_pMainWnd->m_hWnd );
- }
-
- return( 0 );
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMsgTracerApp command handlers
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::OnAppAbout()
- {
- CAboutDlg aboutDlg;
- aboutDlg.DoModal();
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMsgTracerApp trace stuff
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::AttachProcess( DWORD dwProcessId, BOOL bWait )
- {
- if( NULL == m_pTracerThread )
- {
- return;
- }
-
- CMsgTracerProcess* pProcess = NULL;
- m_pProcessList->Add( dwProcessId, &pProcess );
-
- if( NULL != pProcess )
- {
- m_pTracerThread->AttachProcess( pProcess, bWait );
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- HANDLE CMsgTracerApp::ProcessIdToHandle( DWORD dwProcessId )
- {
- CMsgTracerProcess* pProcess = m_pProcessList->Find( dwProcessId );
-
- if( NULL == pProcess )
- {
- return( NULL );
- }
-
- return( pProcess->GetProcessHandle() );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- BOOL CMsgTracerApp::IsAttached( DWORD dwProcessId, CMsgTracerProcess** pp )
- {
- CMsgTracerProcess* pProcess = m_pProcessList->Find( dwProcessId );
-
- if( NULL != pp )
- {
- *pp = pProcess;
- }
-
- return( NULL != pProcess );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- CMsgTracerProcessList* CMsgTracerApp::GetAttachedProcessList()
- {
- return( m_pProcessList );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::OnTraceAttach()
- {
- CAttachDlg dlg;
- dlg.DoModal();
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- BOOL CMsgTracerApp::IsTracePaused()
- {
- return( !m_pTracerThread->m_bWantOutputDebugStringEvents );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::OnUpdateIndicatorRun( CCmdUI* pCmdUI )
- {
- CString s;
- s.LoadString( m_pTracerThread->m_bWantOutputDebugStringEvents ?
- ID_INDICATOR_RUNNING :
- ID_INDICATOR_STOPPED );
-
- pCmdUI->SetText( s );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::OnUpdateIndicatorProcess( CCmdUI* pCmdUI )
- {
- COutputView* pView =
- (COutputView*) ( (CFrameWnd*) AfxGetMainWnd() )->GetActiveView();
-
- ASSERT( NULL != pView );
-
- CString s = pView->GetCurRowAsString();
-
- if( !s.IsEmpty() )
- {
- DWORD dwProcessId = strtol( ( (LPCSTR) s ) + 1, NULL, 16 );
- CMsgTracerProcess* pProcess = NULL;
-
- if( IsAttached( dwProcessId, &pProcess ) )
- {
- s = pProcess->GetBaseName();
- }
- else
- {
- s.Empty();
- }
- }
-
- pCmdUI->SetText( s );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::OnTraceContinue()
- {
- m_pTracerThread->m_bWantOutputDebugStringEvents = TRUE;
-
- CString s;
- s.LoadString( IDS_TRACE_CONTINUED );
- m_pOutputDoc->AddLine( s );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::OnUpdateTraceContinue(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable( !m_pTracerThread->m_bWantOutputDebugStringEvents );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::OnTracePause()
- {
- m_pTracerThread->m_bWantOutputDebugStringEvents = FALSE;
-
- CString s;
- s.LoadString( IDS_TRACE_PAUSED );
- m_pOutputDoc->AddLine( s );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::OnUpdateTracePause(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable( m_pTracerThread->m_bWantOutputDebugStringEvents );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::WriteOutput( LPCTSTR lpszMessage )
- {
- if( m_pTracerThread->m_bWantOutputDebugStringEvents )
- {
- m_pOutputDoc->AddLine( lpszMessage );
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMsgTracerApp settings stuff
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::OnTraceSettings()
- {
- CSettingsDlg dlg;
-
- dlg.m_bAllowAutoAttach = m_bAllowAutoAttach;
- dlg.m_bShowOnStartup = m_bShowOnStartup;
- dlg.m_nTimeoutWaitForDebugEvent = m_nTimeoutWaitForDebugEvent;
- dlg.m_nBufferedLines = m_nBufferedLines;
-
- if( IDOK == dlg.DoModal() )
- {
- m_bAllowAutoAttach = dlg.m_bAllowAutoAttach;
- m_bShowOnStartup = dlg.m_bShowOnStartup;
- m_nTimeoutWaitForDebugEvent = dlg.m_nTimeoutWaitForDebugEvent;
- m_nBufferedLines = dlg.m_nBufferedLines;
-
- WriteSettings();
- m_pOutputDoc->SetMaxBufferedLines( m_nBufferedLines );
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::ReadSettings()
- {
- m_bAllowAutoAttach = GetProfileInt( "Settings", "AllowAutoAttach", m_bAllowAutoAttach );
- m_bShowOnStartup = GetProfileInt( "Settings", "ShowOnStartup", m_bShowOnStartup );
- m_nTimeoutWaitForDebugEvent = GetProfileInt( "Settings", "TimeoutWaitForDebugEvent", m_nTimeoutWaitForDebugEvent );
- m_nBufferedLines = GetProfileInt( "Settings", "BufferedLines", 1000 );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerApp::WriteSettings()
- {
- WriteProfileInt( "Settings", "AllowAutoAttach", (int) m_bAllowAutoAttach );
- WriteProfileInt( "Settings", "ShowOnStartup", (int) m_bShowOnStartup );
- WriteProfileInt( "Settings", "TimeoutWaitForDebugEvent", m_nTimeoutWaitForDebugEvent );
- WriteProfileInt( "Settings", "BufferedLines", m_nBufferedLines );
- }
-
-