forked from Excel-DNA/IntelliSense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntelliSenseServer.cs
541 lines (484 loc) · 23.7 KB
/
IntelliSenseServer.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.Win32;
using ExcelDna.Integration;
using System.Threading;
namespace ExcelDna.IntelliSense
{
// This class implements the registration and activation of the calling add-in as an IntelliSense Server.
//
// Among different add-ins that are loaded into an Excel process, at most one IntelliSenseServer can be Active.
// This should always be an IntelliSenseServer with the greatest version number among those registered.
//
// At the moment the bookkeeping for registration and activation in the process is done with environment variables.
// (An attractive alternative is the hidden Excel name space: http://www.cpearson.com/excel/hidden.htm )
// This allows us not to make cross-AppDomain calls, which are problematic because assemblies are then loaded into multiple AppDomains, and
// since the mechanism is intended to cater for different assembly versions, this would be a problem. Also, as we don't control
// the CLR hosting configuration, we can't always set the MultiDomain flag on setup. COM mechanisms could work, but are complicated.
// Another approach would be to use a hidden Excel function that the Active server provides, and have all server register with the active server.
// When a new server should become active, it then tells the active server, and somehow gets all the other registrations...
// Registered Servers also register a macro with Excel through which control calls are to be made.
// This is against a unique GUID-based name for every registered server, so that the Activate call can be made on an inactive server.
// (To be called in a macro context only, e.g. from AutoOpen.)
// REMEMBER: COM events are not necessarily safe macro contexts.
public static class IntelliSenseServer
{
const string ServerVersion = "1.7.1"; // TODO: Define and manage this somewhere else
// NOTE: Do not change these constants in custom versions.
// They are part of the co-operative safety mechanism allowing different add-ins providing IntelliSense to work together safely.
const string DisabledVersionsMachineKeyName = @"HKEY_LOCAL_MACHINE\Software\ExcelDna\IntelliSense";
const string DisabledVersionsUserKeyName = @"HKEY_CURRENT_USER\Software\ExcelDna\IntelliSense";
const string DisabledVersionsValueName = "DisabledVersions";
const string DisabledVersionsVariable = "EXCELDNA_INTELLISENSE_DISABLEDVERSIONS";
const string ServersVariable = "EXCELDNA_INTELLISENSE_SERVERS";
const string ActiveServerVariable = "EXCELDNA_INTELLISENSE_ACTIVE_SERVER";
const string ControlMessageActivate = "ACTIVATE";
const string ControlMessageDeactivate = "DEACTIVATE";
const string ControlMessageRefresh = "REFRESH";
// Info for registration
// _serverId is a transient ID to identify this IntelliSense server - we could have used the ExcelDnaUtil.XllGuid one too,
// but it wasn't public in Excel-DNA v 0.32
// The advantage of the XllGuid one is that it would be a stable ID across runs.
static string _xllPath = ExcelDnaUtil.XllPath;
static Guid _serverId = Guid.NewGuid();
// Activation
static bool _isActive = false;
static IntelliSenseHelper _helper = null;
// The name change from Register to Install is just to force attention on this message for anyone upgrading
// - omitting the Uninstall call causes an Excel crash when the add-in is unloaded for any reason
[Obsolete("IntelliSenseServer now requires matching calls to Install (inside AutoOpen) and Uninstall (inside AutoClose)", true)]
public static void Register()
{
}
// Called directly from AutoOpen()
public static void Install()
{
TraceLogger.Initialize();
Logger.Initialization.Info($"IntelliSenseServer.Install Begin: Version {ServerVersion} in {AppDomain.CurrentDomain.FriendlyName}");
if (IsDisabled())
return;
RegisterControlMacro();
PublishRegistration();
bool shouldActivate = false;
var activeInfo = GetActiveRegistrationInfo();
if (activeInfo == null)
{
shouldActivate = true;
}
else if (RegistrationInfo.CompareVersions(ServerVersion, activeInfo.Version) > 0)
{
// Check version
// We're newer - deactivate the active server and activate ourselves.
shouldActivate = true;
}
else
{
Logger.Initialization.Info($"IntelliSenseServer not being activated now. Active Version: {activeInfo.Version}");
}
// Else we're not activating - there is an active server and it is the same version or newer
if (shouldActivate &&
(activeInfo == null || DeactivateServer(activeInfo)))
{
Activate();
}
AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload;
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
Logger.Initialization.Info("IntelliSenseServer.Install End");
}
// Should be called in a macro context, e.g. from AutoClose()
// Was added again in 1.0.10 to clean up WinEvents
// Maybe we won't need it if we can move to our own thread handling WinEvents
public static void Uninstall()
{
Logger.Initialization.Info($"IntelliSenseServer.Uninstall Begin: Version {ServerVersion} in {AppDomain.CurrentDomain.FriendlyName}");
if (IsDisabled())
return;
UnpublishRegistration();
if (_isActive)
{
Deactivate();
// See if there is another server to active
var highestRegistration = GetHighestPublishedRegistration();
if (highestRegistration != null)
{
ActivateServer(highestRegistration);
}
}
Logger.Initialization.Info($"IntelliSenseServer.Uninstall End");
}
// Invokes a Refresh on the Active server (if there is on)
// Must be called from a macro context
// Appropriate for calling after registering extra methods in an add-in
public static void Refresh()
{
Logger.Initialization.Info($"IntelliSenseServer.Refresh Begin");
if (_isActive)
{
RefreshProviders();
}
else
{
RegistrationInfo registrationInfo = GetActiveRegistrationInfo();
if (registrationInfo != null)
RefreshServer(registrationInfo);
}
Logger.Initialization.Info($"IntelliSenseServer.Refresh End");
}
static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
// CONSIDER: We get this quite late in the shutdown
// We should try to find a way to identify Excel shutdown a lot earlier
Logger.Initialization.Verbose("IntelliSenseServer ProcessExit Begin");
if (_isActive)
{
// Parachute in asap a call to prevent further XLPenHelper calls
XlCall.ShutdownStarted();
// Don't try to clean up clean up on process exit - all our resources are in-process anyway
// Leads to an error with the main thread SynchronizationContext, which might be shut down already.
// In particular we don't have to call UnhookWinEvent: "If the client's thread ends, the system automatically calls this function."
}
Logger.Initialization.Verbose("IntelliSenseServer ProcessExit End");
}
// DomainUnload runs when AutoClose() would run on the add-in.
// I.e when the add-in is explicitly unloaded via code or the add-ins dialog, or when the add-in is re-loaded
// (reload via File->Open is equivalent to unload, then load).
// We don't expect DomainUnload to run when Excel is shutting down.
static void CurrentDomain_DomainUnload(object sender, EventArgs e)
{
Logger.Initialization.Verbose("IntelliSenseServer DomainUnload Begin");
if (_isActive)
{
Logger.Initialization.Error("IntelliSenseServer DomainUnload while active - Add and IntelliSenseServer.Unregister() call in AutoClose() !");
}
Logger.Initialization.Verbose("IntelliSenseServer DomainUnload End");
}
// Called internally from the Register() call, or via the control function from another server.
internal static bool Activate()
{
try
{
SetActiveRegistrationInfo();
_isActive = true;
_helper = new IntelliSenseHelper();
// TODO: Perhaps also register macro to trigger updates
return true;
}
catch (Exception ex)
{
Logger.Initialization.Error($"IntelliSenseServer.Activate failed: {ex}");
return false;
}
}
// Should be called only on the Excel main thread, either from Unregister (AutoClose)
// or via the control function from another server when that server figures out that it must become the active server.
internal static bool Deactivate()
{
if (Thread.CurrentThread.ManagedThreadId != 1)
{
// At least try to log
Logger.Initialization.Error($"IntelliSenseServer.Deactivate not called on the main Excel thread!");
}
try
{
if (_helper != null)
_helper.Dispose();
_isActive = false;
ClearActiveRegistrationInfo();
return true;
}
catch (Exception ex)
{
Logger.Initialization.Error($"IntelliSenseServer.Deactivate error: {ex}");
return false;
}
}
// Called directly from Refresh or via the control function
internal static void RefreshProviders()
{
Logger.Initialization.Info($"IntelliSenseServer.RefreshProviders");
try
{
Debug.Assert(_helper != null);
_helper.RefreshProviders();
}
catch (Exception ex)
{
// TODO: Log
Logger.Initialization.Error($"IntelliSenseServer.RefreshProviders error: {ex}");
}
}
// NOTE: Please do not remove this safety mechanism in custom versions.
// The IntelliSense mechanism is co-operative between independent add-ins.
// Allowing a safe disable options is important to support future versions, and protect against problematic bugs.
// Checks whether this IntelliSense Server version is completely disabled
static bool IsDisabled()
{
var machineDisabled = Registry.GetValue(DisabledVersionsMachineKeyName, DisabledVersionsValueName, null) as string;
var userDisabled = Registry.GetValue(DisabledVersionsUserKeyName, DisabledVersionsValueName, null) as string;
var environmentDisabled = Environment.GetEnvironmentVariable(DisabledVersionsVariable) as string;
var thisVersion = ServerVersion;
var isDisabled = IsVersionMatch(thisVersion, machineDisabled) ||
IsVersionMatch(thisVersion, userDisabled) ||
IsVersionMatch(thisVersion, environmentDisabled);
if (isDisabled)
{
Logger.Initialization.Info($"IntelliSenseServer version {thisVersion} is disabled. MachineDisabled: {machineDisabled}, UserDisabled: {userDisabled}, EnvironmentDisabled: {environmentDisabled}");
}
return isDisabled;
}
// Attempts to activate the server described by registrationInfo
// return true for success, false for any problems
static bool ActivateServer(RegistrationInfo registrationInfo)
{
// Suppress errors if things go wrong, including unexpected return types.
try
{
var result = ExcelDna.Integration.XlCall.Excel(ExcelDna.Integration.XlCall.xlUDF, registrationInfo.GetControlMacroName(), ControlMessageActivate);
return (bool)result;
}
catch (Exception ex)
{
Logger.Initialization.Error(ex, $"IntelliSenseServer {registrationInfo.ToRegistrationString()} could not be activated.");
return false;
}
}
// Attempts to deactivate the server described by registrationInfo
// returns true for success, false if there were problems,
static bool DeactivateServer(RegistrationInfo registrationInfo)
{
// Suppress errors if things go wrong, including unexpected return types.
try
{
var result = ExcelDna.Integration.XlCall.Excel(ExcelDna.Integration.XlCall.xlUDF, registrationInfo.GetControlMacroName(), ControlMessageDeactivate);
if (result is ExcelError)
{
Logger.Initialization.Error($"IntelliSenseServer {registrationInfo.ToRegistrationString()} could not be deactivated.");
return false;
}
return (bool)result;
}
catch (Exception ex)
{
Logger.Initialization.Error(ex, $"IntelliSenseServer Deactivate call for {registrationInfo.ToRegistrationString()} failed.");
return false;
}
}
static bool RefreshServer(RegistrationInfo registrationInfo)
{
// Suppress errors if things go wrong, including unexpected return types.
try
{
var result = ExcelDna.Integration.XlCall.Excel(ExcelDna.Integration.XlCall.xlUDF, registrationInfo.GetControlMacroName(), ControlMessageRefresh);
if (result is ExcelError)
{
Logger.Initialization.Error($"IntelliSenseServer {registrationInfo.ToRegistrationString()} could not be deactivated.");
return false;
}
return (bool)result;
}
catch (Exception ex)
{
Logger.Initialization.Error(ex, $"IntelliSenseServer Deactivate call for {registrationInfo.ToRegistrationString()} failed.");
return false;
}
}
#region Registration
// NOTE: We have to be really careful about compatibility across versions here...
class RegistrationInfo : IComparable<RegistrationInfo>
{
public string XllPath;
public Guid ServerId;
public string Version;
public static RegistrationInfo FromRegistrationString(string registrationString)
{
try
{
var parts = registrationString.Split(',');
return new RegistrationInfo
{
XllPath = parts[0],
ServerId = Guid.ParseExact(parts[1], "N"),
Version = parts[2]
};
}
catch (Exception ex)
{
// TODO: Log
Debug.Print($"!!! ERROR: Invalid RegistrationString {registrationString}: {ex.Message}");
return null;
}
}
public string ToRegistrationString() => $"{XllPath},{ServerId:N},{Version}";
public int CompareTo(RegistrationInfo other) => CompareVersions(Version, other.Version);
public static string GetControlMacroName(Guid serverId) => $"IntelliSenseServerControl_{serverId:N}";
public string GetControlMacroName() => GetControlMacroName(ServerId);
// 1.2.0 is equal to 1.2
// Returns: -1 if version1 < version2
// 0 if version1 == version2
// 1 if version1 > version2
// Invalid version strings are considered very small (as if they are 0
public static int CompareVersions(string versionString1, string versionString2)
{
int[] version1 = ParseVersion(versionString1);
int[] version2 = ParseVersion(versionString2);
var maxLength = Math.Max(version1.Length, version2.Length);
for (int i = 0; i < maxLength; i++)
{
int v1 = (version1.Length - 1) < i ? 0 : version1[i];
int v2 = (version2.Length - 1) < i ? 0 : version2[i];
if (v1 < v2)
return -1;
if (v1 > v2)
return 1;
}
return 0;
}
}
// NOTE: We assume this will always run on the main thread in the process, so we have no synchronization.
// Max length for an environment variable is 32,767 characters.
static void PublishRegistration()
{
var oldServers = Environment.GetEnvironmentVariable(ServersVariable);
var newServers = (oldServers == null) ? RegistrationString : string.Join(";", oldServers, RegistrationString);
Environment.SetEnvironmentVariable(ServersVariable, newServers);
}
static void UnpublishRegistration()
{
var oldServers = new List<string>(Environment.GetEnvironmentVariable(ServersVariable).Split(';'));
var removed = oldServers.Remove(RegistrationString);
Debug.Assert(removed, ("IntelliSenseServer.UnpublishRegistration - Registration not found in " + ServersVariable));
var newServers = string.Join(";", oldServers);
Environment.SetEnvironmentVariable(ServersVariable, newServers);
}
// returns null if there is no active IntelliSense Server.
static RegistrationInfo GetActiveRegistrationInfo()
{
var activeString = Environment.GetEnvironmentVariable(ActiveServerVariable);
if (string.IsNullOrEmpty(activeString))
return null;
return RegistrationInfo.FromRegistrationString(activeString);
}
static void SetActiveRegistrationInfo()
{
var currentActive = Environment.GetEnvironmentVariable(ActiveServerVariable);
Debug.Assert(currentActive == null, "ActiveServer already set while activating");
Environment.SetEnvironmentVariable(ActiveServerVariable, RegistrationString);
}
static void ClearActiveRegistrationInfo()
{
Environment.SetEnvironmentVariable(ActiveServerVariable, null);
}
static string RegistrationString
{
get
{
var ri = new RegistrationInfo
{
XllPath = ExcelDnaUtil.XllPath,
ServerId = _serverId,
Version = ServerVersion
};
return ri.ToRegistrationString();
}
}
// Versions are dotted integer strings, e.g. 1.2.3
// Invalid strings parse as [0]
static int[] ParseVersion(string versionString)
{
if (string.IsNullOrEmpty(versionString))
{
return new int[] {0};
}
var versionParts = versionString.Split('.');
int[] version = new int[versionParts.Length];
for (int i = 0; i < versionParts.Length; i++)
{
int versionPart;
if (!int.TryParse(versionParts[i], out versionPart))
{
return new int[] {0};
}
version[i] = versionPart;
}
return version;
}
// Version patterns are either a single * (universal match) or a ","-joined lists of (dotted integer strings, with a possible trailing .* wildcard).
// e.g. 1.2.*, which would be matched with regex 1\.2(\.\d+)*
static bool IsVersionMatch(string version, string versionPattern)
{
if (string.IsNullOrEmpty(versionPattern))
return false;
if (versionPattern == "*")
return true; // Universal pattern - matches all versions
var regexParts = new List<string>();
var parts = versionPattern.Split(',');
foreach (var part in parts)
{
var trimmed = part.Trim();
if (Regex.IsMatch(trimmed, @"(\d+\.)+(\.\*)?", RegexOptions.None))
{
regexParts.Add("^" + trimmed.Replace(".*", @"(\.\d+)*") + "$");
}
}
var regex = string.Join(@"|", regexParts);
return Regex.IsMatch(version, regex);
}
// returns null if there are none registered
static RegistrationInfo GetHighestPublishedRegistration()
{
var servers = Environment.GetEnvironmentVariable(ServersVariable);
if (servers == null)
{
Debug.Print("!!! ERROR: ServersVariable not set");
return null;
}
return servers.Split(';')
.Select(str => RegistrationInfo.FromRegistrationString(str))
.Max();
}
#endregion
#region IntelliSense control function registered with Excel
static void RegisterControlMacro()
{
Func<object, object> delControl = control => IntelliSenseServerControl(control);
var name = RegistrationInfo.GetControlMacroName(_serverId);
ExcelIntegration.RegisterDelegates(new List<Delegate> { delControl },
new List<object> { new ExcelCommandAttribute { Name = name } }, // Macros in .xlls are always hidden
new List<List<object>> { new List<object> { null } });
// No Unregistration - that will happen automatically (and is only needed) when we are unloaded.
}
// NOTE: The name here is used by Reflection above (when registering the method with Excel)
static object IntelliSenseServerControl(object control)
{
string controlMessage = control as string;
if (controlMessage == ControlMessageActivate)
{
Debug.Print("IntelliSenseServer.Activate in AppDomain: " + AppDomain.CurrentDomain.FriendlyName);
return Activate();
}
else if (controlMessage == ControlMessageDeactivate)
{
Debug.Print("IntelliSenseServer.Deactivate in AppDomain: " + AppDomain.CurrentDomain.FriendlyName);
return Deactivate();
}
else if (controlMessage == ControlMessageRefresh)
{
Debug.Print("IntelliSenseServer.Refresh in AppDomain: " + AppDomain.CurrentDomain.FriendlyName);
if (!_isActive)
{
// Something went wrong...
Logger.Initialization.Error("IntelliSenseServer Refresh call on inactive server.");
return false;
}
RefreshProviders();
return true;
}
return false;
}
#endregion
}
}