Commit 3b2e1c50 authored by chili's avatar chili
Browse files

some basic windows error checks

parent e5933fa6
......@@ -61,7 +61,7 @@ HINSTANCE Window::WindowClass::GetInstance() noexcept
// Window Stuff
Window::Window( int width,int height,const char* name ) noexcept
Window::Window( int width,int height,const char* name )
{
// calculate window size based on desired client region size
RECT wr;
......@@ -69,7 +69,10 @@ Window::Window( int width,int height,const char* name ) noexcept
wr.right = width + wr.left;
wr.top = 100;
wr.bottom = height + wr.top;
AdjustWindowRect( &wr,WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,FALSE );
if( FAILED( AdjustWindowRect( &wr,WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,FALSE ) ) )
{
throw CHWND_LAST_EXCEPT();
};
// create window & get hWnd
hWnd = CreateWindow(
WindowClass::GetName(),name,
......@@ -77,6 +80,11 @@ Window::Window( int width,int height,const char* name ) noexcept
CW_USEDEFAULT,CW_USEDEFAULT,wr.right - wr.left,wr.bottom - wr.top,
nullptr,nullptr,WindowClass::GetInstance(),this
);
// check for error
if( hWnd == nullptr )
{
throw CHWND_LAST_EXCEPT();
}
// show window
ShowWindow( hWnd,SW_SHOWDEFAULT );
}
......@@ -135,7 +143,7 @@ Window::Exception::Exception( int line,const char * file,HRESULT hr ) noexcept
hr( hr )
{}
const char * Window::Exception::what() const noexcept
const char* Window::Exception::what() const noexcept
{
std::ostringstream oss;
oss << GetType() << std::endl
......@@ -154,17 +162,21 @@ const char* Window::Exception::GetType() const noexcept
std::string Window::Exception::TranslateErrorCode( HRESULT hr ) noexcept
{
char* pMsgBuf = nullptr;
// windows will allocate memory for err string and make our pointer point to it
DWORD nMsgLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,hr,MAKELANGID( LANG_NEUTRAL,SUBLANG_DEFAULT ),
reinterpret_cast<LPSTR>(&pMsgBuf),0,nullptr
);
// 0 string length returned indicates a failure
if( nMsgLen == 0 )
{
return "Unidentified error code";
}
// copy error string from windows-allocated buffer to std::string
std::string errorString = pMsgBuf;
// free windows buffer
LocalFree( pMsgBuf );
return errorString;
}
......
......@@ -54,7 +54,7 @@ private:
HINSTANCE hInst;
};
public:
Window( int width,int height,const char* name ) noexcept;
Window( int width,int height,const char* name );
~Window();
Window( const Window& ) = delete;
Window& operator=( const Window& ) = delete;
......@@ -70,4 +70,5 @@ private:
// error exception helper macro
#define CHWND_EXCEPT( hr ) Window::Exception( __LINE__,__FILE__,hr )
\ No newline at end of file
#define CHWND_EXCEPT( hr ) Window::Exception( __LINE__,__FILE__,hr )
#define CHWND_LAST_EXCEPT() Window::Exception( __LINE__,__FILE__,GetLastError() )
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment