11using System ;
2+ using System . Diagnostics ;
23using System . Runtime . InteropServices ;
34
45namespace ExcelDna . IntelliSense
@@ -32,9 +33,9 @@ static class UnicodeString
3233
3334 public static string ToString ( IntPtr pUnicodeString )
3435 {
35- short length = ( short ) Marshal . PtrToStructure ( pUnicodeString , typeof ( short ) ) ;
36- IntPtr buffer = Marshal . ReadIntPtr ( pUnicodeString , 4 ) ;
37- return Marshal . PtrToStringUni ( buffer , length / 2 ) ;
36+ short length = ( short ) Marshal . PtrToStructure ( pUnicodeString , typeof ( short ) ) ;
37+ IntPtr buffer = Marshal . ReadIntPtr ( pUnicodeString , IntPtr . Size ) ; // The offset is determined by the natural size for the struct packing
38+ return Marshal . PtrToStringUni ( buffer , length / 2 ) ;
3839 }
3940 }
4041
@@ -80,10 +81,11 @@ enum NtStatus : uint
8081 public LoaderNotification ( )
8182 {
8283 IntPtr context = IntPtr . Zero ; // new IntPtr(12345);
83- _notificationDelegate = Notification ;
84+ _notificationDelegate = Notification ; // To prevent GC of the delegate
8485 var status = LdrRegisterDllNotification ( 0 , _notificationDelegate , context , out _cookie ) ;
8586 if ( status != 0 )
8687 {
88+ Debug . Print ( $ "@@@@ LoaderNotification Result: { status } ") ;
8789 throw new InvalidOperationException ( $ "Error in LdrRegisterDlLNotification. Result: { status } ") ;
8890 }
8991 }
@@ -92,15 +94,13 @@ public LoaderNotification()
9294 // LoadNotification event handler must be very careful, not load any other managed library etc...
9395 void Notification ( Reason notificationReason , IntPtr pNotificationData , IntPtr context )
9496 {
95- IntPtr pFullDllName = Marshal . ReadIntPtr ( pNotificationData , 4 ) ;
96- string fullDllName = UnicodeString . ToString ( pFullDllName ) ;
97- NotificationEventArgs args = new NotificationEventArgs { Reason = notificationReason , FullDllName = fullDllName } ;
98- LoadNotification ? . Invoke ( this , args ) ;
99-
100- // Debug.Print($"@@@@ LdrNotification: {notificationReason} - {fullDllName}");
97+ IntPtr pFullDllName = Marshal . ReadIntPtr ( pNotificationData , IntPtr . Size ) ; // The offset is determined by the natural size for the struct packing
98+ string fullDllName = UnicodeString . ToString ( pFullDllName ) ;
99+ NotificationEventArgs args = new NotificationEventArgs { Reason = notificationReason , FullDllName = fullDllName } ;
100+ LoadNotification ? . Invoke ( this , args ) ;
101101 }
102102
103- #region IDisposable Support
103+ #region IDisposable Support
104104 // CONSIDER: We might not need the finalizer support ...
105105 private bool disposedValue = false ; // To detect redundant calls
106106
@@ -113,8 +113,11 @@ protected virtual void Dispose(bool disposing)
113113 // TODO: dispose managed state (managed objects).
114114 }
115115
116- var status = LdrUnregisterDllNotification ( _cookie ) ;
117- Logger . Initialization . Verbose ( $ "LoaderNotification LdrUnregisterDllNotification Result: { status } ") ;
116+ if ( _cookie != IntPtr . Zero )
117+ {
118+ var status = LdrUnregisterDllNotification ( _cookie ) ;
119+ Logger . Initialization . Verbose ( $ "LoaderNotification LdrUnregisterDllNotification Result: { status } ") ;
120+ }
118121 disposedValue = true ;
119122 }
120123 }
@@ -132,7 +135,7 @@ public void Dispose()
132135 Dispose ( true ) ;
133136 GC . SuppressFinalize ( this ) ;
134137 }
135- #endregion
138+ #endregion
136139
137140
138141 /*
0 commit comments