Skip to content
Permalink
Browse files

osgeo4w: add a minimal exe wrapper to initialize the enviroment and load

qgis_app.dll (inspired by Nathan's PR #4156)

On install each shortcut/configuration gets a own copy of the (small)
exe.  That exe is started through the batch file that originally was
used to start the app at runtime. Argument --exit will cause the wrapper
to just dump the environment to an .env file next to the copy of the
.exe.  The created shortcuts now point at those .exes instead of the
batch files.

At runtime the .exes source their .env file to get a working environment
to run QGIS (ie. qgis_app.dll).  This for example allows the application
to be pinned to the taskbar and avoids the of necessity of batch files
at runtime to start the app.
  • Loading branch information
jef-n committed Feb 19, 2017
1 parent 32504b1 commit 801184a8a14d925a68d56b8b4dabed1673ab3447
@@ -62,6 +62,7 @@ call "%PF86%\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86
path %path%;%PF86%\Microsoft Visual Studio 14.0\VC\bin
set CMAKE_COMPILER_PATH=%PF86%\Microsoft Visual Studio 14.0\VC\bin
set SETUPAPI_LIBRARY=c:\Program Files (x86)\Windows Kits\10\Lib\10.0.14393.0\um\x86\SetupAPI.Lib
if not exist "%SETUPAPI_LIBRARY%" set SETUPAPI_LIBRARY=c:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86\SetupAPI.Lib
if not exist "%SETUPAPI_LIBRARY%" (echo SETUPAPI_LIBRARY not found & goto error)

set CMAKE_OPT=^
@@ -77,6 +78,7 @@ call "%PF86%\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
path %path%;%PF86%\Microsoft Visual Studio 14.0\VC\bin
set CMAKE_COMPILER_PATH=%PF86%\Microsoft Visual Studio 14.0\VC\bin\amd64
set SETUPAPI_LIBRARY=c:\Program Files (x86)\Windows Kits\10\Lib\10.0.14393.0\um\x64\SetupAPI.Lib
if not exist "%SETUPAPI_LIBRARY%" set SETUPAPI_LIBRARY=c:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x64\SetupAPI.Lib
if not exist "%SETUPAPI_LIBRARY%" (echo SETUPAPI_LIBRARY not found & goto error)

set CMAKE_OPT=^
@@ -292,6 +294,8 @@ touch exclude

move %PKGDIR%\bin\qgis.exe %OSGEO4W_ROOT%\bin\%PACKAGENAME%-bin.exe
if errorlevel 1 (echo move of desktop executable failed & goto error)
copy qgis.vars %OSGEO4W_ROOT%\bin\%PACKAGENAME%-bin.vars
if errorlevel 1 (echo copy of desktop executable vars failed & goto error)
move %PKGDIR%\bin\qbrowser.exe %OSGEO4W_ROOT%\bin\%PACKAGENAME%-browser-bin.exe
if errorlevel 1 (echo move of browser executable failed & goto error)

@@ -356,6 +356,8 @@ if errorlevel 1 (echo tar server failed & goto error)

move %PKGDIR%\bin\qgis.exe %OSGEO4W_ROOT%\bin\%PACKAGENAME%-bin.exe
if errorlevel 1 (echo move of desktop executable failed & goto error)
copy qgis.vars %OSGEO4W_ROOT%\bin\%PACKAGENAME%-bin.vars
if errorlevel 1 (echo copy of desktop executable vars failed & goto error)
move %PKGDIR%\bin\qbrowser.exe %OSGEO4W_ROOT%\bin\%PACKAGENAME%-browser-bin.exe
if errorlevel 1 (echo move of browser executable failed & goto error)

@@ -7,14 +7,18 @@ REM get short path without blanks
for %%i in ("%OSGEO4W_ROOT%") do set O4W_ROOT=%%~fsi
if "%OSGEO4W_DESKTOP%"=="" set OSGEO4W_DESKTOP=~$folder.common_desktop$

if not %OSGEO4W_MENU_LINKS%==0 mkdir "%OSGEO4W_STARTMENU%"
if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%O4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_STARTMENU%" "QGIS Desktop @version@" "exec hide %O4W_ROOT%\bin\@package@.bat" "%O4W_ROOT%\apps\@package@\icons\QGIS.ico"
if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%O4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_STARTMENU%" "QGIS Browser @version@" "exec hide %O4W_ROOT%\bin\@package@-browser.bat" "%O4W_ROOT%\apps\@package@\icons\browser.ico"
if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%O4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_STARTMENU%" "Qt Designer with QGIS @version@ custom widgets" "exec hide """%OSGEO4W_ROOT%\bin\@package@-designer.bat"" "%O4W_ROOT%\apps\@package@\icons\QGIS.ico"
"%OSGEO4W_ROOT%\bin\@package@.bat" --exit

if not %OSGEO4W_MENU_LINKS%==0 mkdir "%OSGEO4W_STARTMENU%"
if not %OSGEO4W_DESKTOP_LINKS%==0 mkdir "%OSGEO4W_DESKTOP%"
if not %OSGEO4W_DESKTOP_LINKS%==0 nircmd shortcut "%O4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_DESKTOP%" "QGIS Desktop @version@" "exec hide %O4W_ROOT%\bin\@package@.bat" "%O4W_ROOT%\apps\@package@\icons\QGIS.ico"

if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%O4W_ROOT%\bin\@package@-bin.exe" "%OSGEO4W_STARTMENU%" "QGIS Desktop @version@"
if not %OSGEO4W_DESKTOP_LINKS%==0 nircmd shortcut "%O4W_ROOT%\bin\package@-bin.exe" "%OSGEO4W_DESKTOP%" "QGIS Desktop @version@"

if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%O4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_STARTMENU%" "QGIS Browser @version@" "exec hide %O4W_ROOT%\bin\@package@-browser.bat" "%O4W_ROOT%\apps\@package@\icons\browser.ico"
if not %OSGEO4W_DESKTOP_LINKS%==0 nircmd shortcut "%O4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_DESKTOP%" "QGIS Browser @version@" "exec hide %O4W_ROOT%\bin\@package@-browser.bat" "%O4W_ROOT%\apps\@package@\icons\browser.ico"

if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%O4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_STARTMENU%" "Qt Designer with QGIS @version@ custom widgets" "exec hide """%OSGEO4W_ROOT%\bin\@package@-designer.bat"" "%O4W_ROOT%\apps\@package@\icons\QGIS.ico"
if not %OSGEO4W_DESKTOP_LINKS%==0 nircmd shortcut "%O4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_DESKTOP%" "Qt Designer with QGIS @version@ custom widgets" "exec hide %O4W_ROOT%\bin\@package@-designer.bat" "%O4W_ROOT%\apps\@package@\icons\QGIS.ico"

set OSGEO4W_ROOT=%OSGEO4W_ROOT:\=\\%
@@ -8,12 +8,18 @@ if not %OSGEO4W_DESKTOP_LINKS%==0 mkdir "%OSGEO4W_DESKTOP%"

for %%g in (@grassversions@) do (
textreplace -std -t bin\@package@-g%%g.bat
textreplace -std -t bin\@package@-browser-g%%g.bat

if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_STARTMENU%" "QGIS Desktop @version@ with GRASS %%g (Nightly)" "exec hide """%OSGEO4W_ROOT%\bin\@package@-g%%g.bat"" "%OSGEO4W_ROOT%\apps\@package@\icons\QGIS.ico"
if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_STARTMENU%" "QGIS Browser @version@ with GRASS %%g (Nightly)" "exec hide """%OSGEO4W_ROOT%\bin\@package@-browser-g%%g.bat"" "%OSGEO4W_ROOT%\apps\@package@\icons\browser.ico"
REM copy executable so we have one (now small) executable for each environment
REM and call it to produce .env from .vars
copy "%OSGEO4W_ROOT%\bin\@package@-bin.exe" "%OSGEO4W_ROOT%\bin\@package@-bin-g%%g.exe"
copy "%OSGEO4W_ROOT%\bin\@package@-bin.vars" "%OSGEO4W_ROOT%\bin\@package@-bin-g%%g.vars"
"%OSGEO4W_ROOT%\bin\@package@-g%%g.bat" --exit

if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\@package@-bin-g%%g.exe" "%OSGEO4W_STARTMENU%" "QGIS Desktop @version@ with GRASS %%g (Nightly)"
if not %OSGEO4W_DESKTOP_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\@package@-bin-g%%g.exe" "%OSGEO4W_DESKTOP%" "QGIS Desktop @version@ with GRASS %%g (Nightly)"

if not %OSGEO4W_DESKTOP_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_DESKTOP%" "QGIS Desktop @version@ with GRASS %%g (Nightly)" "exec hide """%OSGEO4W_ROOT%\bin\@package@-g%%g.bat"" "%OSGEO4W_ROOT%\apps\@package@\icons\QGIS.ico"
textreplace -std -t bin\@package@-browser-g%%g.bat
if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_STARTMENU%" "QGIS Browser @version@ with GRASS %%g (Nightly)" "exec hide """%OSGEO4W_ROOT%\bin\@package@-browser-g%%g.bat"" "%OSGEO4W_ROOT%\apps\@package@\icons\browser.ico"
if not %OSGEO4W_DESKTOP_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_DESKTOP%" "QGIS Browser @version@ with GRASS %%g (Nightly)" "exec hide """%OSGEO4W_ROOT%\bin\@package@-browser-g%%g.bat"" "%OSGEO4W_ROOT%\apps\@package@\icons\browser.ico"
)

@@ -3,10 +3,15 @@ textreplace -std -t bin\@package@-browser-grass@grassmajor@.bat

if "%OSGEO4W_DESKTOP%"=="" set OSGEO4W_DESKTOP=~$folder.common_desktop$

if not %OSGEO4W_MENU_LINKS%==0 mkdir "%OSGEO4W_STARTMENU%"
if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_STARTMENU%" "QGIS Desktop @version@ with GRASS @grassversion@" "exec hide %OSGEO4W_ROOT%\bin\@package@-grass@grassmajor@.bat" "%OSGEO4W_ROOT%\apps\@package@\icons\QGIS.ico"
if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_STARTMENU%" "QGIS Browser @version@ with GRASS @grassversion@" "exec hide %OSGEO4W_ROOT%\bin\@package@-browser-grass@grassmajor@.bat" "%OSGEO4W_ROOT%\apps\@package@\icons\browser.ico"
copy bin\@package@-bin.exe bin\@package@-bin-grass@grassmajor@.exe
copy bin\@package@-bin.vars bin\@package@-bin-grass@grassmajor@.vars
"%OSGEO4W_ROOT%\bin\@package@-grass@grassmajor@.bat" --exit

if not %OSGEO4W_MENU_LINKS%==0 mkdir "%OSGEO4W_STARTMENU%"
if not %OSGEO4W_DESKTOP_LINKS%==0 mkdir "%OSGEO4W_DESKTOP%"
if not %OSGEO4W_DESKTOP_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_DESKTOP%" "QGIS Desktop @version@ with GRASS @grassversion@" "exec hide %OSGEO4W_ROOT%\bin\@package@-grass@grassmajor@.bat" "%OSGEO4W_ROOT%\apps\@package@\icons\QGIS.ico"

if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\@package@-bin-grass@grassmajor@.exe" "%OSGEO4W_STARTMENU%" "QGIS Desktop @version@ with GRASS @grassversion@"
if not %OSGEO4W_DESKTOP_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\@package@-bin-grass@grassmajor@.exe" "%OSGEO4W_DESKTOP%" "QGIS Desktop @version@ with GRASS @grassversion@"

if not %OSGEO4W_MENU_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_STARTMENU%" "QGIS Browser @version@ with GRASS @grassversion@" "exec hide %OSGEO4W_ROOT%\bin\@package@-browser-grass@grassmajor@.bat" "%OSGEO4W_ROOT%\apps\@package@\icons\browser.ico"
if not %OSGEO4W_DESKTOP_LINKS%==0 nircmd shortcut "%OSGEO4W_ROOT%\bin\nircmd.exe" "%OSGEO4W_DESKTOP%" "QGIS Browser @version@ with GRASS @grassversion@" "exec hide %OSGEO4W_ROOT%\bin\@package@-browser-grass@grassmajor@.bat" "%OSGEO4W_ROOT%\apps\@package@\icons\browser.ico"
@@ -7,6 +7,8 @@ del "%OSGEO4W_DESKTOP%\QGIS Browser @version@.lnk"
del "%OSGEO4W_DESKTOP%\Qt Designer with QGIS @version@ custom widgets.lnk"
rmdir "%OSGEO4W_DESKTOP%"
del "%OSGEO4W_ROOT%\bin\@package@.bat"
del "%OSGEO4W_ROOT%\bin\@package@-bin.vars"
del "%OSGEO4W_ROOT%\bin\@package@-bin.env"
del "%OSGEO4W_ROOT%\bin\@package@-browser.bat"
del "%OSGEO4W_ROOT%\bin\@package@-designer.bat"
del "%OSGEO4W_ROOT%\apps\@package@\python\qgis\qgisconfig.py"
@@ -4,6 +4,9 @@ for %%g in (@grassversions@) do (
del "%OSGEO4W_DESKTOP%\QGIS Desktop @version@ with GRASS %%g (Nightly).lnk"
del "%OSGEO4W_DESKTOP%\QGIS Browser @version@ with GRASS %%g (Nightly).lnk"
del "%OSGEO4W_ROOT%\bin\@package@-g%%g.bat"
del "%OSGEO4W_ROOT%\bin\@package@-bin-g%%g.exe"
del "%OSGEO4W_ROOT%\bin\@package@-bin-g%%g.env"
del "%OSGEO4W_ROOT%\bin\@package@-bin-g%%g.vars"
del "%OSGEO4W_ROOT%\bin\@package@-browser-g%%g.bat"
)

@@ -12,6 +15,7 @@ rmdir "%OSGEO4W_STARTMENU%"
del "%OSGEO4W_DESKTOP%\Qt Designer with QGIS @version@ custom widgets (Nightly).lnk"
rmdir "%OSGEO4W_DESKTOP%"

del "%OSGEO4W_ROOT%\bin\@package@-bin.env"
del "%OSGEO4W_ROOT%\bin\@package@-designer.bat"
del "%OSGEO4W_ROOT%\bin\python-@package@.bat"
del "%OSGEO4W_ROOT%\apps\@package@\python\qgis\qgisconfig.py"
@@ -6,3 +6,6 @@ del "%OSGEO4W_DESKTOP%\QGIS Browser @version@ with GRASS @grassversion@.lnk"
rmdir "%OSGEO4W_DESKTOP%"
del "%OSGEO4W_ROOT%\bin\@package@-grass@grassmajor@.bat"
del "%OSGEO4W_ROOT%\bin\@package@-browser-grass@grassmajor@.bat"
del "%OSGEO4W_ROOT%\bin\@package@-bin-grass@grassmajor@.exe"
del "%OSGEO4W_ROOT%\bin\@package@-bin-grass@grassmajor@.vars"
del "%OSGEO4W_ROOT%\bin\@package@-bin-grass@grassmajor@.env"
@@ -11,4 +11,4 @@ rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\@package@\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
start "QGIS" /B "%OSGEO4W_ROOT%\bin\@package@-bin.exe" %*
start "QGIS" /B "%OSGEO4W_ROOT%\bin\@package@-bin-g@grassversion@.exe" %*
@@ -0,0 +1,22 @@
PATH
GDAL_DATA
GDAL_DRIVER_PATH
GDAL_FILENAME_IS_UTF8
GEOTIFF_CSV
GISBASE
GRASS_HTML_BROWSER
GRASS_PROJSHARE
GRASS_PYTHON
GRASS_SH
GRASS_WISH
JPEGMEM
NLS_LANG
OSGEO4W_ROOT
PROJ_LIB
PYTHONHOME
PYTHONPATH
QGIS_PREFIX_PATH
QT_PLUGIN_PATH
QT_RASTER_CLIP_LIMIT
VSI_CACHE
VSI_CACHE_SIZE
@@ -476,15 +476,15 @@ QT5_ADD_RESOURCES(TEST_RCC_SRCS ${TEST_RCCS})

QT5_WRAP_CPP(QGIS_APP_MOC_SRCS ${QGIS_APP_MOC_HDRS})

SET (QGIS_APPMAIN_SRCS main.cpp)

IF (WIN32)
FIND_PACKAGE(Qt5WinExtras)

SET (QGIS_APP_SRCS ${QGIS_APP_SRCS} main.cpp ${IMAGE_RCC_SRCS} ${TEST_RCC_SRCS})
SET (QGIS_APPMAIN_SRCS mainwin.cpp)

IF (MSVC)
SET (QGIS_APPMAIN_SRCS ${QGIS_APPMAIN_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/qgis_win32.rc)
ELSE(MSVC)

# Icon for windows MINGW
# Note: can't include .rc directly to source files
# as it's ignored when used MinGW
@@ -503,6 +503,8 @@ IF (WIN32)
-o ${CMAKE_CURRENT_BINARY_DIR}/icon.o )
SET(QGIS_APPMAIN_SRCS ${QGIS_APPMAIN_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/icon.o)
ENDIF (MSVC)
ELSE(WIN32)
SET (QGIS_APPMAIN_SRCS main.cpp ${IMAGE_RCC_SRCS} ${TEST_RCC_SRCS})
ENDIF (WIN32)

IF(PEDANTIC)
@@ -590,9 +592,9 @@ ENDIF (POSTGRES_FOUND)

#############
IF (ANDROID)
ADD_LIBRARY(${QGIS_APP_NAME} SHARED ${QGIS_APPMAIN_SRCS} ${IMAGE_RCC_SRCS} ${TEST_RCC_SRCS})
ADD_LIBRARY(${QGIS_APP_NAME} SHARED ${QGIS_APPMAIN_SRCS})
ELSE (ANDROID)
ADD_EXECUTABLE(${QGIS_APP_NAME} MACOSX_BUNDLE WIN32 ${QGIS_APPMAIN_SRCS} ${IMAGE_RCC_SRCS} ${TEST_RCC_SRCS})
ADD_EXECUTABLE(${QGIS_APP_NAME} MACOSX_BUNDLE WIN32 ${QGIS_APPMAIN_SRCS})
ENDIF (ANDROID)

# Putting IMAGE_RCC_SRCS into qgis_app lib is causing problems when the lib is
@@ -633,7 +635,9 @@ if(MSVC AND CMAKE_SIZEOF_VOID_P EQUAL 8)
SET_TARGET_PROPERTIES(qgis_app PROPERTIES STATIC_LIBRARY_FLAGS "/machine:x64")
ENDIF(MSVC AND CMAKE_SIZEOF_VOID_P EQUAL 8)

TARGET_LINK_LIBRARIES(${QGIS_APP_NAME} qgis_app)
IF(NOT WIN32)
TARGET_LINK_LIBRARIES(${QGIS_APP_NAME} qgis_app)
ENDIF(NOT WIN32)

IF (ANDROID)
TARGET_LINK_LIBRARIES(${QGIS_APP_NAME} log)
@@ -642,11 +646,11 @@ ENDIF (ANDROID)
IF(WIN32)
ADD_DEFINITIONS(-DQWT_DLL)
TARGET_LINK_LIBRARIES(${QGIS_APP_NAME} DbgHelp)
TARGET_LINK_LIBRARIES(qgis_app DbgHelp Qt5::WinExtras )
TARGET_LINK_LIBRARIES(qgis_app DbgHelp Qt5::WinExtras)
ENDIF(WIN32)

IF (APPLE)
TARGET_LINK_LIBRARIES(${QGIS_APP_NAME} ${APP_SERVICES_LIBRARY} )
TARGET_LINK_LIBRARIES(${QGIS_APP_NAME} ${APP_SERVICES_LIBRARY})

SET_TARGET_PROPERTIES(${QGIS_APP_NAME} PROPERTIES
INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/${QGIS_LIB_DIR}
@@ -660,7 +664,7 @@ IF (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
ENDIF (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")

IF (POSTGRES_FOUND)
TARGET_LINK_LIBRARIES (${QGIS_APP_NAME} ${POSTGRES_LIBRARY})
TARGET_LINK_LIBRARIES (qgis_app ${POSTGRES_LIBRARY})
ENDIF (POSTGRES_FOUND)

IF(MSVC)
@@ -423,9 +423,15 @@ void myMessageOutput( QtMsgType type, const char *msg )
}
}

#if(ANDROID)
#ifdef _MSC_VER
#undef APP_EXPORT
#define APP_EXPORT __declspec(dllexport)
#endif

#if defined(ANDROID) || defined(Q_OS_WIN)
// On Android, there there is a libqgis.so instead of a qgis executable.
// The main method symbol of this library needs to be exported so it can be called by java
// On Windows this main is included in qgis_app and called from mainwin.cpp
APP_EXPORT
#endif
int main( int argc, char *argv[] )
@@ -0,0 +1,122 @@
#include <windows.h>
#include <io.h>

#include <sstream>
#include <iostream>
#include <fstream>
#include <list>

std::string moduleExeBaseName( void )
{
DWORD l = MAX_PATH;
std::auto_ptr<char> filepath;
for ( ;; )
{
filepath.reset( new char[l] );
if ( GetModuleFileName( nullptr, filepath.get(), l ) < l )
break;

l += MAX_PATH;
}

std::string basename( filepath.get() );
basename.resize( basename.length() - 4 );
return basename;
}


int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
std::string basename( moduleExeBaseName() );

if ( getenv( "OSGEO4W_ROOT" ) )
{
std::string envfile( basename + ".env" );

// write or update environment file
if ( _access( envfile.c_str(), 0 ) < 0 || _access( envfile.c_str(), 2 ) == 0 )
{
std::list<std::string> vars;

try
{
std::ifstream varfile;
varfile.open( basename + ".vars" );

std::string var;
while ( std::getline( varfile, var ) )
{
vars.push_back( var );
}

varfile.close();
}
catch ( std::ifstream::failure e )
{
std::cerr << "could read environment variable list " << basename + ".vars" << " [" << e.what() << "]" << std::endl;
return EXIT_FAILURE;
}

try
{
std::ofstream file;
file.open( envfile, std::ifstream::out );

for ( std::list<std::string>::const_iterator it = vars.begin(); it != vars.end(); ++it )
{
if ( getenv( it->c_str() ) )
file << *it << "=" << getenv( it->c_str() ) << std::endl;
}
}
catch ( std::ifstream::failure e )
{
std::cerr << "could not write environment file " << basename + ".env" << " [" << e.what() << "]" << std::endl;
return EXIT_FAILURE;
}
}

if ( __argc == 2 && strcmp( __argv[1], "--exit" ) == 0 )
{
return EXIT_SUCCESS;
}
}
else
{
try
{
std::ifstream file;
file.open( basename + ".env" );

std::string var;
while ( std::getline( file, var ) )
{
if ( _putenv( var.c_str() ) < 0 )
{
std::cerr << "could not set environment variable:" << var << std::endl;
return EXIT_FAILURE;
}
}
}
catch ( std::ifstream::failure e )
{
std::cerr << "could not read environment file " << basename + ".env" << " [" << e.what() << "]" << std::endl;
return EXIT_FAILURE;
}
}

HINSTANCE hGetProcIDDLL = LoadLibrary( "qgis_app.dll" );
if ( !hGetProcIDDLL )
{
std::cerr << "Could not load the qgis_app.dll" << std::endl;
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;
return EXIT_FAILURE;
}

return realmain( __argc, __argv );
}

2 comments on commit 801184a

@nyalldawson

This comment has been minimized.

Copy link
Collaborator

@nyalldawson nyalldawson replied Feb 19, 2017

@jef-n the standalone browser will also need to be updated to match, right?

Can we use a similar approach with the python libraries? It'd be nice to only have to set the python path and not all the other env variables to be able to import qgis libraries...

@jef-n

This comment has been minimized.

Copy link
Member Author

@jef-n jef-n replied Feb 19, 2017

@nyalldawson done for the browser.

Please sign in to comment.
You can’t perform that action at this time.