|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | + |
| 5 | +namespace ExcelDna.IntelliSense |
| 6 | +{ |
| 7 | + // Also not LDR_MODULE infor here: http://stackoverflow.com/questions/4242469/detect-when-a-module-dll-is-unloaded |
| 8 | + class LoaderNotification : IDisposable |
| 9 | + { |
| 10 | + public enum Reason : uint |
| 11 | + { |
| 12 | + Loaded = 1, |
| 13 | + Unloaded = 2 |
| 14 | + } |
| 15 | + |
| 16 | + // Helper for UNICODE_STRING type - couldn't figure out how to do it simply with Marshaling |
| 17 | + static class UnicodeString |
| 18 | + { |
| 19 | + // Layout is: |
| 20 | + |
| 21 | + // ushort Length; // Bytes |
| 22 | + // ushort MaximumLength; // Bytes |
| 23 | + // IntPtr buffer; |
| 24 | + |
| 25 | + public static string ToString(IntPtr pUnicodeString) |
| 26 | + { |
| 27 | + short length = (short)Marshal.PtrToStructure(pUnicodeString, typeof(short)); |
| 28 | + IntPtr buffer = Marshal.ReadIntPtr(pUnicodeString, 4); |
| 29 | + return Marshal.PtrToStringUni(buffer, length / 2); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // At the moment, _LDR_DLL_LOADED_NOTIFICATION_DATA and _LDR_DLL_UNLOADED_NOTIFICATION_DATA |
| 34 | + // are the same, so we don't have to bother with the union. |
| 35 | + // TODO: Check 64-bit packing |
| 36 | + [StructLayout(LayoutKind.Sequential)] |
| 37 | + struct Data |
| 38 | + { |
| 39 | + public uint Flags; // Reserved. |
| 40 | + public IntPtr FullDllName; // PCUNICODE_STRING // The full path name of the DLL module. |
| 41 | + public IntPtr BaseDllName; // PCUNICODE_STRING // The base file name of the DLL module. |
| 42 | + public IntPtr DllBase; // A pointer to the base address for the DLL in memory. |
| 43 | + public uint SizeOfImage; // The size of the DLL image, in bytes. |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + enum NtStatus : uint |
| 48 | + { |
| 49 | + // Success |
| 50 | + Success = 0x00000000, |
| 51 | + DllNotFound = 0xc0000135, |
| 52 | + // Many, many others... |
| 53 | + } |
| 54 | + |
| 55 | + delegate void LdrNotification(Reason notificationReason, IntPtr pNotificationData, IntPtr context); |
| 56 | + |
| 57 | + // Registers for notification when a DLL is first loaded. This notification occurs before dynamic linking takes place. |
| 58 | + [DllImport("ntdll.dll")] |
| 59 | + static extern uint /*NtStatus*/ LdrRegisterDllNotification( |
| 60 | + uint flags, // This parameter must be zero. |
| 61 | + LdrNotification notificationFunction, |
| 62 | + IntPtr context, |
| 63 | + out IntPtr cookie); |
| 64 | + |
| 65 | + [DllImport("ntdll.dll")] |
| 66 | + static extern uint /*NtStatus*/ LdrUnregisterDllNotification(IntPtr cookie); |
| 67 | + |
| 68 | + IntPtr _cookie; |
| 69 | + LdrNotification _notificationDelegate; |
| 70 | + |
| 71 | + public LoaderNotification() |
| 72 | + { |
| 73 | + IntPtr context = new IntPtr(12345); |
| 74 | + _notificationDelegate = Notification; |
| 75 | + var status = LdrRegisterDllNotification(0, _notificationDelegate, context, out _cookie); |
| 76 | + } |
| 77 | + |
| 78 | + // WARNING! LoaderLock danger here |
| 79 | + void Notification(Reason notificationReason, IntPtr pNotificationData, IntPtr context) |
| 80 | + { |
| 81 | + IntPtr pFullDllName = Marshal.ReadIntPtr(pNotificationData, 4); |
| 82 | + string fullDllName = UnicodeString.ToString(pFullDllName); |
| 83 | + Debug.Print($"@@@@ LdrNotification: {notificationReason} - {fullDllName}"); |
| 84 | + // Raise Event |
| 85 | + } |
| 86 | + |
| 87 | + #region IDisposable Support |
| 88 | + private bool disposedValue = false; // To detect redundant calls |
| 89 | + |
| 90 | + protected virtual void Dispose(bool disposing) |
| 91 | + { |
| 92 | + if (!disposedValue) |
| 93 | + { |
| 94 | + if (disposing) |
| 95 | + { |
| 96 | + // TODO: dispose managed state (managed objects). |
| 97 | + } |
| 98 | + |
| 99 | + var status = LdrUnregisterDllNotification(_cookie); |
| 100 | + disposedValue = true; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + ~LoaderNotification() |
| 105 | + { |
| 106 | + // Do not change this code. Put cleanup code in Dispose(bool disposing) above. |
| 107 | + Dispose(false); |
| 108 | + } |
| 109 | + |
| 110 | + // This code added to correctly implement the disposable pattern. |
| 111 | + public void Dispose() |
| 112 | + { |
| 113 | + // Do not change this code. Put cleanup code in Dispose(bool disposing) above. |
| 114 | + Dispose(true); |
| 115 | + GC.SuppressFinalize(this); |
| 116 | + } |
| 117 | + #endregion |
| 118 | + |
| 119 | + |
| 120 | + /* |
| 121 | + [StructLayout(LayoutKind.Sequential)] |
| 122 | + struct UnicodeString |
| 123 | + { |
| 124 | + // Helper for UNICODE_STRING type, with layout |
| 125 | + ushort Length; // Bytes |
| 126 | + ushort MaximumLength; // Bytes |
| 127 | + IntPtr buffer; |
| 128 | +
|
| 129 | + public override string ToString() |
| 130 | + { |
| 131 | + return Marshal.PtrToStringUni(buffer, Length / 2); |
| 132 | + } |
| 133 | + } |
| 134 | +
|
| 135 | + // At the moment, _LDR_DLL_LOADED_NOTIFICATION_DATA and _LDR_DLL_UNLOADED_NOTIFICATION_DATA |
| 136 | + // are the same, so we don't have to bother with the union. |
| 137 | + // TODO: Check 64-bit packing |
| 138 | + [StructLayout(LayoutKind.Sequential)] |
| 139 | + struct Data |
| 140 | + { |
| 141 | + public uint Flags; // Reserved. |
| 142 | + ???? [MarshalAs(UnmanagedType.LPStruct, MarshalTypeRef = typeof(UnicodeString))] |
| 143 | + public UnicodeString FullDllName; |
| 144 | + ???? [MarshalAs(UnmanagedType.LPStruct, MarshalTypeRef = typeof(UnicodeString))] |
| 145 | + public UnicodeString BaseDllName; |
| 146 | + //public IntPtr FullDllName; // PCUNICODE_STRING // The full path name of the DLL module. |
| 147 | + //public IntPtr BaseDllName; // PCUNICODE_STRING // The base file name of the DLL module. |
| 148 | + public IntPtr DllBase; // A pointer to the base address for the DLL in memory. |
| 149 | + public uint SizeOfImage; // The size of the DLL image, in bytes. |
| 150 | + } |
| 151 | +
|
| 152 | + delegate void LdrNotification(Reason notificationReason, [In] ref Data notificationData, IntPtr context); |
| 153 | +*/ |
| 154 | + } |
| 155 | +} |
0 commit comments