|
1 | 1 | using System; |
2 | 2 | using System.Diagnostics; |
| 3 | +using System.Runtime.ExceptionServices; |
3 | 4 | using System.Runtime.InteropServices; |
4 | 5 |
|
5 | 6 | namespace ExcelDna.IntelliSense |
@@ -29,22 +30,51 @@ struct FmlaInfo |
29 | 30 | public int ichCaret; // char offset to blinking caret |
30 | 31 | } |
31 | 32 |
|
| 33 | + // This give a global mechanism to indicate shutdown as early as possible |
| 34 | + // Maybe helps with debugging... |
| 35 | + static bool _shutdownStarted = false; |
| 36 | + public static void ShutdownStarted() |
| 37 | + { |
| 38 | + _shutdownStarted = true; |
| 39 | + } |
| 40 | + |
32 | 41 | // Returns null if not in edit mode |
33 | 42 | // TODO: What do we know about the threading constraints on this call? |
| 43 | + |
| 44 | + // NOTE: I've only seen this crash during shutdown |
| 45 | + // We enable handling of the AccessViolation as best we can |
| 46 | + [HandleProcessCorruptedStateExceptions] |
34 | 47 | public static string GetFormulaEditPrefix() |
35 | 48 | { |
36 | | - var fmlaInfo = new FmlaInfo(); |
37 | | - var result = LPenHelper(xlGetFmlaInfo, ref fmlaInfo); |
38 | | - if (result != 0) |
39 | | - throw new InvalidOperationException("LPenHelper failed. Result: " + result); |
40 | | - if (fmlaInfo.wPointMode == xlModeReady) |
41 | | - return null; |
42 | | - |
43 | | - // Debug.Print("LPenHelper Status: PointMode: {0}, Formula: {1}, First: {2}, Last: {3}, Caret: {4}", |
44 | | - // fmlaInfo.wPointMode, Marshal.PtrToStringUni(fmlaInfo.lpch, fmlaInfo.cch), fmlaInfo.ichFirst, fmlaInfo.ichLast, fmlaInfo.ichCaret); |
45 | | - |
46 | | - var prefixLen = Math.Min(Math.Max(fmlaInfo.ichCaret, fmlaInfo.ichLast), fmlaInfo.cch); // I've never seen ichLast > cch !? |
47 | | - return Marshal.PtrToStringUni(fmlaInfo.lpch, prefixLen); |
| 49 | + if (_shutdownStarted) |
| 50 | + return null; |
| 51 | + |
| 52 | + try |
| 53 | + { |
| 54 | + var fmlaInfo = new FmlaInfo(); |
| 55 | + var result = LPenHelper(xlGetFmlaInfo, ref fmlaInfo); |
| 56 | + if (result != 0) |
| 57 | + { |
| 58 | + Logger.WindowWatcher.Warn($"LPenHelper Failed. Result: {result}"); |
| 59 | + throw new InvalidOperationException("LPenHelper Failed. Result: " + result); |
| 60 | + } |
| 61 | + if (fmlaInfo.wPointMode == xlModeReady) |
| 62 | + { |
| 63 | + // Logger.WindowWatcher.Verbose($"LPenHelper PointMode Ready"); |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + //Logger.WindowWatcher.Verbose("LPenHelper Status: PointMode: {0}, Formula: {1}, First: {2}, Last: {3}, Caret: {4}", |
| 68 | + // fmlaInfo.wPointMode, Marshal.PtrToStringUni(fmlaInfo.lpch, fmlaInfo.cch), fmlaInfo.ichFirst, fmlaInfo.ichLast, fmlaInfo.ichCaret); |
| 69 | + |
| 70 | + var prefixLen = Math.Min(Math.Max(fmlaInfo.ichCaret, fmlaInfo.ichLast), fmlaInfo.cch); // I've never seen ichLast > cch !? |
| 71 | + return Marshal.PtrToStringUni(fmlaInfo.lpch, prefixLen); |
| 72 | + } |
| 73 | + catch (AccessViolationException) |
| 74 | + { |
| 75 | + Logger.WindowWatcher.Warn("LPenHelper Access Violation!"); |
| 76 | + return null; |
| 77 | + } |
48 | 78 | } |
49 | 79 | } |
50 | 80 | } |
0 commit comments