home *** CD-ROM | disk | FTP | other *** search
- /*
- * BugBench.cpp
- * $Header: /BoundsChecker/Examples/BUGBNCHX/BUGBENCH.CPP 6 4/09/97 9:26a Bob $
- *
- * Description:
- * Defines the behavior for the application and the main dialog box.
- *
- * Notes:
- * <implementation notes go here>
- *
- ***********************************************************************
- *
- * Nu-Mega Technologies, Inc.
- * P.O. Box 7780
- * Nashua, NH 03060
- *
- * (c) Copyright 1994, 1995 Nu-Mega Technologies, Inc.
- * ALL RIGHTS RESERVED.
- *
- ***********************************************************************
- *
- **********************************************************************/
- #include "stdafx.h"
- #include <time.h>
- #include <ctl3d.h>
- #ifndef BCBVER
- #include <crtdbg.h>
- #endif
-
- #include "bcerrtyp.h"
-
- #include "cstatbmp.h"
- #include "BCTree.h"
- #include "aboutbox.h"
- #include "RegIface.h"
-
- #include "BugBench.h"
- #include "resource.h"
-
- ////////////////////////////////////////////////////////////////////////
- // Types
- typedef void (*PFUNC_POPTREE) ( HWND hWndTree ) ;
- typedef void (*PFUNC_DEPOPTREE) ( HWND hWndTree ) ;
- typedef void (*PFUNC_DORANDOM) ( ) ;
- typedef void (*PFUNC_DOALL) ( ) ;
-
-
- ////////////////////////////////////////////////////////////////////////
- // Constants that we need.
- // The application name.
- #ifndef BCBVER
- const TCHAR * constAPPNAME = _T ( "BugBench" ) ;
- #else
- const TCHAR * constAPPNAME = _T ( "BugBcb" ) ;
- #endif
- const constMaxBugModules = 255 ;
-
- ////////////////////////////////////////////////////////////////////////
- // Globals
- HINSTANCE g_hInstance ;
- HICON g_hIcon ;
- BOOL g_fDoRealizationInActive ;
- BOOL g_fSkipMsgBox ;
- HWND g_hwndTree ;
- HWND g_hwndStaticBmp ;
- HINSTANCE g_hBugModArray [ constMaxBugModules ] ;
- int g_nBugModules ;
- int g_nRunError;
-
- ////////////////////////////////////////////////////////////////////////
-
-
- void MainDlg_FreeBugModules ( )
- {
- for ( int i = 0; i < g_nBugModules ; i++ )
- {
- PFUNC_DEPOPTREE pFreeFunc = ( PFUNC_DEPOPTREE ) GetProcAddress ( g_hBugModArray [ i ], _T ( "DePopulateTree" ) ) ;
- if ( NULL == pFreeFunc )
- {
- pFreeFunc = ( PFUNC_DEPOPTREE ) GetProcAddress ( g_hBugModArray [ i ], _T ( "_DePopulateTree" ));
- }
- if ( NULL != pFreeFunc )
- {
- #ifndef BCBVER
- __try
- {
- #else
- try
- {
- #endif
- pFreeFunc ( g_hwndTree );
- }
- __except ( EXCEPTION_EXECUTE_HANDLER )
- {
- }
- }
-
- FreeLibrary ( g_hBugModArray [ i ] ) ;
- }
- }
-
- void MainDlg_CleanUpAndShutDown ( HWND hWnd )
- {
- // Cleans up all allocations. It doesn't really look good
- // if Nu-Mega's own demo applications show all sorts of problems
- // and leaks.
- ASSERT ( NULL != g_hwndTree ) ;
- ASSERT ( NULL != g_hwndStaticBmp ) ;
-
- // Hide the window so we don't see every single tree node deleted.
- ShowWindow ( hWnd , SW_HIDE ) ;
-
- // Delete all items in the tree control.
- //TreeCtrl_DeleteAllItems ( g_hwndTree ) ;
- MainDlg_FreeBugModules ( ) ;
-
- StaticBitmap_CleanUp ( g_hwndStaticBmp ) ;
-
- // End it.
- EndDialog ( hWnd , 0 ) ;
- }
-
- // Bring up the AboutBox.
- void MainDlg_OnAbout ( HWND hWnd )
- {
- DialogBox ( g_hInstance ,
- MAKEINTRESOURCE ( IDD_ABOUTBOX ) ,
- hWnd ,
- ( DLGPROC ) AboutBox_DlgProc ) ;
- }
-
- // Execute a error check testing function.
- BOOL MainDlg_IsItemExecutable ( HWND hwndDlg )
- {
- BOOL executable = FALSE;
- LParamInfo * pInfo =
- ( LParamInfo * ) TreeCtrl_GetSelectedLParam ( g_hwndTree ) ;
-
- // The only items that have functions are the leaf nodes.
- if ( 2 == pInfo->iType )
- {
- // Since some of the functions might not be done yet, we
- // need to do some sanity check before setting EIP to NULL
- // and executing it.
- if ( NULL != pInfo->stEO.pFunc )
- {
- executable = TRUE;
- }
- }
- return executable;
- }
- // Execute a error check testing function.
- void MainDlg_ExecuteFunction ( HWND hwndDlg )
- {
- if ( MainDlg_IsItemExecutable ( hwndDlg ) == TRUE )
- {
- LParamInfo * pInfo =
- ( LParamInfo * ) TreeCtrl_GetSelectedLParam ( g_hwndTree ) ;
- pInfo->stEO.pFunc ( ) ;
- }
- }
-
- // Handle the double click event on the tree control.
- void MainDlg_OnDblClkTreeList ( HWND hwndDlg ,
- NMHDR * ,
- LRESULT * pResult )
- {
- MainDlg_ExecuteFunction ( hwndDlg ) ;
- *pResult = 0 ;
- }
-
- // Handle the test button or the test menu item.
- void MainDlg_OnTest ( HWND hwndDlg )
- {
- MainDlg_ExecuteFunction ( hwndDlg ) ;
- }
-
- // Handle the 5 random button.
- void MainDlg_OnFiveRandom ( HWND hwndDlg )
- {
- // reseed each time through.
- srand( (unsigned) time( NULL ) ) ;
-
- // Pick a random bug module.
- int iMod = rand ( ) % g_nBugModules ;
- PFUNC_DORANDOM pDoRandomFunc = ( PFUNC_DORANDOM ) GetProcAddress ( g_hBugModArray [ iMod ], _T ( "DoRandomErrors" ) ) ;
- if ( NULL == pDoRandomFunc )
- {
- pDoRandomFunc = ( PFUNC_DORANDOM ) GetProcAddress ( g_hBugModArray [ iMod ], _T ( "_DoRandomErrors" ) );
- }
- if ( NULL != pDoRandomFunc )
- {
- #ifndef BCBVER
- __try
- {
- #else
- try
- {
- #endif
- // Call its random func.
- pDoRandomFunc ( );
- }
- __except ( EXCEPTION_EXECUTE_HANDLER )
- {
- }
- }
- }
-
- // Handle the hidden button (or Ctrl-P).
- void MainDlg_OnAllDo ( HWND hwndDlg )
- {
- // Do all the errors in all the bug modules
- for ( int i = 0; i < g_nBugModules ; i++ )
- {
- PFUNC_DOALL pDoAllFunc = ( PFUNC_DOALL ) GetProcAddress ( g_hBugModArray [ i ], _T ( "DoAllErrors" ) ) ;
- if ( NULL == pDoAllFunc )
- {
- pDoAllFunc = ( PFUNC_DOALL ) GetProcAddress ( g_hBugModArray [ i ], _T ( "_DoAllErrors" ) );
- }
- if ( NULL != pDoAllFunc )
- {
- #ifndef BCBVER
- __try
- {
- #else
- try
- {
- #endif
- pDoAllFunc ( );
- }
- __except ( EXCEPTION_EXECUTE_HANDLER )
- {
- }
- }
- }
- }
-
- // Handle the expand all menu item.
- void MainDlg_OnExpandAll ( HWND hwndDlg )
- {
- ASSERT ( NULL != g_hwndTree ) ;
- TreeCtrl_ExpandAll ( g_hwndTree , TVE_EXPAND ) ;
- TreeCtrl_EnsureVisible ( g_hwndTree , TreeCtrl_GetSelectedItem ( g_hwndTree ) ) ;
- }
-
- // Handle the collapse all menu item.
- void MainDlg_OnCollapseAll ( HWND hwndDlg )
- {
- ASSERT ( NULL != g_hwndTree ) ;
-
- TreeCtrl_ExpandAll ( g_hwndTree , TVE_COLLAPSE ) ;
- TreeCtrl_EnsureVisible ( g_hwndTree , TreeCtrl_GetSelectedItem ( g_hwndTree ) ) ;
- }
-
- BOOL MainDlg_InitBugModule ( LPCTSTR szModName )
- {
- BOOL fReturn = FALSE ;
-
- if ( NULL != szModName )
- {
- // Attempt to load the DLL. The assumption is made that
- // LoadLibrary will prevent any serious errors in the
- // DLLs DllMain function from hosing our memory.
- HINSTANCE hExtension = LoadLibrary ( szModName ) ;
- if ( NULL != hExtension )
- {
- PFUNC_POPTREE pInitFunc = ( PFUNC_POPTREE ) GetProcAddress ( hExtension , _T ( "PopulateTree" ) ) ;
- if ( NULL == pInitFunc )
- {
- pInitFunc = ( PFUNC_POPTREE ) GetProcAddress ( hExtension, _T ("_PopulateTree" ));
- }
- DWORD dwError = GetLastError ( ) ;
- if ( NULL != pInitFunc )
- {
- // Before we try calling the initialization function,
- // setup the exception handler, so if the export isn't
- // setup right by the DLL, we won't blow up.
- #ifndef BCBVER
- __try
- {
- #else
- try
- {
- #endif
- pInitFunc ( g_hwndTree );
- g_hBugModArray [ g_nBugModules++ ] = hExtension;
-
- fReturn = TRUE ;
- }
- __except ( EXCEPTION_EXECUTE_HANDLER )
- {
- }
- } // if valid function pointer
- else
- FreeLibrary ( hExtension ) ;
-
- } // if valid extension DLL instance handle
- }
-
- return fReturn ;
-
- }
-
- void MainDlg_LoadBugModules ( )
- {
- // Begin enumerating the DLLs
- HANDLE hFind ;
- WIN32_FIND_DATA findData = { 0 } ;
-
- g_nBugModules = 0;
-
- hFind = FindFirstFile ( "*.bug", &findData ) ;
-
- if ( hFind == INVALID_HANDLE_VALUE )
- TRACE ( _T ( "No BugModules Found.\n" ) ) ;
- else
- {
- do
- {
- // Pass it to the InitExtension routine. Using the
- // full path prevents other DLLs in the path with
- // the same name from being loaded.
- if ( MainDlg_InitBugModule ( findData.cFileName ) )
- {
- TRACE1 ( _T ( "BugModule Loaded: %s\n" ) , findData.cFileName ) ;
- }
- else
- TRACE1 ( _T ( "Unable to load BugModule: %s\n" ) , findData.cFileName ) ;
- }
- while ( FindNextFile ( hFind , &findData ) ) ;
-
- FindClose ( hFind ) ;
- }
-
- // Return the selected item to be the first item in the tree control.
- HTREEITEM hitemFirst = TreeView_GetNextItem ( g_hwndTree , NULL , TVGN_ROOT ) ;
- if ( NULL != hitemFirst )
- TreeView_SelectItem ( g_hwndTree , hitemFirst ) ;
-
- }
-
- BOOL MainDlg_SelectError( HWND hwndDlg, HTREEITEM item, UINT code, int & errorValue )
- {
- BOOL found = FALSE;
-
- item = TreeView_GetNextItem ( g_hwndTree , item , code ) ;
- if ( item == NULL )
- return found;
-
- TreeCtrl_Select ( g_hwndTree, item, TVGN_CARET ) ;
- if ( MainDlg_IsItemExecutable( hwndDlg ) == TRUE )
- {
- if( ++errorValue == g_nRunError )
- {
- found = TRUE;
- }
- else
- {
- found = MainDlg_SelectError( hwndDlg, item, TVGN_NEXT, errorValue );
- }
- }
- else
- {
- found = MainDlg_SelectError( hwndDlg, item, TVGN_CHILD, errorValue );
- if( found != TRUE )
- found = MainDlg_SelectError( hwndDlg, item, TVGN_NEXT, errorValue );
- }
- return found;
- }
-
- void MainDlg_RunError( HWND hwndDlg )
- {
- int currentError = 0;
- if( MainDlg_SelectError( hwndDlg, NULL, TVGN_ROOT, currentError ) == TRUE )
- {
- MainDlg_ExecuteFunction( hwndDlg );
- }
- }
-
- ///////////////////////////////////////////////////////////
- // Handles the WM_INITDIALOG message for the main dialog.
- BOOL MainDlg_OnInitDialog ( HWND hWnd , HWND hwndFocus , LPARAM lParam )
- {
- TCHAR szBuff[ 1024 ] ;
- BOOL fRetVal = TRUE ;
- HMENU hSysMenu ;
- int nSysMenuItems ;
- DWORD dwStyle ;
-
- // Initialize globals
- g_hIcon = NULL ;
- g_fDoRealizationInActive = FALSE ;
- g_hwndTree = NULL ;
- g_hwndStaticBmp = NULL ;
- g_hIcon = LoadIcon ( g_hInstance , MAKEINTRESOURCE ( IDR_MAINFRAME ) ) ;
- ASSERT ( NULL != g_hIcon ) ;
-
- // Set the icon of the dialog to be the mainframe icon.
- SendMessage(hWnd, WM_SETICON, FALSE, (LPARAM)g_hIcon);
- SendMessage(hWnd, WM_SETICON, TRUE, (LPARAM)g_hIcon);
-
-
- dwStyle = GetWindowLong ( hWnd , GWL_STYLE ) ;
- dwStyle |= DS_3DLOOK ;
- SetWindowLong ( hWnd , GWL_STYLE , dwStyle ) ;
-
- // Add "About..." menu item to system men
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT ( ( IDM_ABOUTBOX & 0xFFF0 ) == IDM_ABOUTBOX ) ;
- ASSERT ( IDM_ABOUTBOX < 0xF000 ) ;
-
- hSysMenu = GetSystemMenu ( hWnd , FALSE ) ;
-
- VERIFY ( LoadString ( GetModuleHandle ( NULL ) ,
- IDS_ABOUTBOX ,
- szBuff ,
- sizeof ( szBuff ) ) ) ;
-
- nSysMenuItems = GetMenuItemCount ( hSysMenu ) ;
- if ( ( nSysMenuItems != 0 ) && ( nSysMenuItems != -1 ) )
- {
- if ( ! AppendMenu ( hSysMenu , MF_SEPARATOR , 0 , NULL ) )
- fRetVal = FALSE ;
- if ( ! AppendMenu ( hSysMenu , MF_STRING , IDM_ABOUTBOX , szBuff ) )
- fRetVal = FALSE ;
- }
-
- // Get the HWNDs for the tree control and the static
- // bitmap.
- g_hwndTree = GetDlgItem ( hWnd , IDC_TREELIST ) ;
- ASSERT ( NULL != g_hwndTree ) ;
- if ( NULL == g_hwndTree )
- fRetVal = FALSE ;
-
- g_hwndStaticBmp = GetDlgItem ( hWnd , IDC_BCBITMAP ) ;
- ASSERT ( NULL != g_hwndStaticBmp ) ;
- if ( NULL == g_hwndStaticBmp )
- fRetVal = FALSE ;
-
- // Center the window.
- CenterWindow ( hWnd ) ;
-
- // Initialize the bitmap
- if ( ! StaticBitmap_Init ( g_hwndStaticBmp ) )
- fRetVal = FALSE ;
-
- // Fill the tree control with data.
- //FillTree ( g_hwndTree ) ;
- MainDlg_LoadBugModules ( ) ;
-
- if( g_nRunError != 0 )
- {
- // Run error
- MainDlg_RunError( hWnd );
- // Shut bugbench down without ever having displayed it.
- MainDlg_CleanUpAndShutDown ( hWnd );
- }
-
- // Show the "Danger Will Robinson" message.
- TCHAR * szMsg = _T ( "All efforts have been made to make BugBench\n"\
- "as failsafe as possible. However, wild pointer\n"\
- "writes can always bring down the best programs.\n"\
- "Additionally, it is possible to corrupt the heap\n"\
- "with certain combinations of errors that stop\n"
- "BugBench. While BugBench might end,\n"\
- "BoundsChecker will always show you where\n"
- "the errors are!" ) ;
- if ( FALSE == g_fSkipMsgBox )
- {
- MessageBox ( NULL ,
- szMsg ,
- constAPPNAME ,
- MB_APPLMODAL | MB_OK ) ;
- }
-
- return ( fRetVal ) ;
- }
-
- ///////////////////////////////////////////////////////////
- // Handles WM_COMMAND messages for the main dialog.
- void MainDlg_OnCommand ( HWND hWnd ,
- int idCmd ,
- HWND hWndCtl ,
- UINT uiCodeNotify )
- {
- switch ( idCmd )
- {
- // If it is one of the bugbench buttons.
- case IDC_TEST :
- case IDM_TEST :
- MainDlg_OnTest ( hWnd ) ;
- break ;
- case IDC_FIVERANDOM :
- MainDlg_OnFiveRandom ( hWnd ) ;
- break ;
- case IDC_ABOUT :
- MainDlg_OnAbout ( hWnd ) ;
- break ;
- case IDC_CLOSE :
- MainDlg_CleanUpAndShutDown ( hWnd ) ;
- break ;
- case IDC_ALLDO :
- MainDlg_OnAllDo ( hWnd ) ;
- break ;
- case IDM_EXPANDALL :
- MainDlg_OnExpandAll ( hWnd ) ;
- break ;
- case IDM_COLLAPSEALL :
- MainDlg_OnCollapseAll ( hWnd ) ;
- break ;
-
- }
- }
-
- // Handle the system menu ( for close and about ).
- void MainDlg_OnSysCommand ( HWND hWnd ,
- UINT idCmd ,
- int xPos ,
- int yPos )
- {
- // Handle About.
- if ( ( idCmd & 0xFFF0 ) == IDM_ABOUTBOX )
- {
- MainDlg_OnAbout ( hWnd ) ;
- }
-
- // Handle Close
- if ( SC_CLOSE == idCmd )
- {
- MainDlg_CleanUpAndShutDown ( hWnd ) ;
- }
- }
-
- void MainDlg_OnPaint ( HWND hWnd )
- {
- PAINTSTRUCT ps ;
- HDC hDC ;
- int cxIcon, cyIcon ;
-
- if ( IsIconic ( hWnd ) )
- {
- // The device context for painting
- hDC = BeginPaint ( hWnd , &ps ) ;
- ASSERT ( NULL != hDC ) ;
-
- SendMessage( hWnd ,
- WM_ICONERASEBKGND ,
- ( WPARAM ) hDC ,
- 0 );
-
- // Center icon in client rectangle
- cxIcon = GetSystemMetrics ( SM_CXICON ) ;
- cyIcon = GetSystemMetrics ( SM_CYICON ) ;
- RECT rect ;
- GetClientRect ( hWnd , &rect ) ;
- int x = ( RECTWIDTH ( &rect ) - cxIcon + 1 ) / 2 ;
- int y = ( RECTHEIGHT ( &rect ) - cyIcon + 1 ) / 2 ;
-
- // Draw the icon
- DrawIcon ( hDC , x , y , g_hIcon ) ;
- }
- }
-
- HCURSOR MainDlg_OnQueryDragIcon ( HWND hWnd )
- {
- return ( (HCURSOR) g_hIcon ) ;
- }
-
- void MainDlg_OnPaletteChanged ( HWND hWnd , HWND hwndPaletteChange )
- {
- // If we are not the window recieving the focus, call the
- // CStaticBitmap control to realize the palette.
- if ( ( hwndPaletteChange != hWnd ) && ( FALSE == IsIconic ( hWnd ) ) )
- {
- ASSERT ( NULL != g_hwndStaticBmp ) ;
- if ( NULL == g_hwndStaticBmp )
- return ;
-
- StaticBitmap_RealizeDIBPalette ( g_hwndStaticBmp ) ;
- }
- }
-
- BOOL MainDlg_OnQueryNewPalette ( HWND hWnd )
- {
- BOOL fReturn = FALSE ;
- // Sometimes the WM_QUERYNEWPALETTE message gets sent with the
- // before it should so we must then handle the palette realization
- // in the WM_ACTIVATE.
- if ( GetActiveWindow ( ) != hWnd )
- {
- g_fDoRealizationInActive = TRUE ;
- // Return TRUE here so that Windows knows we are an appl with
- // a palette to realize.
- fReturn = TRUE ;
- }
-
- if ( ( ! fReturn ) && ( FALSE == IsIconic ( hWnd ) ) )
- {
- // Pass this off to the CStaticBitmap control.
- ASSERT ( NULL != g_hwndStaticBmp ) ;
- if ( NULL != g_hwndStaticBmp )
- fReturn = StaticBitmap_RealizeDIBPalette ( g_hwndStaticBmp ) ;
- }
-
- return ( fReturn ) ;
- }
-
- void MainDlg_OnActivate ( HWND hWnd , UINT nState , HWND hwndActDeact, BOOL fMinimized )
- {
- // Do we need to do the palette realization?
- if ( TRUE == g_fDoRealizationInActive )
- {
- ASSERT ( NULL != g_hwndStaticBmp ) ;
- if ( NULL != g_hwndStaticBmp )
- StaticBitmap_RealizeDIBPalette ( g_hwndStaticBmp ) ;
-
- g_fDoRealizationInActive = FALSE ;
- }
- }
-
- void MainDlg_OnDeleteItemTreeList ( HWND hwndDlg , NMHDR* pNMHDR , LRESULT* pResult )
- {
- NM_TREEVIEW * pNMTreeView = (NM_TREEVIEW*)pNMHDR ;
-
- // Free the memory associated with the lParam.
- // if ( 0 != pNMTreeView->itemOld.lParam )
- // delete (LParamInfo*)pNMTreeView->itemOld.lParam ;
- *pResult = 0;
- }
-
- void MainDlg_OnSelChangedTreeList ( HWND hwndDlg , NMHDR* pNMHDR , LRESULT* pResult )
- {
- NM_TREEVIEW * pNMTreeView = (NM_TREEVIEW*)pNMHDR ;
- // The buffer we load strings into.
- TCHAR szBuff[ 1024 ] ;
- // The current tree item we are working with.
- HTREEITEM hItem ;
- // The structure used to get information about specific items.
- TV_ITEM stTI ;
- // The flag we use to determine if we are supposed to enable
- // or disable the Test button. It is only enabled if it is a
- // leaf node, which has the function in the lParam.
- BOOL bEnableTestButton = FALSE ;
- // The flag that we use to determine which LED to show next to the
- // BC Personal product.
- BOOL bShowPersonalLEDON = FALSE ;
- // The flag that tells us we need to show the product LEDs and
- // descriptions.
- BOOL bShowLEDs = FALSE ;
- // Depending on the item that gets the focus will determine what we
- // show in the description. If it is a leaf node, then that is
- // what we show, otherwise we will show the root node text.
- BOOL bShowRootNodeDescription = TRUE ;
- // Icon used to show if standard edition supports the error
- // check or not.
- HICON hIcon ;
- // Whether or not to show the LED windows
- int iProductLEDsShow ;
- // An HWND of IDC_DESCRIPTION static text.
- HWND hwndDescription = NULL ;
- // A standard HWND which we use to get the individual product
- // description controls.
- HWND hWnd ;
-
- ASSERT ( NULL != g_hwndTree ) ;
-
- // Get the description's HWND
- hwndDescription = GetDlgItem ( hwndDlg , IDC_DESCRIPTION ) ;
- ASSERT ( NULL != hwndDescription ) ;
-
- *pResult = 0;
-
- // Get the new selected item's lParam.
- LParamInfo * pLPI = (LParamInfo *)pNMTreeView->itemNew.lParam ;
- ASSERT ( NULL != pLPI ) ;
-
- hItem = pNMTreeView->itemNew.hItem ;
-
- // If this is a leaf node, set the description, detected by, and
- // instance text.
- if ( 2 == pLPI->iType )
- {
- // Get the description string.
- VERIFY ( ::LoadString ( pLPI->hModule ,
- pLPI->stEO.uiDescription ,
- szBuff ,
- sizeof ( szBuff ) ) ) ;
-
- // We just filled out the description so we don't need to do the
- // root node description.
- bShowRootNodeDescription = FALSE ;
-
- // A leaf node is selected, enable the test button if it has
- // a function to call.
- if ( NULL != pLPI->stEO.pFunc )
- {
- bEnableTestButton = TRUE ;
- }
- // For leaf nodes, we want to show the product LEDs.
- bShowLEDs = TRUE ;
-
- // Now get the parent and change the pLPI to the second
- // level.
- hItem = TreeCtrl_GetParentItem ( g_hwndTree , hItem ) ;
- ASSERT ( NULL != hItem ) ;
- stTI.hItem = hItem ;
- stTI.mask = TVIF_PARAM ;
-
- VERIFY ( TreeCtrl_GetItem ( g_hwndTree , &stTI ) ) ;
- pLPI = (LParamInfo *)stTI.lParam ;
- ASSERT ( NULL != pLPI ) ;
- if ( NULL == pLPI )
- {
- SetWindowText ( hwndDescription , szBuff ) ;
- return ;
- }
- }
- // Is this a first level item?
- if ( 1 == pLPI->iType )
- {
- // The first level item tells us if we are supposed to show
- // the product descriptions.
- if ( TRUE == pLPI->stET.bPersonalDoes )
- {
- // We are supposed to show the ON LED next to the BC Product
- // description.
- bShowPersonalLEDON = TRUE ;
- }
- // For the individual error decriptions, we want to show the
- // LEDs.
- bShowLEDs = TRUE ;
- // Now get the parent and change the pLPI to the root level.
- hItem = TreeCtrl_GetParentItem ( g_hwndTree , hItem ) ;
- ASSERT ( NULL != hItem ) ;
- stTI.hItem = hItem ;
- stTI.mask = TVIF_PARAM ;
-
- VERIFY ( TreeCtrl_GetItem ( g_hwndTree , &stTI ) ) ;
- pLPI = (LParamInfo *)stTI.lParam ;
- ASSERT ( NULL != pLPI ) ;
- if ( NULL == pLPI )
- {
- SetWindowText ( hwndDescription , szBuff ) ;
- return ;
- }
- }
- // Is this a root object?
- if ( 0 == pLPI->iType )
- {
- // If the leaf node did not fill out the description, we need
- // to use the root node.
- if ( TRUE == bShowRootNodeDescription )
- {
- VERIFY ( ::LoadString ( pLPI->hModule ,
- pLPI->stEC.uiDescription ,
- szBuff ,
- sizeof ( szBuff ) ) ) ;
- }
- }
- // Show the text.
- SetWindowText ( hwndDescription , szBuff ) ;
-
- // Enable or disable the Test Button based on the bEnableTestButton.
- hWnd = GetDlgItem ( hwndDlg , IDC_TEST ) ;
- ASSERT ( NULL != hWnd ) ;
- EnableWindow ( hWnd , bEnableTestButton ) ;
- hWnd = NULL ;
-
- // Always enable the 5 random button.
- hWnd = GetDlgItem ( hwndDlg , IDC_FIVERANDOM ) ;
- ASSERT ( NULL != hWnd ) ;
- EnableWindow ( hWnd , TRUE ) ;
- hWnd = NULL ;
-
- // Always turn on the Pro LED.
- hWnd = GetDlgItem ( hwndDlg , IDC_PROLED ) ;
- ASSERT ( NULL != hWnd ) ;
- hIcon = LoadIcon ( g_hInstance , MAKEINTRESOURCE ( IDI_LEDON ) ) ;
- ASSERT ( NULL != hIcon ) ;
- ::SendMessage( hWnd, STM_SETICON , ( WPARAM ) hIcon , 0L );
-
- // If this is an error that BC Personal supports, set the LED
- // for it to ON.
- hWnd = GetDlgItem ( hwndDlg , IDC_PERSLED ) ;
- ASSERT ( NULL != hWnd ) ;
-
- if ( TRUE == bShowPersonalLEDON )
- hIcon = LoadIcon ( g_hInstance , MAKEINTRESOURCE ( IDI_LEDON ) ) ;
- else
- hIcon = LoadIcon ( g_hInstance , MAKEINTRESOURCE ( IDI_LEDOFF ) ) ;
- ASSERT ( NULL != hIcon ) ;
- ::SendMessage( hWnd, STM_SETICON , ( WPARAM ) hIcon , 0L );
- //((CStatic*)pWnd)->SetIcon ( hIcon ) ;
-
- // Hide or show the product description text and LEDs.
- if ( TRUE == bShowLEDs )
- iProductLEDsShow = SW_SHOW ;
- else
- iProductLEDsShow = SW_HIDE ;
-
- hWnd = GetDlgItem ( hwndDlg , IDC_PRODSTR ) ;
- ASSERT ( NULL != hWnd ) ;
- ShowWindow ( hWnd , iProductLEDsShow ) ;
- hWnd = GetDlgItem ( hwndDlg ,IDC_PROSTR ) ;
- ASSERT ( NULL != hWnd ) ;
- ShowWindow ( hWnd , iProductLEDsShow ) ;
- hWnd = GetDlgItem ( hwndDlg ,IDC_PERSSTR ) ;
- ASSERT ( NULL != hWnd ) ;
- ShowWindow ( hWnd , iProductLEDsShow ) ;
- hWnd = GetDlgItem ( hwndDlg , IDC_PROLED ) ;
- ASSERT ( NULL != hWnd ) ;
- ShowWindow ( hWnd , iProductLEDsShow ) ;
- hWnd = GetDlgItem ( hwndDlg , IDC_PERSLED ) ;
- ASSERT ( NULL != hWnd ) ;
- ShowWindow ( hWnd , iProductLEDsShow ) ;
- }
-
-
- LRESULT MainDlg_OnNotify ( HWND hWnd , int nIdCtrl , LPNMHDR pnmh )
- {
- LRESULT nRes = 0 ;
-
- switch ( nIdCtrl )
- {
- // If it is the dialog's tree control.
- case IDC_TREELIST :
- {
- switch ( pnmh->code )
- {
- // When a tree control item is being
- // deleted, handle the notification.
- case TVN_DELETEITEM :
- {
- MainDlg_OnDeleteItemTreeList ( hWnd , pnmh , &nRes ) ;
- break ;
- }
-
- // When the tree control selection
- // has changed, update the data
- // on the dialog.
- case TVN_SELCHANGED :
- {
- MainDlg_OnSelChangedTreeList ( hWnd , pnmh , &nRes ) ;
- break ;
- }
-
- // When an item in the tree control
- // has been double-clicked.
- case NM_DBLCLK :
- {
- MainDlg_OnDblClkTreeList ( hWnd , pnmh , &nRes ) ;
- break ;
- }
-
- } // End switch on notification type
-
- break ;
-
- } // End tree control case
-
- } // End switch on the control
-
- return nRes ;
- }
-
- LRESULT MainDlg_OnContextMenu ( HWND hWnd , WPARAM wParam , LPARAM lParam )
- {
- // We want to only allow the popup menu to show up in the Tree
- // control client rect.
- RECT cTreeRect ;
- POINT cPoint ;
- cPoint.x = LOWORD ( lParam ) ;
- cPoint.y = HIWORD ( lParam ) ;
-
- // Get the area that the Tree is in.
- GetClientRect ( g_hwndTree , &cTreeRect ) ;
- // Convert the mouse coordinates from screen coordinates to the
- // client coordinates of the Tree window.
- ScreenToClient ( g_hwndTree , &cPoint ) ;
- // If the right click is not in the Tree window, don't do anything.
- if ( FALSE == PtInRect ( &cTreeRect , cPoint ) )
- return ( FALSE ) ;
-
- // Load the menu from the resource.
- HMENU hLoadMenu ;
- hLoadMenu = LoadMenu ( g_hInstance , MAKEINTRESOURCE ( IDR_TRACKMENU ) ) ;
- VERIFY ( hLoadMenu != NULL ) ;
-
- // Now get the first popup because we use it to be the actual popup.
- HMENU hPopupMenu = GetSubMenu ( hLoadMenu , 0 ) ;
- ASSERT ( NULL != hPopupMenu ) ;
- if ( NULL == hPopupMenu )
- {
- return ( 0 ) ;
- }
-
- // We need to get the currently selected item in the Tree Control.
- // If it is a leaf node, then we can enable the Test item on the
- // menu provided that there is a function that the leaf node
- // can execute.
- LParamInfo * pInfo =
- (LParamInfo *)TreeCtrl_GetSelectedLParam ( g_hwndTree ) ;
- if ( ( 2 == pInfo->iType ) && ( NULL != pInfo->stEO.pFunc ) )
- {
- EnableMenuItem ( hPopupMenu , IDM_TEST , MF_BYCOMMAND ) ;
- }
-
- // Do the tracking.
- TrackPopupMenu ( hPopupMenu ,
- TPM_LEFTALIGN | TPM_LEFTBUTTON ,
- LOWORD(lParam) ,
- HIWORD(lParam) ,
- 0 ,
- hWnd ,
- NULL ) ;
-
- // If you load a menu, you must destroy it.
- DestroyMenu ( hLoadMenu ) ;
-
- return ( 0 ) ;
- }
-
- ///////////////////////////////////////////////////////////
- // The main dialog proc.
- BOOL CALLBACK Main_DlgProc ( HWND hDlg ,
- UINT uMsg ,
- WPARAM wParam ,
- LPARAM lParam )
- {
- BOOL fProcessed = TRUE;
-
- switch ( uMsg )
- {
- HANDLE_MSG ( hDlg , WM_INITDIALOG , MainDlg_OnInitDialog ) ;
- HANDLE_MSG ( hDlg , WM_COMMAND , MainDlg_OnCommand ) ;
- HANDLE_MSG ( hDlg , WM_SYSCOMMAND , MainDlg_OnSysCommand ) ;
- HANDLE_MSG ( hDlg , WM_NOTIFY , MainDlg_OnNotify ) ;
- HANDLE_MSG ( hDlg , WM_PAINT , MainDlg_OnPaint ) ;
- HANDLE_MSG ( hDlg , WM_QUERYDRAGICON , MainDlg_OnQueryDragIcon ) ;
- HANDLE_MSG ( hDlg , WM_PALETTECHANGED , MainDlg_OnPaletteChanged ) ;
- HANDLE_MSG ( hDlg , WM_QUERYNEWPALETTE , MainDlg_OnQueryNewPalette ) ;
- HANDLE_MSG ( hDlg , WM_ACTIVATE , MainDlg_OnActivate ) ;
- HANDLE_MSG ( hDlg , WM_CONTEXTMENU , MainDlg_OnContextMenu ) ;
-
- default :
- fProcessed = FALSE ;
- break ;
- }
- return ( fProcessed ) ;
- }
-
-
- // Utility function used to check what operating system we are running from.
- BOOL IsWinNT ( )
- {
- DWORD dwVersion;
-
- dwVersion = GetVersion();
-
- if (dwVersion < 0x80000000)
- {
- return ( TRUE ) ;
- }
- else
- {
- return( FALSE ) ;
- }
- }
-
-
- ///////////////////////////////////////////////////////////
- // Parse the command line
- void ParseCmdLine( LPSTR lpszCmdLine )
- {
- char * cmdLineCopy = new char[strlen( lpszCmdLine ) + 1];
- strcpy( cmdLineCopy, lpszCmdLine );
- char seps[] = " -";
-
- /* Establish string and get the first token: */
- char * token = strtok( cmdLineCopy, seps );
- while( token != NULL )
- {
- // -s means skip the initial message box
- if( token[0] == 's' )
- g_fSkipMsgBox = TRUE ;
-
- // -cxxx means execute the the specified error and return without
- // ever displaying any part of the GUI including the info msg box.
- else if( token[0] == 'c' )
- {
- g_nRunError = atoi( &token[1] );
- g_fSkipMsgBox = TRUE ;
- }
-
- /* Get next token: */
- token = strtok( NULL, seps );
- }
- delete [] cmdLineCopy;
- }
-
- ///////////////////////////////////////////////////////////
- // The trusty ol' WinMain.
- int APIENTRY WinMain ( HINSTANCE hInstance ,
- HINSTANCE hPrevInstance ,
- LPSTR lpszCmdLine ,
- int nCmdShow )
- {
- #ifndef BCBVER
- // Turn off new CRT library assertion message boxes.
- _CrtSetReportMode ( _CRT_ASSERT , _CRTDBG_MODE_DEBUG ) ;
- _CrtSetReportMode ( _CRT_ERROR , _CRTDBG_MODE_DEBUG ) ;
- _CrtSetReportMode ( _CRT_WARN , _CRTDBG_MODE_DEBUG ) ;
-
- #endif
-
- g_hInstance = hInstance ;
-
- ParseCmdLine( lpszCmdLine );
-
- // Initialize OLE.
- HRESULT hRes = OleInitialize ( NULL ) ;
- if ( FAILED ( hRes ) )
- {
- MessageBox ( NULL , "OLE could not be initialized." , "BugBench Initialization Error" , MB_APPLMODAL | MB_OK ) ;
- return -1 ;
- }
-
- InitCommonControls ( ) ;
-
- // Register 3D Controls
- if ( IsWinNT ( ) )
- {
- Ctl3dRegister ( hInstance ) ;
- Ctl3dAutoSubclass ( hInstance ) ;
- }
-
- // Dynamically register the Interface Test Object that will be used by the
- // error generating OLE code.
- if ( ! RegisterInterfaceTestObject ( ) )
- {
- TCHAR * szMsg = _T ( "Unable to register the Interface Test Object.\n"
- "OLE errors will not be generated.\n"
- "Please re-install the BoundsChecker samples." ) ;
- MessageBox ( NULL , szMsg , constAPPNAME , MB_APPLMODAL | MB_OK ) ;
- }
-
- DialogBox ( hInstance ,
- MAKEINTRESOURCE ( DLG_BBENCH ) ,
- NULL ,
- ( DLGPROC ) Main_DlgProc ) ;
-
- // Unregister the 3D Controls
- if ( IsWinNT ( ) )
- {
- Ctl3dUnregister ( hInstance ) ;
- }
-
- // Let's be polite and clean-up the registry entries we made.
- UnregisterInterfaceTestObject ( ) ;
-
- return ( 0 ) ;
- }
-
-
- ///////////////////////////////////////////////////////////
- // Center a window.
- void CenterWindow ( HWND hWnd )
- {
- RECT rect ;
- WORD wWidth ;
- WORD wHeight ;
-
- GetWindowRect ( hWnd , &rect ) ;
-
- wWidth = GetSystemMetrics ( SM_CXSCREEN ) ;
- wHeight = GetSystemMetrics ( SM_CYSCREEN ) ;
-
- MoveWindow( hWnd ,
- (wWidth/2) - ((rect.right - rect.left)/2) ,
- (wHeight/2) - ((rect.bottom - rect.top) /2) ,
- rect.right - rect.left ,
- rect.bottom - rect.top ,
- FALSE ) ;
- }
-
-
- // Get the application name
- LPCTSTR GetAppName ( )
- {
- return constAPPNAME ;
- }
-
-
-
-