Archived

This topic is now archived and is closed to further replies.

sdoherty55

Trapping Errors within the DirectX 8.x Framework

Recommended Posts

Recently, I have been trying to clean up my code by checking for any possible errors, calling framework function "DisplayErrorMsg", and returning the appropriate response. The following is an example of the code within the "HRESULT CMyD3DApplication::InitDeviceObjects()" method: --------------------------------------------- if( FAILED(m_oRed.mesh->Create( m_pd3dDevice, _T "planet.x") ) ) ){ DisplayErrorMsg( hr, MSGERR_APPMUSTEXIT ); return E_FAIL; } --------------------------------------------- However, I have noticed that after the message box is displayed, the code never seeems to call the "return E_FAIL; " statement. Instead the code ends up calling the following. where it will not exit cleanly: "d:\...\vc98\include\LIST" Please explain why this is happening? The folling is the framework function "DisplayErrorMsg": //----------------------------------------------------------------------------- // Name: DisplayErrorMsg() // Desc: Displays error messages in a message box //----------------------------------------------------------------------------- HRESULT CD3DApplication::DisplayErrorMsg( HRESULT hr, DWORD dwType ) { TCHAR strMsg[512]; switch( hr ) { case D3DAPPERR_NODIRECT3D: _tcscpy( strMsg, _T("Could not initialize Direct3D. You may\n") _T("want to check that the latest version of\n") _T("DirectX is correctly installed on your\n") _T("system. Also make sure that this program\n") _T("was compiled with header files that match\n") _T("the installed DirectX DLLs.") ); break; case D3DAPPERR_NOCOMPATIBLEDEVICES: _tcscpy( strMsg, _T("Could not find any compatible Direct3D\n") _T("devices.") ); break; case D3DAPPERR_NOWINDOWABLEDEVICES: _tcscpy( strMsg, _T("This sample cannot run in a desktop\n") _T("window with the current display settings.\n") _T("Please change your desktop settings to a\n") _T("16- or 32-bit display mode and re-run this\n") _T("sample.") ); break; case D3DAPPERR_NOHARDWAREDEVICE: _tcscpy( strMsg, _T("No hardware-accelerated Direct3D devices\n") _T("were found.") ); break; case D3DAPPERR_HALNOTCOMPATIBLE: _tcscpy( strMsg, _T("This sample requires functionality that is\n") _T("not available on your Direct3D hardware\n") _T("accelerator.") ); break; case D3DAPPERR_NOWINDOWEDHAL: _tcscpy( strMsg, _T("Your Direct3D hardware accelerator cannot\n") _T("render into a window.\n") _T("Press F2 while the app is running to see a\n") _T("list of available devices and modes.") ); break; case D3DAPPERR_NODESKTOPHAL: _tcscpy( strMsg, _T("Your Direct3D hardware accelerator cannot\n") _T("render into a window with the current\n") _T("desktop display settings.\n") _T("Press F2 while the app is running to see a\n") _T("list of available devices and modes.") ); break; case D3DAPPERR_NOHALTHISMODE: _tcscpy( strMsg, _T("This sample requires functionality that is\n") _T("not available on your Direct3D hardware\n") _T("accelerator with the current desktop display\n") _T("settings.\n") _T("Press F2 while the app is running to see a\n") _T("list of available devices and modes.") ); break; case D3DAPPERR_MEDIANOTFOUND: _tcscpy( strMsg, _T("Could not load required media." ) ); break; case D3DAPPERR_RESIZEFAILED: _tcscpy( strMsg, _T("Could not reset the Direct3D device." ) ); break; case D3DAPPERR_NONZEROREFCOUNT: _tcscpy( strMsg, _T("A D3D object has a non-zero reference\n") _T("count (meaning things were not properly\n") _T("cleaned up).") ); break; case D3DAPPERR_NULLREFDEVICE: _tcscpy( strMsg, _T("Warning: Nothing will be rendered.\n") _T("The reference rendering device was selected, but your\n") _T("computer only has a reduced-functionality reference device\n") _T("installed. Install the DirectX SDK to get the full\n") _T("reference device.\n") ); break; case E_OUTOFMEMORY: _tcscpy( strMsg, _T("Not enough memory.") ); break; case D3DERR_OUTOFVIDEOMEMORY: _tcscpy( strMsg, _T("Not enough video memory.") ); break; default: _tcscpy( strMsg, _T("Generic application error. Enable\n") _T("debug output for detailed information.") ); } if( MSGERR_APPMUSTEXIT == dwType ) { _tcscat( strMsg, _T("\n\nThis sample will now exit.") ); MessageBox( NULL, strMsg, m_strWindowTitle, MB_ICONERROR|MB_OK ); // Close the window, which shuts down the app if( m_hWnd ) SendMessage( m_hWnd, WM_CLOSE, 0, 0 ); } else { if( MSGWARN_SWITCHEDTOREF == dwType ) _tcscat( strMsg, _T("\n\nSwitching to the reference rasterizer,\n") _T("a software device that implements the entire\n") _T("Direct3D feature set, but runs very slowly.") ); MessageBox( NULL, strMsg, m_strWindowTitle, MB_ICONWARNING|MB_OK ); } return hr; }

Share this post


Link to post
Share on other sites
The reason E_FAIL is not being returned in because DisplayErrorMsg is telling the controlling window to close. Take a look again at the following code

  
if( MSGERR_APPMUSTEXIT == dwType )
{
_tcscat( strMsg, _T("\n\nThis sample will now exit.") );
MessageBox( NULL, strMsg, m_strWindowTitle, MB_ICONERROR|MB_OK );

// Close the window, which shuts down the app

if( m_hWnd )
SendMessage( m_hWnd, WM_CLOSE, 0, 0 );
}
else



The comment says it all. Once this message is sent, all control of the program returns back to windows and execution stop. If you want E_FAIL to be returned, then you should look for an E_FAIL in the calling function, ( presumably Run() or Main() ), then call DisplayErrorMsg there.

-----------------------------
kevin@mayday-anime.com
http://games.mayday-anime.com

Share this post


Link to post
Share on other sites