Repository navigation
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
214 lines (185 loc) · 7.61 KB
/
Copy pathApp.xaml.cs
File metadata and controls
214 lines (185 loc) · 7.61 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Threading;
namespace RSImageProcessor
{
public partial class App : Application
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetDllDirectory(string lpPathName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetEnvironmentVariable(string lpName, string lpValue);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr LoadLibrary(string lpLibFileName);
// Module initializer - runs before ANYTHING else
[ModuleInitializer]
internal static void InitializeModule()
{
SetupEnvironment();
}
// Static constructor as backup
static App()
{
SetupEnvironment();
}
private static bool _environmentSetup = false;
private static void SetupEnvironment()
{
if (_environmentSetup) return;
_environmentSetup = true;
try
{
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
// Remove trailing backslash if present
baseDir = baseDir.TrimEnd('\\', '/');
// 1. Set DLL directory
SetDllDirectory(baseDir);
// 2. Prepend to PATH
string currentPath = Environment.GetEnvironmentVariable("PATH") ?? "";
string newPath = baseDir + ";" + currentPath;
SetEnvironmentVariable("PATH", newPath);
Environment.SetEnvironmentVariable("PATH", newPath);
// 3. Set GDAL_DATA
string gdalData = Path.Combine(baseDir, "gdal-data");
if (Directory.Exists(gdalData))
{
SetEnvironmentVariable("GDAL_DATA", gdalData);
Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);
}
else
{
// Try alternate location
gdalData = Path.Combine(baseDir, "maxrev.gdal.core.libshared", "gdal-data");
if (Directory.Exists(gdalData))
{
SetEnvironmentVariable("GDAL_DATA", gdalData);
Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);
}
}
// 4. Set PROJ_LIB
string projData = Path.Combine(baseDir, "proj-data");
if (Directory.Exists(projData))
{
SetEnvironmentVariable("PROJ_LIB", projData);
Environment.SetEnvironmentVariable("PROJ_LIB", projData);
}
else
{
projData = Path.Combine(baseDir, "maxrev.gdal.core.libshared", "proj-data");
if (Directory.Exists(projData))
{
SetEnvironmentVariable("PROJ_LIB", projData);
Environment.SetEnvironmentVariable("PROJ_LIB", projData);
}
}
// 5. Disable plugin search
SetEnvironmentVariable("GDAL_DRIVER_PATH", "disable");
Environment.SetEnvironmentVariable("GDAL_DRIVER_PATH", "disable");
// 6. Pre-load critical DLLs in order
string[] dllsToLoad = { "gdal.dll", "gdal_wrap.dll" };
foreach (var dll in dllsToLoad)
{
string dllPath = Path.Combine(baseDir, dll);
if (File.Exists(dllPath))
{
LoadLibrary(dllPath);
}
}
}
catch
{
// Silently ignore - will fail later with better error message
}
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
DispatcherUnhandledException += OnDispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += OnDomainUnhandledException;
InitializeGdal();
}
private void InitializeGdal()
{
try
{
Core.GdalManager.Initialize();
}
catch (Exception ex)
{
string errorMsg = ex.Message;
if (ex.InnerException != null)
errorMsg += "\n" + ex.InnerException.Message;
bool needsVCRuntime = errorMsg.Contains("0x8007007E") ||
errorMsg.Contains("Unable to load DLL") ||
errorMsg.Contains("找不到指定的模块");
string message;
if (needsVCRuntime)
{
message = "程序需要 Visual C++ 运行库才能运行。\n\n" +
"请下载安装:\n" +
"https://aka.ms/vs/17/release/vc_redist.x64.exe\n\n" +
"安装后重新运行程序。\n\n" +
"点击\"是\"打开下载页面。";
}
else
{
message = $"GDAL初始化失败:\n{errorMsg}\n\n程序部分功能可能不可用。";
}
var result = MessageBox.Show(
message,
"初始化错误",
needsVCRuntime ? MessageBoxButton.YesNo : MessageBoxButton.OK,
MessageBoxImage.Warning);
if (needsVCRuntime && result == MessageBoxResult.Yes)
{
try
{
Process.Start(new ProcessStartInfo
{
FileName = "https://aka.ms/vs/17/release/vc_redist.x64.exe",
UseShellExecute = true
});
}
catch { }
// Exit after opening download page
Environment.Exit(0);
}
// Write error log
WriteErrorLog(ex);
}
}
private void WriteErrorLog(Exception ex)
{
try
{
string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "gdal_error.log");
string content = Core.GdalManager.GetDiagnosticInfo() + "\n\nException:\n" + ex;
File.WriteAllText(logPath, content);
}
catch { }
}
protected override void OnExit(ExitEventArgs e)
{
Core.GdalManager.Cleanup();
base.OnExit(e);
}
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show($"错误:{e.Exception.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
e.Handled = true;
}
private void OnDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
{
MessageBox.Show($"严重错误:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}