-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntry.cs
173 lines (154 loc) · 5.88 KB
/
Entry.cs
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
/// <author creation-date="15 марта 2012 г.">
/// Закиров Артур
/// </author>
/// <summary>
/// Файл реализации экспортных методов плагина ЛОЦМАН
/// </summary>
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using DllExport;
using LoodsmanDotNet;
using System.Security.Permissions;
namespace ASCON.Loodsman.$safeprojectname$
{
/// <summary>
/// Класс для выполнения методов плагина в отдельном домене приложений
/// </summary>
public class PluginDomainWorker : MarshalByRefObject
{
public int PgiCheckMenuItemCom(IntPtr stFunction, IntPtr IPC)
{
if (IPC != null)
{
IPluginCall pc = (IPluginCall)Marshal.GetTypedObjectForIUnknown(IPC, typeof(IPluginCall));
string funcName = Marshal.PtrToStringAnsi(stFunction);
if (funcName == "RunModule")
if (pc.IdVersion != 0)
return 1;
}
return 0;
}
public void RunModule(IntPtr IPC)
{
if (IPC != null)
{
try
{
IPluginCall pc = (IPluginCall)Marshal.GetTypedObjectForIUnknown(IPC, typeof(IPluginCall));
// Работа модуля
// ...
MessageBox.Show("Hello, Loodsman!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
/// <summary>
/// Отключить контроль времени существования LifetimeService
/// </summary>
/// <returns></returns>
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
public override object InitializeLifetimeService()
{
return null;
}
}
/// <summary>
/// Класс Entry - класс, реализующий экспортные методы плагина ЛОЦМАН
/// </summary>
internal static class Entry
{
private static PluginDomainWorker worker;
private static AppDomain CreatePluginDomain()
{
var execAssembly = Assembly.GetExecutingAssembly();
Uri assemblyFileUri = new Uri(execAssembly.CodeBase);
var configFile = assemblyFileUri.LocalPath + ".config";
var appSetup = new AppDomainSetup()
{
ApplicationBase = Path.GetDirectoryName(assemblyFileUri.LocalPath),
ConfigurationFile = configFile
};
AppDomain ad = AppDomain.CreateDomain(execAssembly.GetName().Name,
null,
appSetup);
worker = (PluginDomainWorker)ad.CreateInstanceAndUnwrap(
execAssembly.FullName,
typeof(PluginDomainWorker).FullName);
return ad;
}
private static Assembly AssemblyResolve(object sender, ResolveEventArgs e)
{
// Определяем текущую папку библиотеки
Assembly assembly = Assembly.GetExecutingAssembly();
Uri assemblyFileUri = new Uri(assembly.CodeBase);
string modulePath = Path.GetDirectoryName(assemblyFileUri.LocalPath);
string[] nameSplit = e.Name.Split(',');
string path = Path.Combine(modulePath, nameSplit[0] + ".dll");
return Assembly.LoadFile(path);
}
[DllExport("InitUserDLLCom", CallingConvention.StdCall)]
public static int InitUserDLLCom(IntPtr value)
{
if (value != IntPtr.Zero)
{
byte[] menu = Encoding.GetEncoding(1251).GetBytes("Модуль DotNet\u0000");
byte[] function = Encoding.GetEncoding(1251).GetBytes("RunModule\u0000");
Marshal.Copy(menu, 0, value, menu.Length);
Marshal.Copy(function, 0, (IntPtr)((int)value + 255), function.Length);
}
return 1;
}
[DllExport("PgiCheckMenuItemCom", CallingConvention.StdCall)]
public static int PgiCheckMenuItemCom(IntPtr stFunction, IntPtr IPC)
{
try
{
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
AppDomain ad = CreatePluginDomain();
try
{
AppDomain.CurrentDomain.AssemblyResolve -= AssemblyResolve;
return worker.PgiCheckMenuItemCom(stFunction, IPC);
}
finally
{
AppDomain.Unload(ad);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + " " + ex.GetType().FullName);
}
return 0;
}
[DllExport("RunModule", CallingConvention.StdCall)]
public static void RunModule(IntPtr IPC)
{
try
{
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
AppDomain ad = CreatePluginDomain();
try
{
AppDomain.CurrentDomain.AssemblyResolve -= AssemblyResolve;
worker.RunModule(IPC);
}
finally
{
AppDomain.Unload(ad);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + " " + ex.GetType().FullName);
}
}
}
}