-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwin.cpp
123 lines (105 loc) · 2.95 KB
/
mainwin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <windows.h>
#include <io.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <list>
#include <memory>
std::string moduleExeBaseName( void )
{
DWORD l = MAX_PATH;
std::unique_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 );
}