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