MessageBox does not work in a catch of an exception
Well I have 2 issues but my main concern right now is my catch exception. Here is the code...
int GXRenderManager::Ignite(HINSTANCE * hinst, int * nCmd, GXDEVICE DeviceType, int width, int height)
{
try
{
GXRenderManager::hinstance = hinst;
GXRenderManager::nCmdShow = nCmd;
GXRenderManager::height = height;
GXRenderManager::width = width;
InitWindows();
switch(DeviceType)
{
case DIRECTX:
GXRenderManager::renderDevice = new GXDX;
break;
case OPENGL:
GXRenderManager::renderDevice = new GXGL;
break;
default:
throw GXException(L"Error Finding Video Device");
}
Device()->StartUp(GXRenderManager::mainWindow ,width, height); //Error happens here
}
catch(GXVideoException &e)
{
MessageBox(0,e.pReason,L"GXVideoException",1);//Catch happens but no message box
return 0;
}
catch(GXWindowsException &e)
{
MessageBox(0,e.pReason,L"Windows Error",1);
return 0;
}
catch(GXException &e)
{
MessageBox(0,e.pReason,L"Error",1);
return 0;
}
return 1;
}
Here is where the error happens
void GXDX::StartUp(HWND* mainWindow,int w, int h)
{
width = w;
height = h;
this->mainWindow = mainWindow;
ID3D10Texture2D *backBufferSurface;
DXGI_SWAP_CHAIN_DESC swapChainDesc;
swapChainDesc.BufferCount = 2;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Qu开发者_运维问答ality = 0;
swapChainDesc.OutputWindow = *mainWindow;
swapChainDesc.Windowed = TRUE;
D3D10_DRIVER_TYPE driverType = D3D10_DRIVER_TYPE_HARDWARE;
HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL,driverType,NULL,0,
D3D10_SDK_VERSION, &swapChainDesc,&swapChain,&dxDevice);
if(FAILED(hr))
throw GXVideoException(L"Problems retrieving directX device");
}
When I do a walk through. the D3D10CreateDeviceAndSwapChain returns a failure, therefore triggering the GXVideoException error.
It then catches and returns back to GXRenderManager class as shown below.
catch(GXVideoException &e)
{
MessageBox(0,e.pReason,L"GXVideoException",1);
return 0;
}
At this point in time, If I put my cursor over the &e, I clearly see my message "Problems retrieving directX device". But the message box does not show
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
if(GXRenderManager::Ignite(&hInstance, &nCmdShow,DIRECTX) != 1)
return 0;
//NEVER REACHES THE RUN METHOD BELOW YET THE MAIN WINDOW REMAINS OPEN
GXRenderManager::Run();
return 0;
}
Another thing I find strange is that my created window remains showing but never reaches the main loop. It is like the application is idle due to the message box, but the message box is not showing...
I also would like to add that the static member renderDevice is a interface datatype. GXDX is of course a implemented class of the interface.
GXDX class includes the GXException Header so it is able to throw those exceptions so the main GXRenderManager can catch them.
[EDIT]
Another thing I would like to add is if I remove the Show window method
ShowWindow(*GXRenderManager::mainWindow, *GXRenderManager::nCmdShow);
It works. So as long as my main application window is not open. My message box appears like its suppose to.
[EDIT]
Prior to dauphic response which fixed part of the problem, I went on and edited my code. My catch now looks like this
catch(GXVideoException &e)
{
MessageBox(*GXRenderManager::mainWindow,e.pReason,L"GXVideoException",1);
return 0;
}
But now my application opens and then immediately closes without displaying the box. mainWindow is a pointer to my base window. So I had to dereference the pointer.
[EDIT]
The windows pointer is bad
If a dialog is present, MessageBox should always be passed a handle to it, rather than 0. Only pass MessageBox 0 if no dialog is available.
Side note, I don't understand the exact reason for this, so it would be great if someone else could give insight.
It's also possible that your message box is being attached to your dialog, and because your dialog isn't active, the message box isn't showing. Pressing alt when it hangs may cause it to show.
精彩评论