forked from ttgzs/iedexplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
108 lines (97 loc) · 3.73 KB
/
Program.cs
File metadata and controls
108 lines (97 loc) · 3.73 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
/*
* Copyright (C) 2013 Pavel Charvat
*
* This file is part of IEDExplorer.
*
* IEDExplorer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* IEDExplorer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with IEDExplorer. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Threading;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using PcapDotNet.Core;
namespace IEDExplorer
{
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
string resPrefix = "IEDExplorer.Embed.";
string filename1 = "iec61850dotnet.dll";
string filename2 = "iec61850.dll";
string filename3 = "PcapDotNet.Base.dll";
string filename4 = "PcapDotNet.Core.dll";
string filename5 = "PcapDotNet.Core.Extensions.dll";
string filename6 = "PcapDotNet.Packets.dll";
EmbeddedAssembly.Load(resPrefix + filename1, filename1);
EmbeddedAssembly.Load(resPrefix + filename2, filename2);
EmbeddedAssembly.Load(resPrefix + filename3, filename3);
EmbeddedAssembly.Load(resPrefix + filename4, filename4);
EmbeddedAssembly.Load(resPrefix + filename5, filename5);
EmbeddedAssembly.Load(resPrefix + filename6, filename6);
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Env env = new Env();
Application.Run(new Views.MainWindow());
List<string> natives = new List<string>();
natives.Add(filename2);
//natives.Add(filename4);
foreach (string filename in natives)
{
string path2 = null;
try
{
path2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename);
if (File.Exists(path2))
File.Delete(path2);
}
catch
{
try
{
//AppDomain.CurrentDomain.DomainManager. //.Unload(AppDomain.CurrentDomain);
UnloadImportedDll(path2);
Thread.Sleep(100);
File.Delete(path2);
}
catch { }
}
}
}
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
return EmbeddedAssembly.Get(args.Name);
}
[DllImport("kernel32", SetLastError = true)]
private static extern bool FreeLibrary(IntPtr hModule);
public static void UnloadImportedDll(string DllPath)
{
foreach (System.Diagnostics.ProcessModule mod in System.Diagnostics.Process.GetCurrentProcess().Modules)
{
if (mod.FileName.ToUpper() == DllPath.ToUpper())
{
FreeLibrary(mod.BaseAddress);
}
}
}
}
}