Skip to content

Commit

Permalink
[Win] Show error message if mainwin fails to load exe
Browse files Browse the repository at this point in the history
Currently it will just die quietly which is no good for the user.
  • Loading branch information
NathanW2 committed Jan 29, 2018
1 parent cc9f7af commit 0be37ad
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/app/mainwin.cpp
Expand Up @@ -7,6 +7,18 @@
#include <list>
#include <memory>

void showError( std::string message, std::string title )
{
std::string newmessage = "Opps, looks like an error loading QGIS \n\n Details: \n\n" + message;
MessageBox(
NULL,
newmessage.c_str(),
title.c_str(),
MB_ICONERROR | MB_OK
);
std::cerr << message << std::endl;
}

std::string moduleExeBaseName( void )
{
DWORD l = MAX_PATH;
Expand Down Expand Up @@ -54,7 +66,8 @@ int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdL
}
catch ( std::ifstream::failure e )
{
std::cerr << "could read environment variable list " << basename + ".vars" << " [" << e.what() << "]" << std::endl;
std::string message = "Could not read environment variable list " + basename + ".vars" + " [" + e.what() + "]";
showError( message, "Error loading QGIS" );
return EXIT_FAILURE;
}

Expand All @@ -71,7 +84,8 @@ int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdL
}
catch ( std::ifstream::failure e )
{
std::cerr << "could not write environment file " << basename + ".env" << " [" << e.what() << "]" << std::endl;
std::string message = "Could not write environment file " + basename + ".env" + " [" + e.what() + "]";
showError( message, "Error loading QGIS" );
return EXIT_FAILURE;
}
}
Expand All @@ -93,14 +107,16 @@ int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdL
{
if ( _putenv( var.c_str() ) < 0 )
{
std::cerr << "could not set environment variable:" << var << std::endl;
std::string message = "Could not set environment variable:" + var;
showError( message, "Error loading QGIS" );
return EXIT_FAILURE;
}
}
}
catch ( std::ifstream::failure e )
{
std::cerr << "could not read environment file " << basename + ".env" << " [" << e.what() << "]" << std::endl;
std::string message = "Could not read environment file " + basename + ".env" + " [" + e.what() + "]";
showError( message, "Error loading QGIS" );
return EXIT_FAILURE;
}
}
Expand All @@ -114,14 +130,31 @@ int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdL

if ( !hGetProcIDDLL )
{
std::cerr << "Could not load the qgis_app.dll" << std::endl;
DWORD error = GetLastError();
LPTSTR errorText = NULL;

FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
( LPTSTR )&errorText,
0,
NULL );

std::string messsage = "Could not load qgis_app.dll \n Windows Error: " + std::string( errorText )
+ "\n Help: \n\n Check " + basename + ".env for correct environment paths";
showError( messsage, "Error loading QGIS" );

LocalFree( errorText );
errorText = NULL;
return EXIT_FAILURE;
}

int ( *realmain )( int, char *[] ) = ( int ( * )( int, char *[] ) ) GetProcAddress( hGetProcIDDLL, "main" );
if ( !realmain )
{
std::cerr << "could not locate main function in qgis_app.dll" << std::endl;
showError( "Could not locate main function in qgis_app.dll", "Error loading QGIS" );
return EXIT_FAILURE;
}

Expand Down

0 comments on commit 0be37ad

Please sign in to comment.